Skip to content
This repository was archived by the owner on Aug 6, 2025. It is now read-only.

Commit 9ac4f66

Browse files
authored
Dop 2258 (#455)
* Fix sigterm pass-through to worker instances * Ensure container pruning in integration * Re-enqueue jobs on graceful shutdown * Add re-enqueue tests, fix gracefulShutdown tests * Fix typo in mongo.test.js for new reenqueue tests
1 parent 1e53697 commit 9ac4f66

File tree

6 files changed

+53
-6
lines changed

6 files changed

+53
-6
lines changed

.github/workflows/deploy-integration-ec2.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ jobs:
2727
sudo docker stop $(sudo docker ps -a -q)
2828
sudo sh rundocker.sh
2929
sudo sh rundocker.sh
30+
sudo docker container prune -f
3031
'
3132

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,4 @@ RUN mkdir repos && chmod 755 repos
7272

7373
# entry to kick-off the worker
7474
EXPOSE 3000
75-
CMD ["npm", "start"]
75+
CMD ["node", "index.js"]

worker/tests/unit/mongo.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,32 @@ describe('Mongo Tests', () => {
263263
await mongo.logMessageInMongo(job2, 'message 1');
264264
}, 5000);
265265

266+
/** ******************************************************************
267+
* resetJobForReenqueue() *
268+
******************************************************************* */
269+
it('resetJobForReenqueue(queueCollection, job) works properly', async () => {
270+
const jobsColl = db.collection('jobs');
271+
272+
await mongo.resetJobForReenqueue(jobsColl, job2);
273+
const currJob = await jobsColl.findOne({ _id: job2._id });
274+
expect(currJob).toBeTruthy();
275+
expect(currJob.status).toEqual('inQueue');
276+
expect(currJob.startTime).toBeNull();
277+
expect(currJob.logs[0]).toEqual('Job restarted due to server shutdown.');
278+
}, 5000);
279+
280+
it('resetJobForReenqueue() fails on incorrect jobId', async () => {
281+
const jobsColl = db.collection('jobs');
282+
expect.assertions(1);
283+
await expect(
284+
mongo.resetJobForReenqueue(
285+
jobsColl,
286+
{ _id: 'notRealId', numFailures: 0 },
287+
'a'
288+
)
289+
).rejects.toBeTruthy();
290+
}, 5000);
291+
266292
/** ******************************************************************
267293
* reportStatus() *
268294
******************************************************************* */

worker/tests/unit/worker.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ describe('Test Class', () => {
3636
it('onSignal()', async () => {
3737
worker.setCurrentJob({ job: 'doesnt matter' });
3838
workerUtils.promiseTimeoutS = jest.fn().mockResolvedValue();
39-
mongo.finishJobWithFailure = jest.fn().mockResolvedValue();
39+
mongo.resetJobForReenqueue = jest.fn().mockResolvedValue();
4040

4141
await expect(worker.gracefulShutdown()).resolves.toBeUndefined();
42-
expect(mongo.finishJobWithFailure).toHaveBeenCalledTimes(1);
42+
expect(mongo.resetJobForReenqueue).toHaveBeenCalledTimes(1);
4343

4444
worker.setCurrentJob(null);
4545
});

worker/utils/mongo.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,26 @@ module.exports = {
130130
}
131131
},
132132

133+
// Updates the status to be inQueue
134+
async resetJobForReenqueue(queueCollection, job) {
135+
const query = { _id: job._id };
136+
const reenqueueMessage = 'Job restarted due to server shutdown.';
137+
138+
const update = {
139+
$set: {
140+
startTime: null,
141+
status: 'inQueue',
142+
error: {},
143+
logs: [reenqueueMessage],
144+
}
145+
};
146+
147+
const updateResult = await queueCollection.updateOne(query, update);
148+
if (updateResult.result.n < 1) {
149+
throw new Error(`Failed to update job (${job._id}) in queue during re-enqueue operation`);
150+
}
151+
},
152+
133153
async updateJobWithPurgedURLs(currentJob, urlArray) {
134154
const queueCollection = module.exports.getCollection();
135155
if (queueCollection) {

worker/worker.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,17 @@ module.exports = {
9696
workerUtils.resetDirectory('work/');
9797
await workerUtils.promiseTimeoutS(
9898
MONGO_TIMEOUT_S,
99-
await mongo.finishJobWithFailure(
99+
await mongo.resetJobForReenqueue(
100100
queueCollection,
101101
currentJob,
102-
'Server is being shutdown'
103102
),
104-
`Mongo Timeout Error: Timed out finishing failed job with jobId: ${currentJob._id}`
103+
`Mongo Timeout Error: Timed out finishing re-enqueueing job with jobId: ${currentJob._id}`
105104
);
106105
}
107106
if (mongoClient) {
108107
monitorInstance.reportStatus('closed connection');
109108
mongoClient.close();
109+
console.log('\nServer has closed mongo client connection');
110110
}
111111
},
112112

0 commit comments

Comments
 (0)