Skip to content

Commit

Permalink
Merge branch 'release/0.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
smith committed Apr 15, 2011
2 parents 6022da9 + bf1a887 commit 7258b64
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 24 deletions.
9 changes: 9 additions & 0 deletions HISTORY.md
@@ -0,0 +1,9 @@
# History

## 0.2.0 / 2011-04-15

* Foreground job priority support

## 0.1.0 / 2011-04-12

* Initial Release
5 changes: 4 additions & 1 deletion README.md
Expand Up @@ -62,9 +62,12 @@ Submits a job to a manager and returns a `gearman.Job`. `data` defaults to a `Bu

`options` is an object with the following defaults:

{ encoding: null
{ priority: 'normal',
encoding: null
}

`priority` can be one of `'low'`, `'normal'`, or `'high'`.

### gearman.Job

An object representing a job that has been submitted. `gearman.Job` instances are EventEmitters with the these events:
Expand Down
7 changes: 6 additions & 1 deletion lib/job.js
Expand Up @@ -4,26 +4,31 @@ var gearman = require("gearman"),
extend = util.extend,
debug = util.debug,
packet = require("./packet"),
priorities = require("./packet/priorities").names,
EventEmitter = require("events").EventEmitter,
Job;

Job = function (options) {
if (!(this instanceof Job)) { return new Job(options); }
extend(this, options);
this.client = this.client || gearman.createClient();
this.priority = this.priority || "normal";
};
inherits(Job, EventEmitter);
exports.Job = Job;

Job.prototype.submit = function () {
var client = this.client,
data = {
type: "SUBMIT_JOB",
name: this.name,
data: this.data,
encoding: this.encoding
};

// Set the type given the priority
if (!(this.priority in priorities)) { throw Error("invalid priority"); }
data.type = priorities[this.priority];

client.getConnection().write(packet.encode(data), this.encoding);
debug("Sent:", data);
client.lastJobSubmitted = this;
Expand Down
14 changes: 14 additions & 0 deletions lib/packet/priorities.js
@@ -0,0 +1,14 @@
// Maps priority names to request types
exports.names = {
low: "SUBMIT_JOB_LOW",
normal: "SUBMIT_JOB",
high: "SUBMIT_JOB_HIGH"
};

exports.types = {};

for (var n in exports.names) {
if (Object.prototype.hasOwnProperty.call(exports.names, n)) {
exports.types[exports.names[n]] = n;
}
}
46 changes: 25 additions & 21 deletions lib/packet/requests.js
Expand Up @@ -2,27 +2,31 @@

var binary = require("binary");

module.exports = {
SUBMIT_JOB: function (options) {
options = options || {};
var data = options.data || 0,
name = options.name,
encoding = options.encoding;

if (typeof name !== "string") {
throw Error("function name must be a string");
}
if (data !== 0 && typeof encoding === "string") {
data = new Buffer(data, encoding);
} else {
data = new Buffer(data);
}
function submitJob(options) {
options = options || {};
var data = options.data || 0,
name = options.name,
encoding = options.encoding;

return binary.put().
put(new Buffer(name, "ascii")).
word8(0).
word8(0). // TODO: unique client ids
put(data).
buffer();
if (typeof name !== "string") {
throw Error("function name must be a string");
}
if (data !== 0 && typeof encoding === "string") {
data = new Buffer(data, encoding);
} else {
data = new Buffer(data);
}

return binary.put().
put(new Buffer(name, "ascii")).
word8(0).
word8(0). // TODO: unique client ids
put(data).
buffer();
}

module.exports = {
SUBMIT_JOB: submitJob,
SUBMIT_JOB_HIGH: submitJob,
SUBMIT_JOB_LOW: submitJob
};
2 changes: 2 additions & 0 deletions lib/packet/types.js
@@ -1,5 +1,7 @@
exports.names = {
SUBMIT_JOB: 7,
SUBMIT_JOB_HIGH: 21,
SUBMIT_JOB_LOW: 33,
JOB_CREATED: 8,
WORK_COMPLETE: 13,
WORK_FAIL: 14,
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "gearman",
"version": "0.1.0",
"version": "0.2.0",
"description": "Client library for Gearman",
"keywords": ["gearman", "job", "worker", "background"],
"homepage": "https://github.com/cramerdev/gearman-node",
Expand Down
33 changes: 33 additions & 0 deletions test/test-job.js
Expand Up @@ -12,6 +12,7 @@ module.exports = testCase({
"Job": function (test) {
test.ok(job instanceof EventEmitter,
"Job instances are EventEmitters");
test.equal("normal", job.priority, "default priority is normal");
test.done();
},

Expand All @@ -25,6 +26,38 @@ module.exports = testCase({
});
},

"submit { priority: 'high' }": function (test) {
var job = client.submitJob("test", "test", { encoding: "utf8",
priority: "high" });
job.on("create", function (handle) {
test.ok(typeof handle === "string",
"handle returned on create event");
test.equal(job.handle, handle,
"job handle assigned on create event");
test.done();
});
},

"submit { priority: 'low' }": function (test) {
var job = client.submitJob("test", "test", { encoding: "utf8",
priority: "low" });
job.on("create", function (handle) {
test.ok(typeof handle === "string",
"handle returned on create event");
test.equal(job.handle, handle,
"job handle assigned on create event");
test.done();
});
},

"submit { priority: 'invalid' }": function (test) {
test.throws(function () {
var job = client.submitJob("test", "test", { encoding: "utf8",
priority: "other" });
}, "must have a known priority");
test.done();
},

"event: data": function (test) {
job.on("data", function (result) {
test.equal("test", result, "work data received");
Expand Down
16 changes: 16 additions & 0 deletions test/test-packet.js
Expand Up @@ -28,6 +28,22 @@ exports["encode SUBMIT_JOB"] = function (test) {
test.done();
};

exports["encode SUBMIT_JOB_HIGH"] = function (test) {
// \0REQ-SUBMIT_JOB_HIGH-6-test\0-\0-a/
var data = new Buffer([0,0x52,0x45,0x51,0,0,0,21,0,0,0,7,0x74,0x65,0x73,0x74,0,0,0x61]);

test.deepEqual(packet.encode({ type: "SUBMIT_JOB_HIGH", name: "test", data: "a", encoding: "utf8" }), data);
test.done();
};

exports["encode SUBMIT_JOB_LOW"] = function (test) {
// \0REQ-SUBMIT_JOB_LOW-6-test\0-\0-a/
var data = new Buffer([0,0x52,0x45,0x51,0,0,0,33,0,0,0,7,0x74,0x65,0x73,0x74,0,0,0x61]);

test.deepEqual(packet.encode({ type: "SUBMIT_JOB_LOW", name: "test", data: "a", encoding: "utf8" }), data);
test.done();
};

exports["decode"] = function (test) {
// \0RES
var headerOnly = new Buffer([0, 0x52, 0x45, 0x53]),
Expand Down

0 comments on commit 7258b64

Please sign in to comment.