|
| 1 | +import request from 'request'; |
| 2 | +import log from './../log'; |
| 3 | +import { IConfig } from './config'; |
| 4 | + |
| 5 | +interface IPollResponse { |
| 6 | + status: string |
| 7 | + errors: string[] |
| 8 | +} |
| 9 | + |
| 10 | +export default class Poller { |
| 11 | + private config: IConfig; |
| 12 | + private retryNumber = 0; |
| 13 | + private intervalId: NodeJS.Timeout | undefined; |
| 14 | + private static readonly MAX_RETRIES_WAITING = 60; |
| 15 | + private static readonly MAX_RETRIES_READY = 600; |
| 16 | + |
| 17 | + constructor(config: IConfig) { |
| 18 | + this.config = config; |
| 19 | + } |
| 20 | + |
| 21 | + public async check(id: number): Promise<IPollResponse> { |
| 22 | + return new Promise((resolve, reject) => { |
| 23 | + this.intervalId = setInterval(async () => { |
| 24 | + const response = await this.getStatus(id); |
| 25 | + log.info('checking', response); |
| 26 | + |
| 27 | + if (response.status === 'DONE') { |
| 28 | + if (response.errors.length === 0) { |
| 29 | + if (this.intervalId) { |
| 30 | + clearInterval(this.intervalId); |
| 31 | + this.intervalId = undefined; |
| 32 | + } |
| 33 | + return resolve(response); |
| 34 | + } |
| 35 | + |
| 36 | + if (this.intervalId) { |
| 37 | + clearInterval(this.intervalId); |
| 38 | + this.intervalId = undefined; |
| 39 | + } |
| 40 | + return reject(response); |
| 41 | + } |
| 42 | + |
| 43 | + if (response.status === 'WAITING' && this.retryNumber > Poller.MAX_RETRIES_WAITING) { |
| 44 | + if (this.intervalId) { |
| 45 | + clearInterval(this.intervalId); |
| 46 | + this.intervalId = undefined; |
| 47 | + } |
| 48 | + return reject(new Error(`Waited too long to retrieve information, please try again later.`)); |
| 49 | + } else if (response.status === 'READY' && this.retryNumber > Poller.MAX_RETRIES_READY) { |
| 50 | + if (this.intervalId) { |
| 51 | + clearInterval(this.intervalId); |
| 52 | + this.intervalId = undefined; |
| 53 | + } |
| 54 | + return reject(new Error(`This project has been running for over 20 minutes, stopping now.`)); |
| 55 | + } |
| 56 | + |
| 57 | + this.retryNumber += 1; |
| 58 | + }, 2000); |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + private async getStatus(id: number): Promise<IPollResponse> { |
| 63 | + return new Promise((resolve, reject) => { |
| 64 | + const requestOptions = { |
| 65 | + method: 'GET', |
| 66 | + uri: `https://api.testingbot.com/v1/cypress/${id}`, |
| 67 | + auth: { |
| 68 | + user: this.config.auth.key, |
| 69 | + pass: this.config.auth.secret, |
| 70 | + sendImmediately: true, |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | + request(requestOptions, (error, response) => { |
| 75 | + if (error) { |
| 76 | + return reject(error); |
| 77 | + } |
| 78 | + let responseBody: IPollResponse = { status: 'WAITING', errors: [] }; |
| 79 | + if (response) { |
| 80 | + if (response.body && typeof response.body === 'string') { |
| 81 | + response.body = JSON.parse(response.body) as IPollResponse; |
| 82 | + } |
| 83 | + if (response.statusCode.toString().substring(0, 1) === '2') { |
| 84 | + responseBody = response.body; |
| 85 | + } else { |
| 86 | + return reject(response.body); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + resolve(responseBody); |
| 91 | + }); |
| 92 | + }); |
| 93 | + } |
| 94 | +} |
0 commit comments