Skip to content

Commit 9236bf4

Browse files
authored
Merge pull request #4 from wildbit/utc-fix
Don’t rely on UTC for determining pending jobs
2 parents 2d21db7 + c74722b commit 9236bf4

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
# Change Log
2-
All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/).
2+
All notable changes to this project are documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/).
3+
4+
## [Unreleased]
5+
6+
### Fixed
7+
- Internal: Fixed possible test failures in non-UTC timezones.
8+
- Internal: Fixed graceful shutdown test.
9+
10+
### Changed
11+
- Use JVM time when querying for pending jobs instead of relying on UTC.
312

src/mysql_queue/queries.clj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@
7070
scheduled_jobs LEFT JOIN
7171
jobs ON scheduled_jobs.id=jobs.scheduled_job_id
7272
WHERE
73-
scheduled_jobs.scheduled_for <= UTC_TIMESTAMP() AND
73+
scheduled_jobs.scheduled_for <= ? AND
7474
jobs.id IS NULL AND
7575
scheduled_jobs.name IN (" (in-query-stubs jobs-names) ") AND
7676
scheduled_jobs.id NOT IN (" (in-query-stubs sieved-ids) ")
7777
LIMIT ?")]
78-
(concat jobs-names sieved-ids [n]))))
78+
(concat [(java.util.Date.)] jobs-names sieved-ids [n]))))
7979

8080
(defn select-jobs-by-ids
8181
[db ids]
@@ -95,12 +95,12 @@
9595
jobs.status NOT IN (" (in-query-stubs ultimate-statuses) ") AND
9696
jobs.name IN (" (in-query-stubs job-names) ") AND
9797
jobs.id NOT IN (" (in-query-stubs sieved-ids) ") AND
98-
jobs.created_at + INTERVAL ? MINUTE <= UTC_TIMESTAMP()
98+
jobs.created_at + INTERVAL ? MINUTE <= ?
9999
LIMIT ?")]
100100
ultimate-statuses
101101
job-names
102102
sieved-ids
103-
[threshold-mins n])))
103+
[threshold-mins (java.util.Date.) n])))
104104

105105
(defn delete-scheduled-job-by-id!
106106
[db id]

test/mysql_queue/core_test.clj

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,26 +160,28 @@
160160
expected-set (->> num-jobs range (map inc) (into #{}))
161161
success? (promise)
162162
exception (promise)
163+
lock (promise)
163164
check-ins (check-in-atom expected-set success?)
164165
jobs {:test-foo (fn [status {id :id :as args}]
166+
@lock
165167
(Thread/sleep 1500)
166168
(swap! check-ins conj id)
167169
[:done args])}
168170
_ (dotimes [n num-jobs]
169-
(let [scheduled-id (schedule-job db-conn :test-foo :begin {:id (inc n)} (java.util.Date.))]
170-
(queries/insert-job<! db-conn scheduled-id 0 "test-foo" "begin" (pr-str {:id (inc n)}) 1)))]
171+
(schedule-job db-conn :test-foo :begin {:id (inc n)} (java.util.Date.)))]
171172
(with-worker [wrk (worker db-conn
172173
jobs
173174
:num-consumer-threads 2
174175
:err-fn #(deliver exception %)
175176
:recovery-threshold-mins 0
176177
:max-scheduler-sleep-interval 0.5
177178
:max-recovery-sleep-interval 0.5)]
178-
(Thread/sleep 500))
179-
(is (deref success? 10 false)
180-
(str "Failed to finish " num-jobs " test jobs taking 1500ms with 2s quit timeout.\n"
181-
"Missing job IDs: " (clj-set/difference expected-set @check-ins) "\n"
182-
"Exception?: " (deref exception 0 "nope")))
183-
(is (= num-jobs (count @check-ins))
184-
"The number of executed jobs doesn't match the number of jobs queued.")))
179+
(deliver lock :unlocked)
180+
(Thread/sleep 1000))
181+
(is (deref success? 10 false)
182+
(str "Failed to finish " num-jobs " test jobs taking 1500ms with 2s quit timeout.\n"
183+
"Missing job IDs: " (clj-set/difference expected-set @check-ins) "\n"
184+
"Exception?: " (deref exception 0 "nope")))
185+
(is (= num-jobs (count @check-ins))
186+
"The number of executed jobs doesn't match the number of jobs queued.")))
185187

0 commit comments

Comments
 (0)