From 8d7baa75dee927be807d19e4424f4d7f8562e7bb Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Thu, 11 Aug 2022 17:29:25 +0200 Subject: [PATCH] :sparkles: Improve tasks-gc task --- backend/src/app/tasks/tasks_gc.clj | 33 +++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/backend/src/app/tasks/tasks_gc.clj b/backend/src/app/tasks/tasks_gc.clj index 1350c4abf..784b7db13 100644 --- a/backend/src/app/tasks/tasks_gc.clj +++ b/backend/src/app/tasks/tasks_gc.clj @@ -8,7 +8,9 @@ "A maintenance task that performs a cleanup of already executed tasks from the database table." (:require + [app.common.data :as d] [app.common.logging :as l] + [app.config :as cf] [app.db :as db] [app.util.time :as dt] [clojure.spec.alpha :as s] @@ -16,20 +18,31 @@ (declare sql:delete-completed-tasks) -(s/def ::max-age ::dt/duration) +(s/def ::min-age ::dt/duration) (defmethod ig/pre-init-spec ::handler [_] - (s/keys :req-un [::db/pool ::max-age])) + (s/keys :req-un [::db/pool] + :opt-un [::min-age])) + +(defmethod ig/prep-key ::handler + [_ cfg] + (merge {:min-age cf/deletion-delay} + (d/without-nils cfg))) (defmethod ig/init-key ::handler - [_ {:keys [pool max-age] :as cfg}] - (fn [_] - (db/with-atomic [conn pool] - (let [interval (db/interval max-age) - result (db/exec-one! conn [sql:delete-completed-tasks interval]) - result (:next.jdbc/update-count result)] - (l/debug :hint "trim completed tasks table" :removed result) - result)))) + [_ {:keys [pool] :as cfg}] + (fn [params] + (let [min-age (or (:min-age params) (:min-age cfg))] + (db/with-atomic [conn pool] + (let [interval (db/interval min-age) + result (db/exec-one! conn [sql:delete-completed-tasks interval]) + result (:next.jdbc/update-count result)] + (l/debug :hint "task finished" :total result) + + (when (:rollback? params) + (db/rollback! conn)) + + result))))) (def ^:private sql:delete-completed-tasks