Skip to content

Commit

Permalink
Merge pull request #112 from macielti/cronjob-component
Browse files Browse the repository at this point in the history
Add CronJob component
  • Loading branch information
macielti authored Jul 21, 2024
2 parents 5fd9eb6 + a26ce22 commit bba6d00
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
12 changes: 10 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ of [keepachangelog.com](http://keepachangelog.com/).

## [Unreleased]

## [25.51.50] - 2024-07-12
## [25.52.50] - 2024-07-21

## Added

- Add CronJob component.

## [25.51.50] - 2024-07-21

## Changed

Expand Down Expand Up @@ -683,7 +689,9 @@ of [keepachangelog.com](http://keepachangelog.com/).

- Add `loose-schema` function.

[Unreleased]: https://github.com/macielti/common-clj/compare/v25.51.50...HEAD
[Unreleased]: https://github.com/macielti/common-clj/compare/v25.52.50...HEAD

[25.52.50]: https://github.com/macielti/common-clj/compare/v25.51.50...v25.52.50

[25.51.50]: https://github.com/macielti/common-clj/compare/v25.51.49...v25.51.50

Expand Down
5 changes: 3 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject net.clojars.macielti/common-clj "25.51.50"
(defproject net.clojars.macielti/common-clj "25.52.50"
:description "Just common Clojure code that I use across projects"
:url "https://github.com/macielti/common-clj"
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
Expand Down 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 bba6d00

Please sign in to comment.