Welcome! Please see the About page for a little more info on how this works.

0 votes
in Sequences by
edited by
(defn- find-story-ids-by-tag [tag-id]

(map :id (db-content-tag/read-contents-by-tag-id (config/db-spec) tag-id)))

(defn- update-story [txn publisher-id story-id tag-id]
(let [{:keys [published-json]} (db-content/find-by-id txn story-id)

    updated-tags (filter (fn [tag] (not= (:id tag) tag-id)) (:tags published-json))
    _ (db-story/update-published-json-without-timestamps txn publisher-id story-id (assoc published-json :tags updated-tags))]
(log/info {:message "[TAG-DELETION] Updated Story Tags in Published JSON"
           :publisher-id publisher-id
           :tag-id tag-id
           :story-id story-id
           :updated-tags-json updated-tags})))

(defn- delete-tag [publisher-id tag-id]
(let [associated-content-ids (find-story-ids-by-tag tag-id)]

(transaction/with-transaction
  [txn (config/db-spec)]
  (do
    (when (seq associated-content-ids)
      (do
        (doseq [story-id associated-content-ids]
          (update-story txn publisher-id story-id tag-id))
        (db-content-tag/delete-batch-by-tag txn tag-id associated-content-ids)
        (log/info {:message "[TAG-DELETION] Deleted from Content Tag"
                   :publisher-id publisher-id
                   :tag-id tag-id
                   :story-ids associated-content-ids})))
    (db-tag/delete txn publisher-id tag-id)
    (log/info {:message "[TAG-DELETION] Deleted Tag"
               :publisher-id publisher-id
               :tag-id tag-id})))))

(defn run [publisher-id tag-ids]
(comment run 123 [4 5 6])
(try

(do
  (log/info {:message "[TAG-DELETION] started"
             :publisher-id publisher-id
             :tag-ids tag-ids})
  (doseq [tag-id tag-ids] (if (db-tag/find-by-id (config/db-spec) publisher-id tag-id)
                            (delete-tag publisher-id tag-id)
                            (log/info {:message "[TAG-DELETION] Tag Not Found"
                                       :publisher-id publisher-id
                                       :tag-id tag-id})))
  (log/info {:message "[TAG-DELETION] completed"
             :publisher-id publisher-id
             :tag-ids tag-ids}))
(catch Exception e
  (log/exception e {:message "[TAG-DELETION] errored"
                    :publisher-id publisher-id
                    :tag-ids tag-ids}))))

If I have less than 100 tags this works, but say I have 100 000 tags it is very time consuming. How to modify this code which can run in parallel and takes less time? Currently the task uses seq for both deleting and updating the story

Please log in or register to answer this question.

...