UNPKG

@juzi/wechaty

Version:

Wechaty is a RPA SDK for Chatbot Makers.

43 lines 1.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.checkUntilChanged = exports.retryPolicy = void 0; const cockatiel_1 = require("cockatiel"); const wechaty_puppet_1 = require("@juzi/wechaty-puppet"); /** * Create a retry policy that'll try whatever function we execute 3 * times with a randomized exponential backoff. * * https://github.com/connor4312/cockatiel#policyretry */ const retryPolicy = getRetryPolicy(); exports.retryPolicy = retryPolicy; function getRetryPolicy() { const policy = cockatiel_1.Policy .handleAll() .retry() .attempts(3) .exponential({ /** * ExponentialBackoff * https://github.com/connor4312/cockatiel#exponentialbackoff */ initialDelay: 1000, maxAttempts: 5, maxDelay: 10 * 1000, }); policy.onRetry(reason => wechaty_puppet_1.log.silly('wechaty', 'retry-policy getRetryPolicy policy.onRetry() reason: "%s"', JSON.stringify(reason))); policy.onSuccess(({ duration }) => wechaty_puppet_1.log.silly('wechaty', 'retry-policy getRetryPolicy policy.onSuccess(): retry call ran in %s ms', duration)); return policy; } const checkUntilChanged = async (gapMilliseconds, maxRetry, judgement) => { let changed = await judgement(); let currentTry = 1; while (!changed && (currentTry < maxRetry)) { await new Promise(resolve => setTimeout(resolve, gapMilliseconds)); changed = await judgement(); currentTry++; } return changed; }; exports.checkUntilChanged = checkUntilChanged; //# sourceMappingURL=retry-policy.js.map