Skip to content

Commit

Permalink
implement cronjob component and add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
macielti committed Jul 21, 2024
1 parent 5fd9eb6 commit cfa3ec1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
3 changes: 2 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
[clojure.java-time "1.4.2"]
[clj-rate-limiter "0.1.6-RC1"]
[com.github.liquidz/antq "RELEASE"]
[dev.weavejester/medley "1.8.0"]]
[dev.weavejester/medley "1.8.0"]
[hara/io.scheduler "3.0.8"]]

:injections [(require 'hashp.core)]

Expand Down
27 changes: 27 additions & 0 deletions src/common_clj/component/cronjob.clj
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}))
30 changes: 30 additions & 0 deletions test/integration/integration/cronjob_component_test.clj
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))))

0 comments on commit cfa3ec1

Please sign in to comment.