diff --git a/src/goose/brokers/redis/console.clj b/src/goose/brokers/redis/console.clj index 2c091049..1f408c58 100644 --- a/src/goose/brokers/redis/console.clj +++ b/src/goose/brokers/redis/console.clj @@ -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] diff --git a/src/goose/brokers/redis/console/pages/scheduled.clj b/src/goose/brokers/redis/console/pages/scheduled.clj index 3449c737..c775827a 100644 --- a/src/goose/brokers/redis/console/pages/scheduled.clj +++ b/src/goose/brokers/redis/console/pages/scheduled.clj @@ -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"] @@ -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)) @@ -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 @@ -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")))) diff --git a/test/goose/brokers/redis/console/page_test.clj b/test/goose/brokers/redis/console/page_test.clj index 8d47414c..fe857d7e 100644 --- a/test/goose/brokers/redis/console/page_test.clj +++ b/test/goose/brokers/redis/console/page_test.clj @@ -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" @@ -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))))))