Skip to content

Instantly share code, notes, and snippets.

@maxp
Created August 8, 2017 14:11
Show Gist options
  • Save maxp/b3d7dee929019d4694aac1bab565675a to your computer and use it in GitHub Desktop.
Save maxp/b3d7dee929019d4694aac1bab565675a to your computer and use it in GitHub Desktop.
(defn get-slots [t0 t1 limit]
(try
(mq/with-collection (dbc) ENROLLS_COLL
(mq/find {:at {:$gte t0 :$lt t1} :state 0})
(mq/limit limit))
(catch Exception e
(warn e))))
;
(defn send-slot [{bid :bid uid :uid crs :crs day :day _id :_id}]
(if-let [ptok (get-bot-ptok bid)]
(try
(debug "send to user:" bid uid crs day)
;; NOTE: mark as served
(mc/update (dbc) ENROLLS_COLL {:_id _id} {:$set {:state (int 1)}})
(update-user-state (make-sid bid uid)
{:crs {:id crs :day day} :wait nil})
;; daystart
(emit
{ :fb-page {:token ptok}
:sender {:id uid}
:postback {:payload (str "daystart " crs " " day)}})
;
(catch Exception e
(warn "send-slot:" bid uid crs day e)))
;;
(warn "send-slot bot inactive:" bid)))
;
(def run-loop (atom nil))
(defn timeslot-loop []
(reset! run-loop true)
(loop []
(let [t1 (tc/now)
t0 (tc/minus t1 poll-window)]
(doseq [slot (get-slots t0 t1 poll-limit)]
(debug "timeslot:" slot)
(send-slot slot))
;
(Thread/sleep poll-pause)
(when @run-loop
(recur)))))
;
(defstate timeslot-poller
:start
(-> timeslot-loop Thread. .start)
:stop
(reset! run-loop false))
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment