Skip to content

Commit

Permalink
console: delete/prioritise scheduled job/s
Browse files Browse the repository at this point in the history
  • Loading branch information
alishamohanty committed Jul 30, 2024
1 parent adba6c4 commit 66aa431
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/goose/brokers/redis/console.clj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
[:post dead/replay-job]
[:delete dead/delete-job]]}]
["/scheduled" {"" [[:get scheduled/get-jobs]
[:delete scheduled/purge-queue]]}]
[:delete scheduled/purge-queue]]
"/jobs" [[:post scheduled/prioritise-jobs]
[:delete scheduled/delete-jobs]]}]
["/css/style.css" console/load-css]
["/img/goose-logo.png" console/load-img]
["/js/index.js" console/load-js]
Expand Down
29 changes: 27 additions & 2 deletions src/goose/brokers/redis/console/pages/scheduled.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
(defn jobs-table [{:keys [base-path jobs]}]
[:form {:action (str base-path "/jobs")
:method "post"}
(c/action-btns [(c/prioritise-btn)
(c/delete-btn
[:div "Are you sure you want to delete selected jobs?"])])
[:table.jobs-table
[:thead
[:th.when-h [:div.when-label "When"]
Expand All @@ -26,7 +29,8 @@
[:th.id-h "Id"]
[:th.queue-h "Queue"]
[:th.execute-fn-sym-h "Execute fn symbol"]
[:th.type-h "Type"]]]
[:th.type-h "Type"]
[:th.checkbox-h [:input {:type "checkbox" :id "checkbox-h"}]]]]
[:tbody
(for [{:keys [id queue execute-fn-sym schedule-run-at] :as j} jobs]
(let [relative-time (when schedule-run-at (utils/relative-time schedule-run-at))
Expand All @@ -38,7 +42,12 @@
[:td [:div.id id]]
[:td [:div.queue] queue]
[:td [:div.execute-fn-sym (str execute-fn-sym)]]
[:td.type (if (job/retried? j) "Retrying" "Scheduled")]]))]]])
[:td.type (if (job/retried? j) "Retrying" "Scheduled")]
[:td [:div.checkbox-div
[:input {:name "jobs"
:type "checkbox"
:class "checkbox"
:value (utils/encode-to-str j)}]]]]))]]])

(defn- jobs-page-view [{:keys [total-jobs] :as data}]
[:div.redis
Expand Down Expand Up @@ -92,3 +101,19 @@
:keys [prefix-route]}]
(scheduled-jobs/purge redis-conn)
(response/redirect (prefix-route "/scheduled")))

(defn prioritise-jobs [{{:keys [broker]} :console-opts
:keys [prefix-route]
params :params}]
(let [{:keys [encoded-jobs]} (specs/validate-req-params params)
jobs (mapv utils/decode-from-str encoded-jobs)]
(apply scheduled-jobs/prioritises-execution (:redis-conn broker) jobs)
(response/redirect (prefix-route "/scheduled"))))

(defn delete-jobs [{{:keys [broker]} :console-opts
:keys [prefix-route]
params :params}]
(let [{:keys [encoded-jobs]} (specs/validate-req-params params)
jobs (mapv utils/decode-from-str encoded-jobs)]
(apply scheduled-jobs/delete (:redis-conn broker) jobs)
(response/redirect (prefix-route "/scheduled"))))
41 changes: 40 additions & 1 deletion test/goose/brokers/redis/console/page_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,19 @@
:body ""
:header {"Location" "/scheduled"}})]
(console/handler tu/redis-producer (mock/request :delete "/scheduled"))
(is (true? (spy/called-once? scheduled/purge-queue))))))
(is (true? (spy/called-once? scheduled/purge-queue)))))
(testing "Main handler should invoke delete scheduled jobs"
(with-redefs [scheduled/delete-jobs (spy/stub {:status 302
:body ""
:headers {"Location" "/scheduled"}})]
(console/handler tu/redis-producer (mock/request :delete "/scheduled/jobs"))
(is (true? (spy/called-once? scheduled/delete-jobs)))))
(testing "Main handler should invoke prioritise scheduled jobs"
(with-redefs [scheduled/prioritise-jobs (spy/stub {:status 302
:body ""
:headers {"Location" "/scheduled"}})]
(console/handler tu/redis-producer (mock/request :post "/scheduled/jobs"))
(is (true? (spy/called-once? scheduled/prioritise-jobs))))))

(deftest enqueued-purge-queue-test
(testing "Should purge a queue"
Expand Down Expand Up @@ -481,3 +493,30 @@
:status 302} (scheduled/purge-queue {:console-opts tu/redis-console-opts
:prefix-route str})))
(is (= 0 (scheduled-jobs/size tu/redis-conn)))))

(deftest scheduled-prioritise-jobs-test
(testing "Should prioritise scheduled jobs"
(f/create-jobs-in-redis {:scheduled 12})
(let [jobs (scheduled-jobs/get-by-range tu/redis-conn 0 6)
seven-encoded-jobs (mapv u/encode-to-str jobs)]
(is (= 12 (scheduled-jobs/size tu/redis-conn)))
(is (= {:body ""
:headers {"Location" "/scheduled"}
:status 302} (scheduled/prioritise-jobs {:console-opts tu/redis-console-opts
:params {:jobs seven-encoded-jobs}
:prefix-route str})))
(is (= 7 (enqueued-jobs/size tu/redis-conn tu/queue)))
(is (= 5 (scheduled-jobs/size tu/redis-conn))))))

(deftest scheduled-delete-jobs-test
(testing "Should delete scheduled jobs"
(f/create-jobs-in-redis {:scheduled 12})
(let [jobs (scheduled-jobs/get-by-range tu/redis-conn 0 9)
ten-encoded-jobs (mapv u/encode-to-str jobs)]
(is (= 12 (scheduled-jobs/size tu/redis-conn)))
(is (= {:body ""
:headers {"Location" "/scheduled"}
:status 302} (scheduled/delete-jobs {:console-opts tu/redis-console-opts
:params {:jobs ten-encoded-jobs}
:prefix-route str})))
(is (= 2 (scheduled-jobs/size tu/redis-conn))))))

0 comments on commit 66aa431

Please sign in to comment.