Minor changes on tasks subsystem.

This commit is contained in:
Andrey Antukh 2020-01-27 10:07:06 +01:00
parent f2cb2c3791
commit 8061ee7ca7
3 changed files with 12 additions and 13 deletions

View file

@ -51,7 +51,7 @@
[{:id "every 1 hour" [{:id "every 1 hour"
:cron #uxbox/cron "1 1 */1 * * ? *" :cron #uxbox/cron "1 1 */1 * * ? *"
:fn #'uxbox.tasks.demo-gc/handler :fn #'uxbox.tasks.demo-gc/handler
:props {:foo "bar"}}]) :props {:foo 1}}])
(defstate scheduler (defstate scheduler
:start (as-> (impl/scheduler-verticle {:schedule schedule}) $$ :start (as-> (impl/scheduler-verticle {:schedule schedule}) $$

View file

@ -16,5 +16,8 @@
(defn handler (defn handler
{:uxbox.tasks/name "demo-gc"} {:uxbox.tasks/name "demo-gc"}
[{:keys [props] :as task}] [{:keys [props] :as task}]
(Thread/sleep 500) (try
(prn (.getName (Thread/currentThread)) "demo-gc" (:id task) (:props task))) (Thread/sleep 100)
(prn (.getName (Thread/currentThread)) "demo-gc" (:id task) (:props task))
(catch Throwable e
nil)))

View file

@ -180,14 +180,11 @@
(db/with-atomic [conn db/pool] (db/with-atomic [conn db/pool]
(-> (db/query-one conn [sql:lock-scheduled-task id]) (-> (db/query-one conn [sql:lock-scheduled-task id])
(p/then (fn [result] (p/then (fn [result]
(if result (when result
(do
(prn (thr-name) "execute-scheduled-task" "task-locked")
(-> (p/do! ((:fn stask) stask)) (-> (p/do! ((:fn stask) stask))
(p/catch (fn [e] (p/catch (fn [e]
(log/warn "Excepton happens on executing scheduled task" e) (log/warn "Excepton happens on executing scheduled task" e)
nil)))) nil))))))
(prn (thr-name) "execute-scheduled-task" "task-already-locked"))))
(p/finally (fn [v e] (p/finally (fn [v e]
(-> (vc/current-context) (-> (vc/current-context)
(schedule-task stask))))))) (schedule-task stask)))))))
@ -196,14 +193,13 @@
[cron] [cron]
(s/assert tm/cron? cron) (s/assert tm/cron? cron)
(let [^Instant now (tm/now) (let [^Instant now (tm/now)
^Instant next (.toInstant (.getNextValidTimeAfter cron (Date/from now))) ^Instant next (tm/next-valid-instant-from cron now)
^Duration duration (Duration/between now next)] ^Duration duration (Duration/between now next)]
(.toMillis duration))) (.toMillis duration)))
(defn- schedule-task (defn- schedule-task
[ctx {:keys [cron] :as stask}] [ctx {:keys [cron] :as stask}]
(let [ms (ms-until-valid cron)] (let [ms (ms-until-valid cron)]
(prn (thr-name) "schedule-task" (:id stask) ms)
(vt/schedule! ctx (assoc stask (vt/schedule! ctx (assoc stask
:ctx ctx :ctx ctx
::vt/once true ::vt/once true