1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
12 | const retry = require("retry");
|
13 | const shelljs = require("shelljs");
|
14 | const logger_1 = require("./logger");
|
15 | const nats_service_1 = require("./service/nats.service");
|
16 | const rabbitmq_service_1 = require("./service/rabbitmq.service");
|
17 | class Paperboy {
|
18 | constructor(options) {
|
19 | this.options = options;
|
20 | }
|
21 | build(buildCommand) {
|
22 | return __awaiter(this, void 0, void 0, function* () {
|
23 | const operation = retry.operation({
|
24 | forever: true,
|
25 | minTimeout: 1000,
|
26 | maxTimeout: 60 * 1000
|
27 | });
|
28 | yield new Promise(done => {
|
29 | operation.attempt(() => __awaiter(this, void 0, void 0, function* () {
|
30 | try {
|
31 | yield new Promise((resolve, reject) => {
|
32 | const buildProcess = shelljs.exec(buildCommand || this.options.command, {
|
33 | async: true
|
34 | });
|
35 | buildProcess.on('exit', (code) => {
|
36 | if (code === 0) {
|
37 | resolve();
|
38 | }
|
39 | else {
|
40 | reject(code);
|
41 | }
|
42 | });
|
43 | });
|
44 | done();
|
45 | }
|
46 | catch (error) {
|
47 | logger_1.logger.error(`Build process failed with exit code: ${error}`);
|
48 | if (operation.retry(error)) {
|
49 | logger_1.logger.warn('Retrying build...');
|
50 | return;
|
51 | }
|
52 | }
|
53 | }));
|
54 | });
|
55 | });
|
56 | }
|
57 | start() {
|
58 | return __awaiter(this, void 0, void 0, function* () {
|
59 | if (this.options.queue === undefined ||
|
60 | this.options.queue.uri === undefined) {
|
61 | throw new Error('You need to configure the queue when running paperboy start');
|
62 | }
|
63 |
|
64 | yield this.build(this.options.initialCommand);
|
65 |
|
66 | if (this.options.readinessHook && 0 !== this.options.readinessHook.length) {
|
67 | shelljs.exec(this.options.readinessHook);
|
68 | }
|
69 |
|
70 | let queueService;
|
71 | if (this.options.queue.uri.startsWith('nats')) {
|
72 | queueService = new nats_service_1.NatsService(this.options, this.build.bind(this));
|
73 | }
|
74 | else if (this.options.queue.uri.startsWith('amqp')) {
|
75 | queueService = new rabbitmq_service_1.RabbitMQService(this.options, this.build.bind(this));
|
76 | }
|
77 | else {
|
78 | throw new Error(`Unknown protocol in queue URI ${this.options.queue.uri}`);
|
79 | }
|
80 | queueService.listen();
|
81 | });
|
82 | }
|
83 | }
|
84 | exports.Paperboy = Paperboy;
|
85 |
|
\ | No newline at end of file |