Skip to content

Commit 487a9b2

Browse files
committed
creating tasks
1 parent f1ead58 commit 487a9b2

File tree

5 files changed

+80
-48
lines changed

5 files changed

+80
-48
lines changed

examples/index.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const fetch = require('node-fetch');
2+
3+
/*
4+
const dlay = require('dlay')({database: 'dlay_tasks'}),
5+
{worker} = dlay;
6+
*/
7+
const { worker, createTask } = require('../')();
8+
const manobi = worker('manobi');
9+
manobi.addJob('compress', async (ctx, done) => {
10+
//fast exec
11+
//done(null, {sucesso: 'muleque'});
12+
13+
// Async exec
14+
const deps = await ctx.deps();
15+
console.log(deps);
16+
const res = await fetch('https://dog.ceo/api/breeds/image/random');
17+
return res.json();
18+
19+
// failed
20+
//done({deu: 'ruim'});
21+
22+
// Success msg
23+
//done(null, {deu: 'certo'});
24+
25+
//long run
26+
27+
// setTimeout(() => {
28+
// console.log('long exec');
29+
// done(null, 'acabou');
30+
// }, 200000000);
31+
32+
});
33+
34+
createTask({date: new Date().toISOString()});

index.js

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,14 @@
1-
const fetch = require('node-fetch');
21
const Worker = require('./lib/worker');
3-
const worker = new Worker({
4-
name: 'manobi',
5-
database: 'dlay_tasks'
6-
});
7-
8-
worker.addJob('compress', async (ctx, done) => {
9-
//fast exec
10-
//done(null, {sucesso: 'muleque'});
11-
12-
// Async exec
13-
const deps = await ctx.deps();
14-
console.log(deps);
15-
const res = await fetch('https://dog.ceo/api/breeds/image/random');
16-
return res.json();
17-
18-
// failed
19-
//done({deu: 'ruim'});
20-
21-
// Success msg
22-
//done(null, {deu: 'certo'});
23-
24-
//long run
25-
26-
// setTimeout(() => {
27-
// console.log('long exec');
28-
// done(null, 'acabou');
29-
// }, 200000000);
30-
31-
});
2+
const Task = require('./lib/task');
3+
const CouchdbAdaptor = require('./lib/couchdb-adaptor');
4+
5+
module.exports = (connection = {}, Adaptor = CouchdbAdaptor) => {
6+
return {
7+
worker(name, options = connection, Adaptor = CouchdbAdaptor){
8+
return new Worker({ ...options, name }, Adaptor);
9+
},
10+
createTask(info, options = connection, Adaptor = CouchdbAdaptor){
11+
return new Task(info, options, Adaptor).save();
12+
}
13+
}
14+
}

lib/couchdb-adaptor.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,23 @@ module.exports = class Adaptor {
2424
* @param {Object} options
2525
*/
2626
createConnection(options){
27-
let merged = {...STD, ...options};
28-
const url = new URL(`${merged.protocol}://${merged.hostname}`);
29-
url.port = merged.port;
30-
url.username = merged.username;
31-
url.password = merged.password;
27+
28+
const url = new URL(`${options.protocol}://${options.hostname}`);
29+
url.port = options.port;
30+
url.username = options.username;
31+
url.password = options.password;
3232
this.connection = nano(url.toString());
3333
}
3434

3535
/**
3636
* @method connect
3737
* @param {Object} options
3838
*/
39-
async connect(options){
40-
this.createConnection(options);
41-
const {database} = options;
39+
connect(options){
40+
let merged = {...STD, ...options};
41+
this.createConnection(merged);
42+
const {database} = merged;
4243
this.db = this.connection.db.use(database);
43-
this.subscribe(options.seq);
44-
// for cold start
45-
const docs = await this.looseTasks();
46-
docs.forEach((doc) => {
47-
this.onChange(this.formatDoc(doc));
48-
});
4944
}
5045

5146
/**
@@ -72,9 +67,14 @@ module.exports = class Adaptor {
7267

7368
/**
7469
* @method subscribe
75-
* @param {Number} seq
7670
*/
77-
subscribe(seq){
71+
async subscribe(){
72+
// for cold start
73+
const docs = await this.looseTasks();
74+
docs.forEach((doc) => {
75+
this.onChange(this.formatDoc(doc));
76+
});
77+
7878
const name = this.name;
7979
this.feed = this.db.follow({
8080
include_docs: true,
@@ -115,6 +115,10 @@ module.exports = class Adaptor {
115115
return docs;
116116
}
117117

118+
create(task){
119+
return this.db.insert(task);
120+
}
121+
118122
/**
119123
* @method update
120124
* @param {Object} task

lib/task.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = class Task {
2+
constructor(info = {}, connection = {}, Adaptor){
3+
this.storage = new Adaptor();
4+
this.storage.connect(connection);
5+
this.info = info;
6+
}
7+
8+
save(){
9+
return this.storage.create(this.info);
10+
}
11+
}

lib/worker.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const Scheduler = require('./scheduler');
2-
const CouchdbAdaptor = require('./couchdb-adaptor');
32
const Context = require('./context');
43
const queue = require('queue');
54

@@ -8,7 +7,7 @@ const queue = require('queue');
87
* @extends Scheduler
98
*/
109
module.exports = class Worker extends Scheduler {
11-
constructor(options, Adaptor = CouchdbAdaptor){
10+
constructor(options, Adaptor){
1211
// Can't create a worker without name
1312
if(!options || !options.name) throw new Error('The attribute "name" is required to construct Workers');
1413

@@ -24,6 +23,7 @@ module.exports = class Worker extends Scheduler {
2423
this.storage.name = this.name;
2524
this.storage.onChange = (change) => this.changeHandler.call(this, change);
2625
this.storage.connect(options);
26+
this.storage.subscribe();
2727
}
2828

2929
enqueue(id, fn){

0 commit comments

Comments
 (0)