@grouparoo/core
Version:
The Grouparoo Core
70 lines (69 loc) • 2.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DestinationDestroy = void 0;
const actionhero_1 = require("actionhero");
const Destination_1 = require("../../models/Destination");
const Run_1 = require("../../models/Run");
const Export_1 = require("../../models/Export");
const clsTask_1 = require("../../classes/tasks/clsTask");
class DestinationDestroy extends clsTask_1.CLSTask {
constructor() {
super(...arguments);
this.name = "destination:destroy";
this.description = "untrack the group, wait for the exports, and then delete the destination";
this.frequency = 0;
this.queue = "destinations";
this.inputs = {
destinationId: { required: true },
};
}
async runWithinTransaction({ destinationId, }) {
const destination = await Destination_1.Destination.scope(null).findOne({
where: { id: destinationId, state: "deleted" },
});
// the destination may have been force-deleted
if (!destination)
return;
// Are there any running runs for this destination in progress? We should let them finish.
const run = await Run_1.Run.scope(null).findOne({
where: { destinationId: destination.id },
order: [["updatedAt", "desc"]],
limit: 1,
});
// the run is not yet complete
if (run && (run.state === "running" || run.state === "draft")) {
return;
}
// ensure that all the exports are complete
try {
await Destination_1.Destination.waitForPendingExports(destination);
}
catch (error) {
if (error.message.match(/cannot delete destination/)) {
return;
}
else
throw error;
}
// wait an appropriate amount of time to ensure that there are no more exports being created in another thread
const wait = actionhero_1.config.tasks.timeout * 5;
const latestExport = await Export_1.Export.findOne({
where: { destinationId: destination.id },
order: [["updatedAt", "desc"]],
limit: 1,
});
if (run) {
if (run.updatedAt.getTime() + wait > new Date().getTime()) {
return;
}
}
if (latestExport) {
if (latestExport.updatedAt.getTime() + wait > new Date().getTime()) {
return;
}
}
// the run is done (complete or stopped) or there was nothing tracked for this destination
await destination.destroy();
}
}
exports.DestinationDestroy = DestinationDestroy;