Last active
May 8, 2018 20:46
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns http-poll.core | |
(:require [clojure.core.async | |
:refer [>! <! go chan timeout]] | |
[clj-http.client :as http])) | |
(defn print-status | |
[uid s] | |
(println (str (subs (str uid) 0 4) " " s))) | |
(defn init-request | |
[url uid] | |
(print-status uid "Started request") | |
(let [start-chan (chan)] | |
(go | |
;; Simulating 1 sec to init http request | |
(<! (timeout 1000)) | |
;; Simulating start or fail, | |
;; will be replaced by actual http request | |
(>! start-chan (rand-nth [:started :started :started :failed]))) | |
start-chan)) | |
(defn poll | |
[url uid] | |
(print-status uid "Polling") | |
(let [poll-chan (chan)] | |
(go | |
;; Simulating 1 sec for http request. | |
;; | |
(<! (timeout 1000)) | |
;; Simulating status of poll, | |
;; will be replace by actual http request | |
(>! poll-chan (rand-nth [:ongoing :ongoing :ongoing :completed :failed]))) | |
poll-chan)) | |
(def statuses (atom {})) | |
(defn set-status! | |
[uid status] | |
(print-status uid (str "status of uid =" status)) | |
(swap! statuses #(assoc % uid status))) | |
(defn start-poll | |
[] | |
(let [uid (java.util.UUID/randomUUID) | |
url (str "some-url?uid=" uid)] | |
(set-status! uid :initializing) | |
(go (let [start-chan (init-request url uid) | |
status (<! start-chan)] | |
(print-status uid "Init request finished") | |
(set-status! uid status) | |
(when-not (= :failed (get @statuses uid)) | |
(print-status uid "Polling started") | |
(while (contains? #{:started :ongoing} (get @statuses uid)) | |
;; Poll once every second | |
(<! (timeout 1000)) | |
(let [poll-chan (poll url uid)] | |
(set-status! uid (<! poll-chan)))))) | |
(print-status uid (str "Request completed with status " (get @statuses uid)))) | |
uid)) | |
;(start-poll) ;; Returns uid immediately | |
;d82e status of uid =:initializing | |
;d82e Started request | |
;=> #uuid"d82e9750-ce32-4d1f-b30d-b866b3c73903" | |
;d82e Init request finished | |
;d82e status of uid =:started | |
;d82e Polling started | |
;d82e Polling | |
;d82e status of uid =:failed | |
;d82e Request completed with status :failed | |
;; Starts 10 simultaneous polls. About 4 seem to run concurrently. | |
;; It's possible to interact with the REPL during the run | |
;(repeatedly 10 start-poll) | |
;7bec status of uid =:initializing | |
;4eaf status of uid =:initializing | |
;7bec Started request | |
;4eaf Started request | |
;9d35 status of uid =:initializing | |
;9d35 Started request | |
;76cd status of uid =:initializing | |
;4393 status of uid =:initializing | |
;6cb3 status of uid =:initializing | |
;b38e status of uid =:initializing | |
;7ebb status of uid =:initializing | |
;13e0 status of uid =:initializing | |
;776c status of uid =:initializing | |
;=> | |
;(#uuid"7bec8224-aef9-4cb9-b979-de101f952a0b" | |
; #uuid"4eafa5db-52f4-409e-8f7e-dbc383afd601" | |
; #uuid"9d35169f-8930-4be4-bbc1-93b1b7f30d4b" | |
; #uuid"76cd484b-66f8-4798-a0ba-3fc286c2883b" | |
; #uuid"4393a084-09cb-4f82-bb78-0e4a58d19543" | |
; #uuid"6cb3158a-7102-4531-a10f-bc1fadc3f1c0" | |
; #uuid"b38e984f-e21b-4eb3-ae7c-0642ae04aeb4" | |
; #uuid"7ebb9411-a571-4b8f-8955-2cd272ef58e6" | |
; #uuid"13e00d6e-2f13-4148-a787-abcc20fe0fa1" | |
; #uuid"776c665b-2453-40e3-bec9-1e4da820f87d") | |
;6cb3 Started request | |
;b38e Started request | |
;7ebb Started request | |
;13e0 Started request | |
;776c Started request | |
;76cd Started request | |
;4393 Started request | |
;7bec Init request finished | |
;4eaf Init request finished | |
;7bec status of uid =:failed | |
;7bec Request completed with status :failed | |
;9d35 Init request finished | |
;4eaf status of uid =:started | |
;4eaf Polling started | |
;9d35 status of uid =:started | |
;9d35 Polling started | |
;7ebb Init request finished | |
;13e0 Init request finished | |
;7ebb status of uid =:failed | |
;6cb3 Init request finished | |
;13e0 status of uid =:started | |
;6cb3 status of uid =:failed | |
;6cb3 Request completed with status :failed | |
;b38e Init request finished | |
;b38e status of uid =:started | |
;b38e Polling started | |
;13e0 Polling started | |
;7ebb Request completed with status :failed | |
;776c Init request finished | |
;776c status of uid =:failed | |
;776c Request completed with status :failed | |
;76cd Init request finished | |
;76cd status of uid =:started | |
;76cd Polling started | |
;4393 Init request finished | |
;4393 status of uid =:started | |
;4393 Polling started | |
;4eaf Polling | |
;9d35 Polling | |
;b38e Polling | |
;13e0 Polling | |
;4393 Polling | |
;76cd Polling | |
;4eaf status of uid =:completed | |
;9d35 status of uid =:completed | |
;9d35 Request completed with status :completed | |
;4eaf Request completed with status :completed | |
;13e0 status of uid =:ongoing | |
;4393 status of uid =:complete | |
;db38e status of uid =:ongoing | |
;76cd status of uid =:failed | |
;76cd Request completed with status :failed | |
;4393 Request completed with status :completed | |
;b38e Polling13e0 Polling | |
;b38e status of uid =:completed | |
;13e0 status of uid =:ongoing | |
;b38e Request completed with status :completed | |
;13e0 Polling | |
;13e0 status of uid =:ongoing | |
;13e0 Polling | |
;13e0 status of uid =:failed | |
;13e0 Request completed with status :failed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment