Skip to content

Commit 029c012

Browse files
committed
huge compose job refactor
1 parent 8f096ad commit 029c012

File tree

4 files changed

+63
-63
lines changed

4 files changed

+63
-63
lines changed

index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ const worker = new Worker({
77

88
worker.addJob('compress', async (ctx, done) => {
99
//fast exec
10-
//done();
10+
//done(null, {sucesso: 'muleque'});
1111

1212
// Async exec
13-
//const res = await fetch('https://dog.ceo/api/breeds/image/random');
14-
//return res.json();
13+
const res = await fetch('https://dog.ceo/api/breeds/image/random');
14+
return res.json();
1515

1616
// failed
1717
//done({deu: 'ruim'});
@@ -20,5 +20,10 @@ worker.addJob('compress', async (ctx, done) => {
2020
//done(null, {deu: 'certo'});
2121

2222
//long run
23-
setTimeout(() => done(null, 'acabou'), 200000000);
23+
24+
// setTimeout(() => {
25+
// console.log('long exec');
26+
// done(null, 'acabou');
27+
// }, 200000000);
28+
2429
});

lib/context.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@ module.exports = class Context {
66
this.duration = null;
77
//Object.assign(this, task);
88
this.task = task;
9-
/*
10-
119
this.timeout = undefined;
1210
this.execution = undefined;
1311
this.running = undefined;
14-
*/
15-
12+
1613
const obs = new PerformanceObserver((items) => {
1714
this.duration = items.getEntries()[0].duration;
1815
performance.clearMarks();
1916
obs.disconnect();
2017
});
2118
obs.observe({ entryTypes: ['measure'] });
19+
20+
this.timeout = setTimeout(() => {
21+
this.cancel();
22+
this.done({error: 'timeout'});
23+
}, task.timeout || 5000);
2224
}
2325

2426
prev(){
@@ -29,16 +31,29 @@ module.exports = class Context {
2931

3032
}
3133

32-
queue(){
33-
34-
}
35-
3634
// private
37-
start(){
35+
start(job){
3836
performance.mark('job_start');
37+
try {
38+
this.execution = setTimeout(() => {
39+
this.running = job(this, this.done);
40+
const isPromise = this.running && typeof this.running.then == 'function';
41+
if(isPromise){
42+
this.running.then(res => this.done(null, res)).catch(err => this.done(err));
43+
}
44+
}, 0);
45+
} catch (error) {
46+
console.log('Error executing job', error);
47+
this.done(error);
48+
}
49+
}
50+
51+
cancel(){
52+
clearTimeout(this.execution);
3953
}
4054

4155
stop(){
56+
clearTimeout(this.timeout);
4257
performance.mark('job_stop');
4358
performance.measure('execution_time', 'job_start', 'job_stop');
4459
return this.duration;

lib/worker.js

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,7 @@ module.exports = class Worker extends Scheduler {
8080
composeJob(task, job){
8181
// Create context
8282
const context = this.createContext(task);
83-
84-
// Job composition
85-
let timeout,
86-
execution,
87-
running;
88-
89-
const cancel = () => {
90-
clearTimeout(execution);
91-
}
92-
93-
const done = (err, res) => {
94-
console.log('Tarefa finalizada');
95-
console.log('err', err);
96-
console.log('res', res);
97-
clearTimeout(timeout);
83+
context.done = (err, res) => {
9884
context.stop();
9985
// Enqueue the task update
10086
this.enqueue(task.id, _ => {
@@ -103,30 +89,8 @@ module.exports = class Worker extends Scheduler {
10389
}
10490

10591
return () => {
106-
/*
107-
* TODO: test timeout error
108-
*/
109-
timeout = setTimeout(() => {
110-
cancel();
111-
done({error: 'timeout'});
112-
}, task.timeout || 5000);
113-
114-
context.start();
92+
context.start(job);
11593
console.log('Running', task.id, 'date', task.date);
116-
117-
try {
118-
execution = setTimeout(() => {
119-
running = job(context, done);
120-
}, 0);
121-
const isPromise = running && typeof running.then == 'function';
122-
if(isPromise){
123-
console.log('é um promessa');
124-
running.then(res => done(null, res)).catch(err => done(err));
125-
}
126-
} catch (error) {
127-
console.log('Error executing job', error);
128-
done(error);
129-
}
13094
}
13195
}
13296

spec/context.spec.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ describe('Context', () => {
6868
});
6969
describe('#next', () => {
7070
let task = {};
71+
const job = (ctx, done) => {
72+
done();
73+
}
7174
beforeEach(() => {
7275
task = {
7376
id: '101020teste2010',
@@ -88,8 +91,11 @@ describe('Context', () => {
8891
executions: 1
8992
}
9093
const ctx = new Context(incrementalTask);
91-
ctx.start();
92-
ctx.stop();
94+
ctx.done = () => {
95+
ctx.stop();
96+
}
97+
ctx.start(job);
98+
9399
const next = {
94100
...task,
95101
status: 'failed',
@@ -105,8 +111,10 @@ describe('Context', () => {
105111
describe('When error', () => {
106112
it('without retry the status is "failed"', () => {
107113
const ctx = new Context(task);
108-
ctx.start();
109-
ctx.stop();
114+
ctx.done = () => {
115+
ctx.stop();
116+
}
117+
ctx.start(job);
110118
const next = {
111119
...task,
112120
status: 'failed',
@@ -131,8 +139,10 @@ describe('Context', () => {
131139
}
132140
};
133141
const ctx = new Context(retryableTask);
134-
ctx.start();
135-
ctx.stop();
142+
ctx.done = () => {
143+
ctx.stop();
144+
}
145+
ctx.start(job);
136146
const next = {
137147
...retryableTask,
138148
date: '2019-12-20T12:14:25.864Z',
@@ -150,8 +160,10 @@ describe('Context', () => {
150160
describe('When success', () => {
151161
it('without repeat option', () => {
152162
const ctx = new Context(task);
153-
ctx.start();
154-
ctx.stop();
163+
ctx.done = () => {
164+
ctx.stop();
165+
}
166+
ctx.start(job);
155167
const next = {
156168
...task,
157169
status: 'complete',
@@ -169,8 +181,10 @@ describe('Context', () => {
169181
...task,
170182
retries: 3
171183
});
172-
ctx.start();
173-
ctx.stop();
184+
ctx.done = () => {
185+
ctx.stop();
186+
}
187+
ctx.start(job);
174188
const next = {
175189
...task,
176190
status: 'complete',
@@ -196,8 +210,10 @@ describe('Context', () => {
196210
}
197211
};
198212
const ctx = new Context(repeatableTask);
199-
ctx.start();
200-
ctx.stop();
213+
ctx.done = () => {
214+
ctx.stop();
215+
}
216+
ctx.start(job);
201217
const next = {
202218
...repeatableTask,
203219
date: '2019-12-20T12:14:25.864Z',

0 commit comments

Comments
 (0)