n8n
Version:
n8n Workflow Automation Tool
100 lines • 5.21 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PollTriggerTaskHandler = void 0;
const backend_common_1 = require("@n8n/backend-common");
const db_1 = require("@n8n/db");
const di_1 = require("@n8n/di");
const ensure_error_1 = require("@n8n/utils/errors/ensure-error");
const n8n_core_1 = require("n8n-core");
const n8n_workflow_1 = require("n8n-workflow");
const trigger_execution_context_factory_1 = require("../../workflows/triggers/trigger-execution-context.factory");
const poll_trigger_task_1 = require("./poll-trigger-task");
let PollTriggerTaskHandler = class PollTriggerTaskHandler {
constructor(logger, triggerExecutionContextFactory, triggersAndPollers, workflowRepository) {
this.logger = logger;
this.triggerExecutionContextFactory = triggerExecutionContextFactory;
this.triggersAndPollers = triggersAndPollers;
this.workflowRepository = workflowRepository;
this.taskType = poll_trigger_task_1.POLL_TRIGGER_TASK_TYPE;
this.logger = this.logger.scoped('scheduler');
}
async execute(task, report) {
const { workflowId, nodeId } = this.parsePayload(task);
const workflowData = await this.triggerExecutionContextFactory.loadPublishedWorkflowData(workflowId, { bypassCache: true });
const node = this.resolveTriggerNode(workflowData, nodeId, task);
const { workflow, pollFunctions } = await this.triggerExecutionContextFactory.createPollExecutionContext(workflowData, node);
await workflow.expression.acquireIsolate();
try {
const pollResponse = await this.triggersAndPollers.runPollFunction(workflow, node, pollFunctions);
if (pollResponse !== null) {
if (!(await this.workflowRepository.isActive(workflowId))) {
this.logger.debug('Workflow deactivated during poll; discarding the result', {
taskId: task.id,
jobId: task.jobId,
workflowId,
nodeId,
});
return report.notDispatched();
}
pollFunctions.__emit(pollResponse);
this.logger.debug('Poll returned new data; handed off to a new execution', {
taskId: task.id,
jobId: task.jobId,
workflowId,
nodeId,
});
return report.dispatched();
}
this.logger.debug('Poll returned no new data; nothing to hand off', {
taskId: task.id,
jobId: task.jobId,
workflowId,
nodeId,
});
return report.notDispatched();
}
catch (error) {
pollFunctions.__emitError((0, ensure_error_1.ensureError)(error));
this.logger.debug('Poll failed at runtime; routed to the error workflow', {
taskId: task.id,
jobId: task.jobId,
workflowId,
nodeId,
});
return report.dispatched();
}
finally {
await workflow.expression.releaseIsolate();
}
}
parsePayload(task) {
if (!(0, poll_trigger_task_1.isPollTriggerTaskPayload)(task.payload)) {
throw new n8n_workflow_1.UnexpectedError('Poll-trigger task payload is missing workflowId or nodeId', {
extra: { taskId: task.id, jobId: task.jobId },
});
}
return task.payload;
}
resolveTriggerNode(workflowData, nodeId, task) {
const node = workflowData.nodes.find((candidate) => candidate.id === nodeId);
if (!node || node.disabled) {
throw new n8n_workflow_1.UnexpectedError('Poll-trigger task points to a node that is missing or disabled in the published workflow', { extra: { taskId: task.id, jobId: task.jobId, workflowId: workflowData.id, nodeId } });
}
return node;
}
};
exports.PollTriggerTaskHandler = PollTriggerTaskHandler;
exports.PollTriggerTaskHandler = PollTriggerTaskHandler = __decorate([
(0, di_1.Service)(),
__metadata("design:paramtypes", [backend_common_1.Logger, trigger_execution_context_factory_1.TriggerExecutionContextFactory, n8n_core_1.TriggersAndPollers, db_1.WorkflowRepository])
], PollTriggerTaskHandler);
//# sourceMappingURL=poll-trigger-task-handler.js.map