From 39119ac0401cc9b9006b0e3c769f5ded29539335 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Fri, 24 May 2024 11:06:30 +0200 Subject: [PATCH] :sparkles: Reuse team deletion logic on orphan teams gc task --- backend/src/app/tasks/orphan_teams_gc.clj | 68 +++++++++++++---------- 1 file changed, 38 insertions(+), 30 deletions(-) diff --git a/backend/src/app/tasks/orphan_teams_gc.clj b/backend/src/app/tasks/orphan_teams_gc.clj index c04123a831..8869c72ccb 100644 --- a/backend/src/app/tasks/orphan_teams_gc.clj +++ b/backend/src/app/tasks/orphan_teams_gc.clj @@ -10,29 +10,10 @@ [app.common.logging :as l] [app.db :as db] [app.util.time :as dt] + [app.worker :as wrk] [clojure.spec.alpha :as s] [integrant.core :as ig])) -(declare ^:private delete-orphan-teams!) - -(defmethod ig/pre-init-spec ::handler [_] - (s/keys :req [::db/pool])) - -(defmethod ig/init-key ::handler - [_ cfg] - (fn [params] - (db/tx-run! cfg (fn [{:keys [::db/conn] :as cfg}] - (l/inf :hint "gc started" :rollback? (boolean (:rollback? params))) - (let [total (delete-orphan-teams! cfg)] - (l/inf :hint "task finished" - :teams total - :rollback? (boolean (:rollback? params))) - - (when (:rollback? params) - (db/rollback! conn)) - - {:processed total}))))) - (def ^:private sql:get-orphan-teams "SELECT t.id FROM team AS t @@ -44,16 +25,43 @@ FOR UPDATE OF t SKIP LOCKED") -(defn- delete-orphan-teams! +(defn- delete-orphan-teams "Find all orphan teams (with no members) and mark them for deletion (soft delete)." [{:keys [::db/conn] :as cfg}] - (->> (db/cursor conn sql:get-orphan-teams) - (map :id) - (reduce (fn [total team-id] - (l/trc :hint "mark orphan team for deletion" :id (str team-id)) - (db/update! conn :team - {:deleted-at (dt/now)} - {:id team-id}) - (inc total)) - 0))) + (let [deleted-at (dt/now)] + (->> (db/cursor conn sql:get-orphan-teams) + (map :id) + (reduce (fn [total team-id] + (l/trc :hint "mark orphan team for deletion" :id (str team-id)) + + (db/update! conn :team + {:deleted-at deleted-at} + {:id team-id}) + + (wrk/submit! {::wrk/task :delete-object + ::wrk/conn conn + :object :team + :deleted-at deleted-at + :id team-id}) + + (inc total)) + 0)))) + +(defmethod ig/pre-init-spec ::handler [_] + (s/keys :req [::db/pool])) + +(defmethod ig/init-key ::handler + [_ cfg] + (fn [params] + (db/tx-run! cfg (fn [{:keys [::db/conn] :as cfg}] + (l/inf :hint "gc started" :rollback? (boolean (:rollback? params))) + (let [total (delete-orphan-teams cfg)] + (l/inf :hint "task finished" + :teams total + :rollback? (boolean (:rollback? params))) + + (when (:rollback? params) + (db/rollback! conn)) + + {:processed total})))))