Permalink
Browse files

finish off get status and some cleanup

  • Loading branch information...
1 parent d74810c commit 284adeecd224038b84a5ccbf56591c12ff014db2 @smith smith committed May 3, 2011
Showing with 44 additions and 16 deletions.
  1. +4 −0 README.md
  2. +12 −9 lib/client.js
  3. +26 −4 test/test-client.js
  4. +2 −3 test/test-job.js
View
@@ -71,6 +71,10 @@ Submits a job to a manager and returns a `gearman.Job`. `data` defaults to a `Bu
If `background` is set to `true`, the job is detached after the `create` event and no further events are emitted.
+#### client.getJobStatus(handle, [callback])
+
+Works the same as `job.getStatus` but takes a job handle (assigned previously by the server for a job submitted with `background: true` and executes a callback taking an object with status information.
+
### gearman.Job
An object representing a job that has been submitted. `gearman.Job` instances are EventEmitters with the these events:
View
@@ -30,22 +30,14 @@ Client.prototype.getConnection = function () {
if (!conn) {
conn = net.createConnection(this.port, this.host);
- conn.on("connect", function () {
- client.connected = true;
- });
-
- conn.on("close", function (error) {
- client.connected = false;
- });
-
conn.on("data", function (data) {
// decode the data and execute the proper response handler
data = packet.decode(data);
var type = data.type;
debug("Recieved:", data);
if (type in responses) { responses[type](data, client); }
});
- } else if (!client.connected) { conn.connect(this.port, this.host); }
+ }
this.connection = conn;
return conn;
@@ -69,3 +61,14 @@ Client.prototype.submitJob = function (name, data, options) {
job.submit();
return job;
};
+
+// Get a job's status from its handle
+Client.prototype.getJobStatus = function (handle, callback) {
+ var job = this.jobs[handle];
+
+ // If we don't have the job, create it
+ if (!job) {
+ job = new Job({ client: this, handle: handle, background: true });
+ }
+ job.getStatus(callback);
+};
View
@@ -3,19 +3,25 @@ var gearman = require("gearman"),
Job = gearman.Job,
Socket = require("net").Socket,
testCase = require("nodeunit").testCase,
- client = gearman.createClient();
+ client;
+
+// gearman.debug = true;
+
+client = gearman.createClient();
module.exports = testCase({
"createClient": function (test) {
+ var otherClient;
+
test.ok(client instanceof Client,
"gearman.createClient() creates a client");
test.equal(client.port, 4730, "default port 4730");
test.equal(client.host, "localhost", "default host 'localhost'");
- client = gearman.createClient(1234, "example.com");
+ otherClient = gearman.createClient(1234, "example.com");
- test.equal(client.port, 1234, "port argument 1234");
- test.equal(client.host, "example.com", "host argument 'example.com'");
+ test.equal(otherClient.port, 1234, "port argument 1234");
+ test.equal(otherClient.host, "example.com", "host argument 'example.com'");
test.done();
},
@@ -36,5 +42,21 @@ module.exports = testCase({
test.ok(typeof client.end === "function",
"client.end() method exists");
test.done();
+ },
+
+ "getJobStatus": function (test) {
+ var job = client.submitJob("test", "test", { background: true });
+ test.ok(typeof client.getJobStatus === "function",
+ "client.getJobStatus is a function");
+
+ job.on("create", function (handle) {
+ client.getJobStatus(handle, function (status) {
+ test.deepEqual(status, { handle: handle,
+ known: true,
+ running: true,
+ percentComplete: [ 48, 48 ] });
+ test.done();
+ });
+ });
}
});
View
@@ -7,7 +7,7 @@ var gearman = require("gearman"),
// XXX: These need a real gearman server running on localhost:4730 and
// test/fixtures/worker.rb running. Need to make a mock server or something.
-gearman.debug = true;
+// gearman.debug = true;
client = gearman.createClient();
job = client.submitJob("test", "test", { encoding: "utf8" });
@@ -63,8 +63,7 @@ module.exports = testCase({
},
"submit { background: true }": function (test) {
- var job = client.submitJob("test", "test", { encoding: "utf8",
- background: true });
+ var job = client.submitJob("test", "test", { background: true });
job.on("create", function (handle) {
job.getStatus(function (status) {

0 comments on commit 284adee

Please sign in to comment.