-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
124 lines (113 loc) · 3.16 KB
/
Copy pathindex.js
File metadata and controls
124 lines (113 loc) · 3.16 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const request = require('@fatmatto/ptth')
const CronJob = require('cron').CronJob
const EventEmitter = require('events').EventEmitter
/**
* @class Worker
*/
class Worker extends EventEmitter {
/**
* Creates an instance of a Worker.
* @param {Object} config
* @param {String} config.topic The name of the kind of jobs this worker will run
* @param {String} [config.scheduling] Cron expression to state when to look for queued jobs to process. Defaults to every minute.
* @param {String} [config.gravityUrl] The gravity instance's url
* @param {String} [config.timezone] The timezone to use in the cron parser
* @param {String} [config.retryBackoff] Backoff time after a job is set to be retried
*
*/
constructor (config) {
super()
this.topic = config.topic
this.retryBackoff = config.retryBackoff || null
this.gravityUrl = config.gravityUrl
this.scheduling = config.scheduling || '* * * * *'
if (config.timezone) {
this.timezone = config.timezone
}
}
/**
* Returns the next job to run
*/
async dequeue () {
const response = await request({
method: 'POST',
url: `${this.gravityUrl}/topics/${this.topic}/dequeue`
})
const job = response.body.data
if (job) {
this.emit('job', job)
}
return job
}
/**
* Enqueues a new job into the queue
*/
async enqueue (data) {
const response = await request({
method: 'POST',
url: `${this.gravityUrl}/topics/${this.topic}/enqueue`,
body: data
})
return response.body.data
}
/**
* Mark the job as succesfully completed
* @param {String} jobUuid The job uuid
*/
async complete (jobUuid, output = {}) {
const response = await request({
method: 'PUT',
url: `${this.gravityUrl}/jobs/${jobUuid}/complete`,
body: {
output
}
})
this.emit('complete', response.body.data)
return response.body.data
}
/**
* Mark the job as failed
* @param {String} jobUuid The job uuid
*/
async fail (jobUuid, error = {}) {
const response = await request({
method: 'PUT',
url: `${this.gravityUrl}/jobs/${jobUuid}/fail`,
body: {
error
}
})
this.emit('fail', response.body.data)
return response.body.data
}
/**
* Return the job to the "in_queue" state
* @param {String} jobUuid The job uuid
*/
async return (jobUuid) {
const options = {}
if (this.retryBackoff !== null) {
// Retry backoff is expressed in milliseconds
// we will add these milliseconds to the current time
const currentTime = Date.now()
const backoffMs = currentTime + this.retryBackoff
options.backoffUntil = new Date(backoffMs).toISOString()
}
const response = await request({
method: 'PUT',
url: `${this.gravityUrl}/jobs/${jobUuid}/return`,
body: options
})
this.emit('return', response.body.data)
return response.body.data
}
/**
* Starts dequeing job, whenever a job is found, it emits a "job" event.
*/
start () {
this.cron = new CronJob(this.scheduling, async () => {
await this.dequeue()
}, null, true, this.timezone)
}
}
module.exports = { Worker }