Skip to content
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ and `options` are additional options as follows:
* **encoding** - {string} encoding if string data used, **DEPRECATED**: ignored, will be removed in next release, use Buffer with corresponding string encoding as payload
* **unique** {string} unique identifiter for the job
* **toStringEncoding** {string} if given received response will be converted to `String` with this encoding, otherwise payload turned over as `Buffer`
* **epochTime** {int} if given the job will not run before the given epoch time (will always be a background job!)

```javascript
// by default foreground job with normal priority
Expand Down
7 changes: 6 additions & 1 deletion lib/gearmanode/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,12 @@ Client.prototype.submitJob = function(name, payload, options) {
if (job instanceof Error) { return job; }
job.processing = true;

packet = protocol.encodePacket(job.getPacketType(), [job.name, (job.unique ? job.unique : ''), job.payload]);
var packetType = job.getPacketType();
if (packetType === protocol.PACKET_TYPES.SUBMIT_JOB_EPOCH) {
packet = protocol.encodePacket(packetType, [job.name, (job.unique ? job.unique : ''), job.epochTime, job.payload]);
} else {
packet = protocol.encodePacket(packetType, [job.name, (job.unique ? job.unique : ''), job.payload]);
}

var jsSendCallback = function(err) {
if (err instanceof Error) {
Expand Down
11 changes: 9 additions & 2 deletions lib/gearmanode/job.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var Job = exports.Job = function (clientOrWorker, options) {

// VALIDATION - common
pattern = {
name: 'mandatory', payload: 'mandatory', unique: 'optional'
name: 'mandatory', payload: 'mandatory', unique: 'optional', epochTime: 'optional'
};
returned = common.verifyOptions({ name: options.name, payload: options.payload }, pattern);
if (returned instanceof Error) { return returned; }
Expand Down Expand Up @@ -71,7 +71,12 @@ var Job = exports.Job = function (clientOrWorker, options) {
this.toStringEncoding = options.toStringEncoding;
}

if (options.epochTime && typeof options.epochTime !== 'number') {
return new Error('invalid epochTime: ' + options.epochTime);
options.background = true;
}
this.background = options.background;
this.epochTime = options.epochTime;
this.priority = options.priority;
if (options.hasOwnProperty('unique')) {
this.unique = options.unique;
Expand Down Expand Up @@ -127,7 +132,9 @@ Job.prototype.close = function () {
* Gets packet type for submiting a job. according to job's options.
*/
Job.prototype.getPacketType = function () {
if (this.background) {
if (this.epochTime) {
return protocol.PACKET_TYPES.SUBMIT_JOB_EPOCH;
} else if (this.background) {
if (this.priority == 'LOW') { return protocol.PACKET_TYPES.SUBMIT_JOB_LOW_BG; }
if (this.priority == 'NORMAL') { return protocol.PACKET_TYPES.SUBMIT_JOB_BG; }
if (this.priority == 'HIGH') { return protocol.PACKET_TYPES.SUBMIT_JOB_HIGH_BG; }
Expand Down
1 change: 1 addition & 0 deletions lib/gearmanode/protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ exports.DEFINITION = {
SUBMIT_JOB_HIGH_BG: [32, constants.TYPE_REQ], // C->J: FUNC[0]UNIQ[0]ARGS
SUBMIT_JOB_LOW: [33, constants.TYPE_REQ], // C->J: FUNC[0]UNIQ[0]ARGS
SUBMIT_JOB_LOW_BG: [34, constants.TYPE_REQ], // C->J: FUNC[0]UNIQ[0]ARGS
SUBMIT_JOB_EPOCH: [36, constants.TYPE_REQ], // C->J: FUNC[0]UNIQ[0]ARGS
};


Expand Down
6 changes: 5 additions & 1 deletion test/test-job.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ describe('Job', function() {
j.background.should.be.false;
j.priority.should.equal('NORMAL');
j.encoding.should.equal('utf8');
should.not.exist(j.epochTime);
should.not.exist(j.unique);
should.exist(j.uuid);
should.not.exist(j.jobServer);
should.not.exist(j.toStringEncoding);
})
it('should store additional options', function() {
var job = new Job(c,
{ name: 'reverse', payload: 'hi', background: true, priority: 'HIGH', toStringEncoding: 'ascii', encoding: 'ascii', unique: 'foo' }
{ name: 'reverse', payload: 'hi', background: true, priority: 'HIGH', toStringEncoding: 'ascii', encoding: 'ascii', unique: 'foo', epochTime: 1234 }
);
job.should.be.an.instanceof(Job);
job.name.should.equal('reverse');
Expand All @@ -40,6 +41,7 @@ describe('Job', function() {
job.encoding.should.equal('ascii');
job.toStringEncoding.should.equal('ascii');
job.unique.should.equal('foo');
job.epochTime.should.equal(1234);
})
it('should return error when missing mandatory options', function() {
var job = new Job();
Expand All @@ -66,6 +68,8 @@ describe('Job', function() {
job.should.be.an.instanceof(Error);
job = new Job(c, { name: 'foo', payload: 'bar', toStringEncoding: 'baz' });
job.should.be.an.instanceof(Error);
job = new Job(c, { name: 'foo', payload: 'bar', epochTime: '1234' });
job.should.be.an.instanceof(Error);
})
})

Expand Down