forked from esbenp/pdf-bot
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathpdf-bot.js
executable file
·417 lines (343 loc) · 10.1 KB
/
pdf-bot.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/usr/bin/env node
var fs = require('fs')
var path = require('path')
var debug = require('debug')('pdf:cli')
var Table = require('cli-table')
var program = require('commander');
var merge = require('lodash.merge')
var createPdfGenerator = require('../src/pdfGenerator')
var createApi = require('../src/api')
var error = require('../src/error')
var createQueue = require('../src/queue')
var webhook = require('../src/webhook')
var pjson = require('../package.json')
var execSync = require('child_process').execSync
var prompt = require('prompt')
program
.version(pjson.version)
.option('-c, --config <path>', 'Path to configuration file')
var decaySchedule = [
1000 * 60, // 1 minute
1000 * 60 * 3, // 3 minutes
1000 * 60 * 10, // 10 minutes
1000 * 60 * 30, // 30 minutes
1000 * 60 * 60 // 1 hour
];
var configuration, queue
var defaultConfig = {
api: {
port: 3000,
//token: 'api-token'
},
// html-pdf-chrome options
generator: {
},
queue: {
generationRetryStrategy: function(job, retries) {
return decaySchedule[retries - 1] ? decaySchedule[retries - 1] : 0
},
generationMaxTries: 5,
webhookRetryStrategy: function(job, retries) {
return decaySchedule[retries - 1] ? decaySchedule[retries - 1] : 0
},
webhookMaxTries: 5,
lowDbOptions: {
}
},
storage: {
/*
's3': createS3Config({
bucket: '',
accessKeyId: '',
region: '',
secretAccessKey: ''
})
*/
},
storagePath: 'storage',
/*webhook: {
headerNamespace: 'X-PDF-',
requestOptions: {
},
secret: '12345',
url: 'http://localhost:3001/hook'
}*/
}
program
.command('api')
.description('Start the API')
.action(function (options) {
// We delay initiation of queue. This is because the API will load the DB in memory as
// copy A. When we make changes through the CLI this creates copy B. But next time the
// user pushes to the queue using the API copy A will be persisted again.
var initiateQueue = openConfig(true)
var apiOptions = configuration.api
var port = apiOptions.port
createApi(initiateQueue, {
port: port,
token: apiOptions.token
}).listen(port, function() {
debug('Listening to port %d', port)
})
})
program
.command('install')
.action(function (options) {
var configPath = program.config || path.join(process.cwd(), 'pdf-bot.config.js')
function startPrompt() {
prompt.start({noHandleSIGINT: true})
prompt.get([
{
name: 'storagePath',
description: 'Enter a path for storage',
default: path.join(process.cwd(), 'pdf-storage'),
required: true
},
{
name: 'token',
description: 'An access token for your API',
required: false
}], function (err, result) {
if (err) {
process.exit(0)
}
var options = {}
if (result.token) {
options.api = {token: result.token}
}
options.storagePath = result.storagePath
var configContents = "module.exports = " + JSON.stringify(options, null, 2)
fs.writeFileSync(configPath, configContents)
if (!fs.existsSync(options.storagePath)) {
fs.mkdirSync(options.storagePath, '0775')
fs.mkdirSync(path.join(options.storagePath, 'db'), '0775')
fs.mkdirSync(path.join(options.storagePath, 'pdf'), '0775')
}
console.log('pdf-bot was installed successfully.')
console.log('Config file is placed at ' + configPath + ' and contains')
console.log(configContents)
console.log('You should add ALIAS pdf-bot="pdf-bot -c ' + configPath + '" to your ~/.profile')
});
}
var existingConfigFileFound = fs.existsSync(configPath)
if (existingConfigFileFound) {
prompt.start({noHandleSIGINT: true})
prompt.get([
{
name: 'replaceConfig',
description: 'A config file already exists, are you sure you want to override (yes/no)'
}
], function (err, result) {
if (err) {
process.exit(0)
}
if (result.replaceConfig !== 'yes') {
process.exit(0)
} else {
startPrompt()
}
})
} else {
startPrompt()
}
})
program
.command('generate [jobID]')
.description('Generate PDF for job')
.action(function (jobId, options){
openConfig()
var job = queue.getById(jobId)
if (!job) {
console.log('Job not found')
return;
}
processJob(job, configuration)
})
program
.command('jobs')
.description('List all completed jobs')
.option('--completed', 'Show completed jobs')
.option('--failed', 'Show failed jobs')
.option('-l, --limit [limit]', 'Limit how many jobs to show')
.action(function (options) {
openConfig()
listJobs(queue, options.failed, options.completed, options.limit)
})
program
.command('ping [jobID]')
.description('Attempt to ping webhook for job')
.action(function (jobId, options) {
openConfig()
var job = queue.getById(jobId)
if (!job) {
console.log('Job not found.')
return;
}
ping(job, configuration.webhook)
})
program
.command('ping:retry-failed')
.action(function() {
openConfig()
var maxTries = configuration.queue.webhookMaxTries
var retryStrategy = configuration.queue.webhookRetryStrategy
var next = queue.getNextWithoutSuccessfulPing(retryStrategy, maxTries)
if (next) {
ping(next, configuration.webhook)
}
})
program
.command('pings [jobId]')
.description('List pings for a job')
.action(function (jobId, options) {
openConfig()
var job = queue.getById(jobId)
if (!job) {
console.log('Job not found')
return;
}
var table = new Table({
head: ['ID', 'URL', 'Method', 'Status', 'Sent at', 'Response', 'Payload'],
colWidths: [40, 40, 50, 20, 20, 20]
});
for(var i in job.pings) {
var ping = job.pings[i]
table.push([
ping.id,
ping.url,
ping.method,
ping.status,
formatDate(ping.sent_at),
JSON.stringify(ping.response),
JSON.stringify(ping.payload)
])
}
console.log(table.toString())
})
program
.command('purge')
.description('Will remove all completed jobs')
.option('--failed', 'Remove all failed jobs')
.option('--new', 'Remove all new jobs')
.action(function (options) {
openConfig()
queue.purge(options.failed, options.new)
console.log('The queue was purged.')
})
program
.command('push [url]')
.description('Push new job to the queue')
.option('-m, --meta [meta]', 'JSON string with meta data. Default: \'{}\'')
.action(function (url, options) {
openConfig()
var response = queue.addToQueue({
url: url,
meta: JSON.parse(options.meta || '{}')
})
if (error.isError(response)) {
console.error('Could not push to queue: %s', response.message)
process.exit(1)
}
})
program
.command('shift')
.description('Run the next job in the queue')
.action(function (url) {
openConfig()
var maxTries = configuration.queue.generationMaxTries
var retryStrategy = configuration.queue.generationRetryStrategy
var next = queue.getNext(retryStrategy, maxTries)
if (next) {
processJob(next, configuration)
}
})
program.parse(process.argv)
if (!process.argv.slice(2).length) {
program.outputHelp();
}
function processJob(job, configuration) {
var generatorOptions = configuration.generator
var storagePlugins = configuration.storage
var generator = createPdfGenerator(configuration.storagePath, generatorOptions, storagePlugins)
queue.processJob(generator, job, configuration.webhook).then(response => {
if (error.isError(response)) {
console.error(response.message)
process.exit(1)
} else {
console.log('Job ID ' + job.id + ' was processed.')
process.exit(0)
}
})
}
function openConfig(delayQueueCreation = false) {
configuration = defaultConfig
if (!program.config) {
if (fs.existsSync(path.join(process.cwd(), 'pdf-bot.config.js'))) {
program.config = 'pdf-bot.config.js'
} else {
throw new Error('You need to supply a config file')
}
}
var configPath = path.join(process.cwd(), program.config)
if (!fs.existsSync(configPath)) {
throw new Error('No config file was found at ' + configPath)
}
debug('Creating CLI using config file %s', configPath)
merge(configuration, require(configPath))
if (!fs.existsSync(configuration.storagePath)) {
throw new Error('Whoops! Looks like your storage folder does not exist. You should run pdf-bot install.')
}
if (!fs.existsSync(path.join(configuration.storagePath, 'db'))) {
throw new Error('There is no database folder in the storage folder. Create it: storage/db')
}
if (!fs.existsSync(path.join(configuration.storagePath, 'pdf'))) {
throw new Error('There is no pdf folder in the storage folder. Create it: storage/pdf')
}
function initiateQueue() {
var queueOptions = configuration.queue
return createQueue(path.join(configuration.storagePath, 'db/db.json'), queueOptions.lowDbOptions)
}
if (delayQueueCreation) {
return initiateQueue
} else {
queue = initiateQueue()
}
}
function listJobs(queue, failed = false, limit) {
var response = queue.getList(
failed,
limit
)
var table = new Table({
head: ['ID', 'URL', 'Meta', 'PDF Gen. tries', 'Created at', 'Completed at'],
colWidths: [40, 40, 50, 20, 20, 20]
});
for(var i in response) {
var job = response[i]
table.push([
job.id,
job.url,
JSON.stringify(job.meta),
job.generations.length,
formatDate(job.created_at),
formatDate(job.completed_at)
])
}
console.log(table.toString());
}
function ping(job, webhookConfiguration) {
queue.attemptPing(job, webhookConfiguration || {}).then(response => {
if (!response.error) {
console.log('Ping succeeded: ' + JSON.stringify(response))
} else {
console.error('Ping failed: ' + JSON.stringify(response))
}
return response
})
}
function formatDate(input) {
if (!input) {
return ''
}
return (new Date(input)).toLocaleString()
}