Skip to content

Commit

Permalink
Merge pull request #8 from hyper63/twilson63/update-refactor-api-to-7
Browse files Browse the repository at this point in the history
update: refactor api (#7)
  • Loading branch information
twilson63 authored Aug 4, 2021
2 parents 2408f51 + be2fd35 commit 5bccf75
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 45 deletions.
43 changes: 22 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,21 @@ local hyper services or services with small workloads.</p>

## Getting Started

In order to get started using `hyper-adapter-queue`, you need to setup a hyper instance:
In order to get started using `hyper-adapter-queue`, you need to setup a hyper
instance:

mod.js file:

``` js
import { core } from 'https://x.nest.land/hyper@1.4.2/mod.js'
import config from './hyper.config.js'
```js
import { core } from "https://x.nest.land/hyper@1.4.2/mod.js";
import config from "./hyper.config.js";

core(config)
core(config);
```

config file:

``` js
```js
import { opine } from "https://x.nest.land/hyper-app-opine@1.2.1/mod.js";
import queue from "https://x.nest.land/hyper-adapter-queue@0.0.1/mod.js";

Expand All @@ -53,45 +54,45 @@ export default = {
## Example

// create a queue
``` js
const hyper = 'https://cloud.hyper.io/apps/test/queue/default'

```js
const hyper = "https://cloud.hyper.io/apps/test/queue/default";
fetch(hyper, {
method: 'PUT',
method: "PUT",
headers,
body: JSON.stringify({
target: 'https://jsonplaceholder.typicode.com/posts'
})
})
target: "https://jsonplaceholder.typicode.com/posts",
}),
});
```

// post a job

``` js
const hyper = 'https://cloud.hyper.io/apps/test/queue/default'
```js
const hyper = "https://cloud.hyper.io/apps/test/queue/default";
fetch(hyper, {
method: 'POST',
method: "POST",
headers,
body: JSON.stringify({
hello: 'world'
})
})
hello: "world",
}),
});
```

## Testing

Run Tests...

``` sh
```sh
./scripts/test.sh
```

Run Test Harness

``` sh
```sh
./scripts/harness.sh
```


## Contributing

Contributions are welcome!
Expand Down
46 changes: 26 additions & 20 deletions adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,36 @@ import { Queue, R } from "./deps.js";

const { pluck, omit } = R;
const queue = new Queue();
const [SUCCESS, ERROR, READY, JOB, QUEUE, NAME, T] = [
"SUCCESS",
"ERROR",
"READY",
"job",
"queue",
"name",
true,
];

export default function ({ db }) {
function index() {
return db.find({ type: "queue" })
.then(pluck("name"));
return db.find({ type: QUEUE })
.then(pluck(NAME));
}

function create({ name, target, secret }) {
return db
.insert({ type: "queue", _id: name, name, target, secret })
.then((doc) => ({ ok: true, _id: doc._id }));
.insert({ type: QUEUE, _id: name, name, target, secret })
.then((doc) => ({ ok: T, id: doc._id }));
}

function doDelete(name) {
return db
.removeOne({ _id: name })
.then((_doc) => ({ ok: true }));
.then((_doc) => ({ ok: T }));
}

async function post({ name, job }) {
// TODO
job = { queue: name, type: "job", ...job, status: "READY" };
job = { queue: name, type: JOB, ...job, status: READY };
// get queue data
const q = await db.findOne({ _id: name });
// store job doc to db
Expand All @@ -37,39 +45,37 @@ export default function ({ db }) {
db.updateOne({ _id: job._id }, { $set: { status: result.status } })
)
.catch((e) =>
db.updateOne({ _id: job._id }, { $set: { status: "ERROR" } })
db.updateOne({ _id: job._id }, { $set: { status: ERROR } })
)
//.then(console.log.bind(console))
);
// return success response
return Promise.resolve({ ok: true, _id: job._id });
return Promise.resolve({ ok: true, id: job._id });
}

async function get({ name, status }) {
return await db.find({ type: "job", queue: name, status })
.then((jobs) => ({ ok: true, jobs, status }));
return await db.find({ type: JOB, queue: name, status })
.then((jobs) => ({ ok: T, jobs, status }));
}

async function retry({ name, id }) {
const job = await db.findOne({ _id: id, type: "job", queue: name });
const q = await db.findOne({ type: "queue", _id: name });
const job = await db.findOne({ _id: id, type: JOB, queue: name });
const q = await db.findOne({ type: QUEUE, _id: name });

queue.push(async () =>
await postJob(q, job)
.then((result) =>
db.updateOne({ _id: job._id }, { $set: { status: result.status } })
)
.catch((e) =>
db.updateOne({ _id: job._id }, { $set: { status: "ERROR" } })
db.updateOne({ _id: job._id }, { $set: { status: ERROR } })
)
//.then(console.log.bind(console))
);
return Promise.resolve({ ok: true });
return Promise.resolve({ ok: T });
}

async function cancel({ name, id }) {
return await db.removeOne({ _id: id, type: "job", queue: name })
.then((res) => ({ ok: true }));
return await db.removeOne({ _id: id, type: JOB, queue: name })
.then((res) => ({ ok: T }));
}

return Object.freeze({
Expand All @@ -92,5 +98,5 @@ function postJob(q, job) {
},
body: JSON.stringify(body),
})
.then((res) => res.ok ? ({ status: "SUCCESS" }) : ({ status: "ERROR" }));
.then((res) => res.ok ? ({ status: SUCCESS }) : ({ status: ERROR }));
}
4 changes: 2 additions & 2 deletions egg.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"description": "Hyper Queue adapter",
"homepage": "https://github.com/hyper63/hyper-adapter-queue",
"repo": "https://github.com/hyper63/hyper-adapter-queue",
"version": "0.0.1",
"version": "0.0.2",
"unstable": false,
"unlisted": false,
"files": [
Expand All @@ -20,4 +20,4 @@
"checkTests": false,
"checkInstallation": false,
"check": false
}
}
2 changes: 1 addition & 1 deletion test/adapter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { assert, validateDataAdapterSchema } from "../dev_deps.js";

import adapterBuilder from "../adapter.js";

const adapter = adapterBuilder({db: null});
const adapter = adapterBuilder({ db: null });

Deno.test("should implement the port", () => {
assert(validateDataAdapterSchema(adapter));
Expand Down
2 changes: 1 addition & 1 deletion test/hyper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import PORT_NAME from "../port_name.js";
const hyperConfig = {
app: appOpine,
adapters: [
{ port: PORT_NAME, plugins: [myAdapter('/tmp/hyper-harness.db')] },
{ port: PORT_NAME, plugins: [myAdapter("/tmp/hyper-harness.db")] },
],
};

Expand Down

0 comments on commit 5bccf75

Please sign in to comment.