Skip to content

Commit

Permalink
Merge pull request #130 from slr71/master
Browse files Browse the repository at this point in the history
#129: mark job status updates for non-existent job steps as propagated …
  • Loading branch information
slr71 authored Nov 1, 2018
2 parents 2cb2510 + f4c7c51 commit 223f630
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
10 changes: 9 additions & 1 deletion src/apps/persistence/jobs.clj
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,14 @@
(defn mark-job-status-updates-propagated
"Marks a set of job status updates as propagated."
[ids]
(when (seq ids)
(sql/update :job_status_updates
(set-fields {:propagated true})
(where {:id [in ids]}))))

(defn mark-job-status-updates-for-external-id-completed
"Marks all job status updates for an external ID as completed."
[external-id]
(sql/update :job_status_updates
(set-fields {:propagated true})
(where {:id [in ids]})))
(where {:external_id external-id})))
9 changes: 5 additions & 4 deletions src/apps/service/apps.clj
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,18 @@

(defn update-job-status
([external-id]
(jobs/validate-job-status-update-step-count external-id)
(transaction
(let [{job-id :job_id} (jobs/get-unique-job-step external-id)
updates (jp/get-job-status-updates external-id)]
(let [updates (jp/get-job-status-updates external-id)]
(when (seq updates)
(let [job-step (jobs/lock-job-step job-id external-id)
(let [job-id (:job_id (first (jp/get-job-steps-by-external-id external-id)))
job-step (jobs/lock-job-step job-id external-id)
job (jobs/lock-job job-id)
batch (when-let [parent-id (:parent_id job)] (jp/get-job-by-id parent-id))
apps-client (get-apps-client-for-username (:username job))]
(doseq [{status :status sent-on :sent_on} updates]
(let [end-date (when (jp/completed? status) (str sent-on))
job-step (jobs/get-unique-job-step external-id)]
job-step (first (jp/get-job-steps-by-external-id external-id))]
(when (jp/status-follows? status (:status job-step))
(jobs/update-job-status apps-client job-step job batch status end-date))))
(jp/mark-job-status-updates-propagated (mapv :id updates))
Expand Down
12 changes: 7 additions & 5 deletions src/apps/service/apps/jobs.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@
[apps.service.apps.jobs.util :as ju]
[apps.util.service :as service]))

(defn get-unique-job-step
"Gets a unique job step for an external ID. An exception is thrown if no job step
is found or if multiple job steps are found."
(defn validate-job-status-update-step-count
"Validates the number of steps for an external ID provided in a set of job status updates. If there are too many or
too few matching job steps then all of the job status updates will be marked as propagated and an exception will be
thrown. This function should not be called from within a transaction."
[external-id]
(let [job-steps (jp/get-job-steps-by-external-id external-id)]
(when (empty? job-steps)
(jp/mark-job-status-updates-for-external-id-completed external-id)
(service/not-found "job step" external-id))
(when (> (count job-steps) 1)
(service/not-unique "job step" external-id))
(first job-steps)))
(jp/mark-job-status-updates-for-external-id-completed external-id)
(service/not-unique "job step" external-id))))

(defn lock-job-step
[job-id external-id]
Expand Down

0 comments on commit 223f630

Please sign in to comment.