Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make job.touch() promise-based #667

Merged
merged 1 commit into from
Jul 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -739,22 +739,21 @@ Disables the `job`. Upcoming runs won't execute.

Enables the `job` if it got disabled before. Upcoming runs will execute.

### touch(callback)
### touch()

Resets the lock on the job. Useful to indicate that the job hasn't timed out
when you have very long running jobs.
when you have very long running jobs. The call returns a promise that resolves
when the job's lock has been renewed.

```js
agenda.define('super long job', (job, done) => {
doSomeLongTask(() => {
job.touch(() => {
doAnotherLongTask(() => {
job.touch(() => {
finishOurLongTasks(done);
});
});
});
});
(async () => {
await doSomeLongTask();
await job.touch();
await doAnotherLongTask();
await job.touch();
await finishOurLongTasks();
})().then(done, done);
});
```

Expand Down
9 changes: 6 additions & 3 deletions lib/job/touch.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
'use strict';

const noCallback = require('../no-callback');

/**
* Updates "lockedAt" time so the job does not get picked up again
* @name Job#touch
* @function
* @param {Function} cb called when job "touch" fails or passes
* @returns {undefined}
*/
module.exports = function(cb) {
module.exports = async function() {
// eslint-disable-next-line prefer-rest-params
noCallback(arguments);
this.attrs.lockedAt = new Date();
this.save(cb);
return this.save();
};
14 changes: 4 additions & 10 deletions test/job.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,18 +349,12 @@ describe('Job', () => {
});

describe('touch', () => {
it('extends the lock lifetime', done => {
it('extends the lock lifetime', async() => {
const lockedAt = new Date();
const job = new Job({agenda, name: 'some job', lockedAt});
job.save = function(cb) {
cb();
};
setTimeout(() => {
job.touch(() => {
expect(job.attrs.lockedAt).to.be.greaterThan(lockedAt);
done();
});
}, 2);
await delay(2);
await job.touch();
expect(job.attrs.lockedAt).to.be.greaterThan(lockedAt);
});
});

Expand Down