-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement cronjob component and add integration tests
- Loading branch information
Showing
3 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
(ns common-clj.component.cronjob | ||
(:require [schema.core :as s] | ||
[com.stuartsierra.component :as component] | ||
[hara.io.scheduler :as io.scheduler])) | ||
|
||
(s/defn tasks-with-components | ||
[tasks components] | ||
(reduce (fn [tasks' task-id] (update-in tasks' [task-id :params] #(merge % {:components components}))) tasks (keys tasks))) | ||
|
||
(defrecord ConJob [config datalevin rabbitmq-producer tasks] | ||
component/Lifecycle | ||
(start [component] | ||
(let [tasks' (tasks-with-components (:tasks tasks) {:datalevin (:datalevin datalevin) | ||
:config (:config config) | ||
:rabbitmq-producer (:rabbitmq-producer rabbitmq-producer)}) | ||
scheduler (io.scheduler/scheduler tasks' {} {:clock {:timezone "UTC"}})] | ||
|
||
(io.scheduler/start! scheduler) | ||
|
||
(merge component {:jobs {:scheduler scheduler}}))) | ||
|
||
(stop [component] | ||
component)) | ||
|
||
(defn new-cronjob | ||
[tasks] | ||
(->ConJob {} {} {} {:tasks tasks})) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
(ns integration.cronjob-component-test | ||
(:require [clojure.test :refer :all] | ||
[com.stuartsierra.component :as component] | ||
[common-clj.component.cronjob :as component.cronjob] | ||
[schema.test :as s])) | ||
|
||
(def test-state (atom nil)) | ||
|
||
(defn test-task | ||
[_as-of | ||
{:keys [_components param-test] :as _params} | ||
_instance] | ||
(reset! test-state param-test)) | ||
|
||
(def tasks {:test-task {:handler test-task | ||
:schedule "* * * * * * *" | ||
:params {:param-test :ok}}}) | ||
|
||
(def system-test | ||
(component/system-map | ||
:cronjob (component.cronjob/new-cronjob tasks))) | ||
|
||
(s/deftest cronjob-task-execution-test | ||
(let [system (component/start system-test)] | ||
(testing "that cronjob task is executed" | ||
(reset! test-state nil) | ||
(Thread/sleep 1000) | ||
|
||
(is (= :ok @test-state)) | ||
(component/stop-system system)))) |