-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumer.event.js
46 lines (37 loc) · 1.22 KB
/
consumer.event.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const process = require('process')
const EventEmitter = require('events')
const event = new EventEmitter()
const log = true;// controla logs
let i = 0; // contador para saber quantas mensagens foram mandadas
const startProcess = (data) => {
log && console.info(`starting`)
setImmediate(() => event.emit('run'))
++i
}
event.on('start', startProcess)
const runProcess = async (data) => {
log && console.info(`running`)
setImmediate(() => event.emit('finish'))
}
event.on('run', runProcess)
const finishProcess = (data) => {
log && console.info(`finished`)
setImmediate(() => event.emit('next'))
}
event.on('finish', finishProcess)
const next = () => {
log && console.info(`next`)
setImmediate(() => event.emit('start', new Date().toISOString()))
}
event.on('next', next)
// tratamento para fechamento do programa
const handler = (err => {
console.error(`\nTotal de ${i}`, err?.stack ?? err);
process.exit(1); // mandatory (as per the Node.js docs)
});
// tratamento caso node quebre
process.on('uncaughtException', handler)
// tratamento caso cancele
process.on('SIGINT', handler)
event.emit('start',new Date().toISOString())
setInterval(() => {console.clear() ; console.log(`Total ${i}`)}, 2000);