|
| 1 | +/** |
| 2 | + * `wheels jobs work` runs each job with its own class's timeout (4517). The CLI worker polls the |
| 3 | + * jobsProcessNext bridge command, which used to default to timeout=300 and so cut every job off |
| 4 | + * at 300 seconds whatever its this.timeout. It now claims and runs each job with its own |
| 5 | + * timeout, like processQueue(), unless the CLI passes --timeout as a cap. The spec reads the |
| 6 | + * timeout each claim recorded (claimTimeout) rather than waiting 300+ seconds. |
| 7 | + */ |
| 8 | +component extends="wheels.WheelsTest" { |
| 9 | + |
| 10 | + function run() { |
| 11 | + |
| 12 | + describe("jobsProcessNext (wheels jobs work) timeouts", function() { |
| 13 | + |
| 14 | + beforeEach(function() { |
| 15 | + var bootstrapJob = new wheels.Job(); |
| 16 | + bootstrapJob.$ensureJobTable(); |
| 17 | + $cleanup(); |
| 18 | + }); |
| 19 | + |
| 20 | + afterEach(function() { |
| 21 | + $cleanup(); |
| 22 | + }); |
| 23 | + |
| 24 | + it("claims a job with its own class's timeout when no cap is given", function() { |
| 25 | + var id = $insertJob(queue = "test_cpn_own", jobClass = "wheels.tests._assets.jobs.LongTimeoutJob"); |
| 26 | + var rv = $processNext({queues = "test_cpn_own"}); |
| 27 | + expect(rv.success).toBeTrue(rv.message ?: ""); |
| 28 | + expect(Val($claimTimeout(id))).toBe(900, "not the bridge's old 300-second default"); |
| 29 | + }); |
| 30 | + |
| 31 | + it("caps the timeout when --timeout is passed", function() { |
| 32 | + var id = $insertJob(queue = "test_cpn_cap", jobClass = "wheels.tests._assets.jobs.LongTimeoutJob"); |
| 33 | + $processNext({queues = "test_cpn_cap", timeout = 120}); |
| 34 | + expect(Val($claimTimeout(id))).toBe(120); |
| 35 | + }); |
| 36 | + |
| 37 | + it("keeps a default-timeout job at 300 seconds", function() { |
| 38 | + var id = $insertJob(queue = "test_cpn_default"); |
| 39 | + $processNext({queues = "test_cpn_default"}); |
| 40 | + expect(Val($claimTimeout(id))).toBe(300); |
| 41 | + }); |
| 42 | + |
| 43 | + }); |
| 44 | + |
| 45 | + } |
| 46 | + |
| 47 | + private struct function $processNext(required struct params) { |
| 48 | + var bridge = new wheels.public.CliBridge(); |
| 49 | + return bridge.dispatch(command = "jobsProcessNext", context = {}, params = arguments.params); |
| 50 | + } |
| 51 | + |
| 52 | + private void function $cleanup() { |
| 53 | + try { |
| 54 | + queryExecute("DELETE FROM wheels_jobs WHERE queue LIKE 'test_cpn_%'", {}, {datasource = application.wheels.dataSourceName}); |
| 55 | + } catch (any e) { |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private string function $insertJob( |
| 60 | + required string queue, |
| 61 | + string jobClass = "wheels.tests._assets.jobs.ProcessOrdersJob" |
| 62 | + ) { |
| 63 | + var id = CreateUUID(); |
| 64 | + var stamp = DateAdd("s", -5, Now()); |
| 65 | + queryExecute( |
| 66 | + "INSERT INTO wheels_jobs (id, jobClass, queue, data, priority, status, attempts, maxRetries, runAt, createdAt, updatedAt) |
| 67 | + VALUES (:id, :jobClass, :queue, '{}', 0, 'pending', 0, 3, :runAt, :createdAt, :updatedAt)", |
| 68 | + { |
| 69 | + id = {value = id, cfsqltype = "cf_sql_varchar"}, |
| 70 | + jobClass = {value = arguments.jobClass, cfsqltype = "cf_sql_varchar"}, |
| 71 | + queue = {value = arguments.queue, cfsqltype = "cf_sql_varchar"}, |
| 72 | + runAt = {value = stamp, cfsqltype = "cf_sql_timestamp"}, |
| 73 | + createdAt = {value = stamp, cfsqltype = "cf_sql_timestamp"}, |
| 74 | + updatedAt = {value = stamp, cfsqltype = "cf_sql_timestamp"} |
| 75 | + }, |
| 76 | + {datasource = application.wheels.dataSourceName} |
| 77 | + ); |
| 78 | + return id; |
| 79 | + } |
| 80 | + |
| 81 | + private string function $claimTimeout(required string id) { |
| 82 | + var q = queryExecute( |
| 83 | + "SELECT claimTimeout FROM wheels_jobs WHERE id = :id", |
| 84 | + {id = {value = arguments.id, cfsqltype = "cf_sql_varchar"}}, |
| 85 | + {datasource = application.wheels.dataSourceName} |
| 86 | + ); |
| 87 | + return q.recordCount && !IsNull(q.claimTimeout[1]) ? q.claimTimeout[1] : ""; |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments