Skip to content

Commit

Permalink
Merge 14808a9 into f0d4d2c
Browse files Browse the repository at this point in the history
  • Loading branch information
nwkeeley committed Oct 1, 2014
2 parents f0d4d2c + 14808a9 commit ec0f4f5
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -480,6 +480,15 @@ job.priority('low');
job.save();
```

### unique(properties)

Ensure that only one instance of this job exists with the specified properties

```js
job.unique('data.type': 'active', 'data.userId': '123', nextRunAt(date)});
job.save();
```

### fail(reason)

Sets `job.attrs.failedAt` to `now`, and sets `job.attrs.failReason`
Expand Down
6 changes: 6 additions & 0 deletions lib/agenda.js
Expand Up @@ -173,8 +173,10 @@ Agenda.prototype.saveJob = function(job, cb) {

var props = job.toJSON();
var id = job.attrs._id;
var unique = job.attrs.unique;

delete props._id;
delete props.unique;

props.lastModifiedBy = this._name;

Expand All @@ -196,6 +198,10 @@ Agenda.prototype.saveJob = function(job, cb) {
}
// Try an upsert.
this._db.findAndModify({name: props.name, type: 'single'}, {}, update, {upsert: true, new: true}, processDbResult);
} else if(unique) {
var query = job.attrs.unique;
query.name = props.name;
this._db.findAndModify(query, {}, update, {upsert: true, new: true}, processDbResult);
} else {
this._db.insert(props, processDbResult);
}
Expand Down
8 changes: 6 additions & 2 deletions lib/job.js
Expand Up @@ -107,6 +107,11 @@ Job.prototype.repeatAt = function(time) {
return this;
};

Job.prototype.unique = function(unique) {
this.attrs.unique = unique;
return this;
};

Job.prototype.schedule = function(time) {
this._scheduled = true;
this.attrs.nextRunAt = (time instanceof Date) ? time : date(time);
Expand Down Expand Up @@ -173,8 +178,7 @@ Job.prototype.run = function(cb) {
});
};

Job.prototype.isRunning = function()
{
Job.prototype.isRunning = function() {
if (!this.attrs.lastRunAt) return false;
if (!this.attrs.lastFinishedAt) return true;
if (this.attrs.lockedAt && this.attrs.lastRunAt.getTime() > this.attrs.lastFinishedAt.getTime()) return true;
Expand Down
53 changes: 53 additions & 0 deletions test/agenda.js
Expand Up @@ -117,6 +117,7 @@ describe('Agenda', function() {
});

describe('job methods', function() {

describe('create', function() {
var job;
beforeEach(function() {
Expand Down Expand Up @@ -212,6 +213,47 @@ describe('Agenda', function() {
after(clearJobs);
});

describe('unique', function() {

describe('should demonstrate unique contraint', function(done) {

it('should create one job when unique matches', function(done) {
var time = new Date();
jobs.create('unique job', {type: 'active', userId: '123', 'other': true}).unique({'data.type': 'active', 'data.userId': '123', nextRunAt: time}).schedule(time).save(function(err, job) {
jobs.create('unique job', {type: 'active', userId: '123', 'other': false}).unique({'data.type': 'active', 'data.userId': '123', nextRunAt: time}).schedule(time).save(function(err, job) {
mongo.collection('agendaJobs').find({name: 'unique job'}).toArray(function(err, j) {
expect(j).to.have.length(1);
done();
});
});
});
});
after(clearJobs);

});

describe('should demonstrate non-unique contraint', function(done) {

it('should create two jobs when unique doesn\t match', function(done) {
var time = new Date(Date.now() + 1000*60*3);
var time2 = new Date(Date.now() + 1000*60*4);

jobs.create('unique job', {type: 'active', userId: '123', 'other': true}).unique({'data.type': 'active', 'data.userId': '123', nextRunAt: time}).schedule(time).save(function(err, job) {
jobs.create('unique job', {type: 'active', userId: '123', 'other': false}).unique({'data.type': 'active', 'data.userId': '123', nextRunAt: time2}).schedule(time).save(function(err, job) {
mongo.collection('agendaJobs').find({name: 'unique job'}).toArray(function(err, j) {
expect(j).to.have.length(2);
done();
});
});
});

});
after(clearJobs);

});

});

describe('now', function() {
it('returns a job', function() {
expect(jobs.now('send email')).to.be.a(Job);
Expand Down Expand Up @@ -361,6 +403,17 @@ describe('Job', function() {
expect(job.repeatAt('3:30pm')).to.be(job);
});
});

describe('unique', function() {
var job = new Job();
it('sets the unique property', function() {
job.unique({'data.type': 'active', 'data.userId': '123'});
expect(JSON.stringify(job.attrs.unique)).to.be(JSON.stringify({'data.type': 'active', 'data.userId': '123'}));
});
it('returns the job', function() {
expect(job.unique({'data.type': 'active', 'data.userId': '123'})).to.be(job);
});
});

describe('repeatEvery', function() {
var job = new Job();
Expand Down

0 comments on commit ec0f4f5

Please sign in to comment.