-
Notifications
You must be signed in to change notification settings - Fork 5
Clean up Jobqueues, minor fixes for S3 Queue #451
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,9 +36,11 @@ export class JobQueueBase implements JobQueue { | |
startConsumer(onJob: OnJobCallback): void | ||
// eslint-disable-next-line @typescript-eslint/require-await | ||
async startConsumer(onJob: OnJobCallback): Promise<void> { | ||
this.started = true | ||
this.onJob = onJob | ||
await this.syncState() | ||
if (!this.started) { | ||
this.started = true | ||
await this.syncState() | ||
} | ||
} | ||
|
||
stopConsumer(): void | ||
|
@@ -62,8 +64,10 @@ export class JobQueueBase implements JobQueue { | |
resumeConsumer(): void | ||
// eslint-disable-next-line @typescript-eslint/require-await | ||
async resumeConsumer(): Promise<void> { | ||
this.paused = false | ||
await this.syncState() | ||
if (this.paused) { | ||
this.paused = false | ||
await this.syncState() | ||
} | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await | ||
|
@@ -77,8 +81,8 @@ export class JobQueueBase implements JobQueue { | |
clearTimeout(this.timeout) | ||
} | ||
// eslint-disable-next-line @typescript-eslint/await-thenable | ||
const hadSomething = await this.readState() | ||
this.timeout = setTimeout(() => this.syncState(), hadSomething ? 0 : this.intervalSeconds * 1000) | ||
await this.readState() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given existing semantics, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea here is that if you have 10k jobs to run, you shouldn't wait 5 sec between them (or between every 100 of them) :). So if we got a job with the last run, immediately see if there's another one. Otherwise wait a few seconds and then try again. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, that makes sense... assuming there's batching involved. I haven't seen it so far (am I missing something?) - so didn't assume such a state was possible. Will revert. |
||
this.timeout = setTimeout(() => this.syncState(), this.intervalSeconds * 1000) | ||
} else { | ||
if (this.timeout) { | ||
clearTimeout(this.timeout) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -151,6 +151,30 @@ describe('job queues', () => { | |
await waitForLogEntries(2) | ||
expect(testConsole.read()).toEqual([['processEvent'], ['reply', 'runIn']]) | ||
}) | ||
|
||
test.only('polls for jobs in future', async () => { | ||
neilkakkar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const DELAY = 10000 // 10s | ||
neilkakkar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// return something to be picked up after a few loops (poll interval is 100ms) | ||
const now = Date.now() | ||
|
||
const job: EnqueuedJob = { | ||
type: 'pluginJob', | ||
payload: { key: 'value' }, | ||
timestamp: now + DELAY, | ||
pluginConfigId: 2, | ||
pluginConfigTeam: 3, | ||
} | ||
|
||
server.hub.jobQueueManager.enqueue(job) | ||
const consumedJob: EnqueuedJob = await new Promise((resolve, reject) => { | ||
server.hub.jobQueueManager.startConsumer((consumedJob) => { | ||
resolve(consumedJob[0]) | ||
}) | ||
}) | ||
|
||
expect(consumedJob).toEqual(job) | ||
}) | ||
}) | ||
|
||
describe('connection', () => { | ||
|
@@ -284,5 +308,44 @@ describe('job queues', () => { | |
Key: `prefix/2020-01-01/20200101-123456.123Z-deadbeef.json.gz`, | ||
}) | ||
}) | ||
|
||
test('polls for new jobs', async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these tests would fail after 60s (jest timeout) if there's no polling. |
||
const DELAY = 10000 // 10s | ||
// calls the right functions to read the enqueued job | ||
mS3WrapperInstance.mockClear() | ||
|
||
// return something to be picked up after a few loops (poll interval is 5s) | ||
const now = Date.now() | ||
const date = new Date(now + DELAY).toISOString() | ||
const [day, time] = date.split('T') | ||
const dayTime = `${day.split('-').join('')}-${time.split(':').join('')}` | ||
|
||
const job: EnqueuedJob = { | ||
type: 'pluginJob', | ||
payload: { key: 'value' }, | ||
timestamp: now, | ||
pluginConfigId: 2, | ||
pluginConfigTeam: 3, | ||
} | ||
|
||
mS3WrapperInstance.listObjectsV2.mockReturnValue({ | ||
Contents: [{ Key: `prefix/${day}/${dayTime}-deadbeef.json.gz` }], | ||
}) | ||
mS3WrapperInstance.getObject.mockReturnValueOnce({ | ||
Body: gzipSync(Buffer.from(JSON.stringify(job), 'utf8')), | ||
}) | ||
|
||
const consumedJob: EnqueuedJob = await new Promise((resolve, reject) => { | ||
hub.jobQueueManager.startConsumer((consumedJob) => { | ||
resolve(consumedJob[0]) | ||
}) | ||
}) | ||
expect(consumedJob).toEqual(job) | ||
await delay(10) | ||
expect(mS3WrapperInstance.deleteObject).toBeCalledWith({ | ||
Bucket: 'bucket-name', | ||
Key: `prefix/${day}/${dayTime}-deadbeef.json.gz`, | ||
}) | ||
}) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resume is called on
drain
events emitted by Piscina, which results in multiple calls to sync state. This is okay, as long as dequeue code is idempotent (which it isn't, thus leading to race conditions).We didn't face this with graphile because the runner object is unchanged over multiple sync state calls.