Skip to content

Commit

Permalink
console: purge scheduled jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
alishamohanty committed Jul 24, 2024
1 parent 0348948 commit adba6c4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/goose/brokers/redis/console.clj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
["/job/" :id] [[:get dead/get-job]
[:post dead/replay-job]
[:delete dead/delete-job]]}]
["/scheduled" {"" scheduled/get-jobs}]
["/scheduled" {"" [[:get scheduled/get-jobs]
[:delete scheduled/purge-queue]]}]
["/css/style.css" console/load-css]
["/img/goose-logo.png" console/load-img]
["/js/index.js" console/load-js]
Expand Down
20 changes: 15 additions & 5 deletions src/goose/brokers/redis/console/pages/scheduled.clj
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
(ns goose.brokers.redis.console.pages.scheduled
(:require [goose.brokers.redis.console.data :as data]
(:require [goose.brokers.redis.api.scheduled-jobs :as scheduled-jobs]
[goose.brokers.redis.console.data :as data]
[goose.brokers.redis.console.pages.components :as c]
[goose.brokers.redis.console.specs :as specs]
[goose.console :as console]
[goose.defaults :as d]
[goose.job :as job]
[goose.utils :as u]
[goose.utils :as utils]
[ring.util.response :as response])
(:import
(java.util Date)))
Expand All @@ -28,7 +29,7 @@
[:th.type-h "Type"]]]
[:tbody
(for [{:keys [id queue execute-fn-sym schedule-run-at] :as j} jobs]
(let [relative-time (when schedule-run-at (u/relative-time schedule-run-at))
(let [relative-time (when schedule-run-at (utils/relative-time schedule-run-at))
absolute-time (when schedule-run-at (Date. ^Long schedule-run-at))]
[:tr
[:td.when
Expand All @@ -47,15 +48,19 @@
[:div.pagination
(when total-jobs
(c/pagination data))]
(jobs-table data)]])
(jobs-table data)
(when (and total-jobs (> total-jobs 0))
[:div.bottom
(console/purge-confirmation-dialog data)
[:button {:class "btn btn-danger btn-lg purge-dialog-show"} "Purge"]])]])

(defn validate-get-jobs [{:keys [page filter-type filter-value limit]}]
(let [f-type (specs/validate-or-default ::specs/scheduled-filter-type filter-type)]
{:page (specs/validate-or-default ::specs/page
(specs/str->long page)
(specs/str->long page)
d/page)
:filter-type f-type
:filter-type f-type
:filter-value (case f-type
"id" (specs/validate-or-default ::specs/job-id
(parse-uuid filter-value)
Expand All @@ -82,3 +87,8 @@
:base-path (prefix-route "/scheduled")
:prefix-route prefix-route
:params params)))))

(defn purge-queue [{{{:keys [redis-conn]} :broker} :console-opts
:keys [prefix-route]}]
(scheduled-jobs/purge redis-conn)
(response/redirect (prefix-route "/scheduled")))
24 changes: 21 additions & 3 deletions test/goose/brokers/redis/console/page_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[clojure.test :refer [deftest is testing use-fixtures]]
[goose.brokers.redis.api.dead-jobs :as dead-jobs]
[goose.brokers.redis.api.enqueued-jobs :as enqueued-jobs]
[goose.brokers.redis.api.scheduled-jobs :as scheduled-jobs]
[goose.brokers.redis.console :as console]
[goose.brokers.redis.console.pages.dead :as dead]
[goose.brokers.redis.console.pages.enqueued :as enqueued]
Expand Down Expand Up @@ -118,7 +119,7 @@
(is (nil? (:filter-value (scheduled/validate-get-jobs {:filter-type "execute-fn-sym"
:filter-value (rand-nth [123 nil])}))))
(is (= "any-string-value" (:filter-value (scheduled/validate-get-jobs {:filter-type "queue"
:filter-value "any-string-value"}))))
:filter-value "any-string-value"}))))
(is (= nil (:filter-value (scheduled/validate-get-jobs {:filter-type "queue"
:filter-value (rand-nth [:default nil 123])}))))
(is (= "scheduled" (:filter-value (scheduled/validate-get-jobs {:filter-type "type"
Expand Down Expand Up @@ -231,10 +232,17 @@
(with-redefs [dead/get-job (spy/stub {:status 200 :body "<html> Dead job page </html>"})]
(console/handler tu/redis-producer (mock/request :get (str "/dead/job/" (str (random-uuid)))))
(is (true? (spy/called-once? dead/get-job)))))

(testing "Main handler should invoke get scheduled jobs"
(with-redefs [scheduled/get-jobs (spy/stub {:status 200 :body "<html> Scheduled Jobs page </html>"})]
(console/handler tu/redis-producer (mock/request :get (str "/scheduled")))
(is (true? (spy/called-once? scheduled/get-jobs))))))
(console/handler tu/redis-producer (mock/request :get "/scheduled"))
(is (true? (spy/called-once? scheduled/get-jobs)))))
(testing "Main handler should invoke purge scheduled jobs"
(with-redefs [scheduled/purge-queue (spy/stub {:status 302
:body ""
:header {"Location" "/scheduled"}})]
(console/handler tu/redis-producer (mock/request :delete "/scheduled"))
(is (true? (spy/called-once? scheduled/purge-queue))))))

(deftest enqueued-purge-queue-test
(testing "Should purge a queue"
Expand Down Expand Up @@ -463,3 +471,13 @@
:prefix-route str})))
(is (= 1 (dead-jobs/size tu/redis-conn)))
(is (= [j1] (dead-jobs/get-by-range tu/redis-conn 0 1))))))

(deftest scheduled-purge-queue-test
(testing "Should purge scheduled queue"
(f/create-jobs-in-redis {:scheduled 12})
(is (= 12 (scheduled-jobs/size tu/redis-conn)))
(is (= {:body ""
:headers {"Location" "/scheduled"}
:status 302} (scheduled/purge-queue {:console-opts tu/redis-console-opts
:prefix-route str})))
(is (= 0 (scheduled-jobs/size tu/redis-conn)))))

0 comments on commit adba6c4

Please sign in to comment.