Skip to content

Commit

Permalink
basic support for HTTP API, far from finish
Browse files Browse the repository at this point in the history
  • Loading branch information
goenning committed Sep 27, 2016
1 parent 3fd8775 commit 9882974
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,28 @@
"start-redis": "docker run -d -p 6060:6379 redis"
},
"dependencies": {
"body-parser": "^1.15.2",
"cron": "^1.1.0",
"express": "^4.14.0",
"node-uuid": "^1.4.7",
"redis": "^2.6.2"
},
"devDependencies": {
"@types/body-parser": "0.0.33",
"@types/chai": "^3.4.33",
"@types/cron": "^1.0.30",
"@types/express": "^4.0.33",
"@types/mocha": "^2.2.32",
"@types/node": "^6.0.41",
"@types/node-uuid": "0.0.28",
"@types/redis": "^0.12.32",
"@types/request": "0.0.31",
"@types/sinon": "^1.16.31",
"chai": "^3.5.0",
"coveralls": "^2.11.14",
"mocha": "^3.0.2",
"nyc": "^8.3.0",
"request": "^2.75.0",
"sinon": "^1.17.6",
"ts-node": "^1.3.0",
"tslint": "^3.15.1",
Expand Down
17 changes: 17 additions & 0 deletions src/yago.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { EventEmitter } from "events";
import { CronJob } from "cron";
import { Logger } from "./logger";
import { StandardLogger } from "./logger/standard_logger";
import * as express from "express";
import { Server } from "http";
import * as bodyParser from "body-parser";

export interface YagoOptions {
queue?: TaskQueue;
Expand All @@ -19,6 +22,8 @@ export class Yago extends EventEmitter {
public readonly runners: { [key: string]: ITaskRunnerClass };
public readonly interval: number;

private app: express.Express;
private server: Server;
private timer: NodeJS.Timer;

constructor(options?: YagoOptions) {
Expand Down Expand Up @@ -49,12 +54,24 @@ export class Yago extends EventEmitter {
}

start(): void {
this.app = express();
this.app.use(bodyParser.json());

this.app.post("/api/enqueue", (req, res) => {
this.enqueue(req.body.name, req.body.options);
res.send();
});

this.server = this.app.listen(8888);

this.timer = setInterval(() => {
this._processQueue();
}, this.interval);
}

stop(): void {
if (this.server)
this.server.close();
clearInterval(this.timer);
}

Expand Down
14 changes: 14 additions & 0 deletions test/yago.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { DEFAULT_RETRY_COUNT, ExecutionResult, ExecutionResultOutcome } from "..
import { HelloWorldTaskRunner } from "./dummy/helloworld_task_runner";
import { ThrowErrorTaskRunner } from "./dummy/throwerror_task_runner";
import { NoNameTaskRunner } from "./dummy/noname_task_runner";
import * as request from "request";

describe("Yago", () => {
let yago: Yago;
Expand Down Expand Up @@ -115,4 +116,17 @@ describe("Yago", () => {
it("should throw error when registering unnamed TaskRunners", () => {
expect(yago.register.bind(yago, NoNameTaskRunner)).to.throw(Error, "NoNameTaskRunner does not have a RunTask decoration.");
});

it.only("should queue task when using HTTP API", (done) => {
yago.start();
yago.on("enqueue", (task: Task) => {
expect(task.name).to.be.eq("hello-world-via-api");
done();
});

const data = {
name: "hello-world-via-api"
};
request.post("http://localhost:8888/api/enqueue", { json: data });
});
});

0 comments on commit 9882974

Please sign in to comment.