@sotatech/nest-taskflow
Version:
A task flow management library for NestJS with Redis Pub/Sub integration.
38 lines • 1.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CallbackHelper = void 0;
const common_1 = require("@nestjs/common");
class CallbackHelper {
static async executeWithRetry(callbackFn, taskId, options = {}) {
const logger = new common_1.Logger(CallbackHelper.name);
const { maxAttempts = 3, backoffStrategy = 'fixed', backoffTime = 1000, } = options;
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
logger.log(`Executing callback for task ${taskId}, attempt ${attempt}`);
await callbackFn();
logger.log(`Callback executed successfully for task ${taskId}`);
return;
}
catch (error) {
logger.error(`Callback failed for task ${taskId}, attempt ${attempt}`, error.stack);
if (attempt === maxAttempts) {
throw error;
}
const delay = this.getBackoffDelay(attempt, backoffTime, backoffStrategy);
await new Promise((resolve) => setTimeout(resolve, delay));
}
}
}
static getBackoffDelay(attempt, baseTime, strategy) {
switch (strategy) {
case 'exponential':
return baseTime * Math.pow(2, attempt - 1);
case 'linear':
return baseTime * attempt;
default:
return baseTime;
}
}
}
exports.CallbackHelper = CallbackHelper;
//# sourceMappingURL=callback-helper.js.map