diff --git a/src/database-backends/index.js b/src/database-backends/index.js index d3b8a0e..c51282c 100644 --- a/src/database-backends/index.js +++ b/src/database-backends/index.js @@ -159,12 +159,13 @@ module.exports = function (state) { }, forItem: function (_options) { // FIXME: Proper validation rules here for the other fields as well - let { item, task, mutationQueue, readTX, simulate } = validateOptions(arguments, { + let { item, task, mutationQueue, readTX, simulate, onDeleteSelf } = validateOptions(arguments, { item: anything, task: [ required, isTask ], mutationQueue: anything, readTX: maybeTX, - simulate: anything + simulate: anything, + onDeleteSelf: isFunction }); // We create a new instance of the actual API for every item being processed. This is necessary because some of the input arguments will default to item-specific values, and some of the logic is dependent on task-specific metadata. This is a more efficient (and understandable) approach than pretending the API is stateless and then separately wrapping the API *again* for every individual item with a whole separate layer of input validation rules. @@ -261,6 +262,11 @@ module.exports = function (state) { ] }); + if (options.id === item.id) { + // This hook is necessary to allow the task kernel to skip certain operations in this case, eg. storing the task result - it would be redundant, and reference a now non-existent item. + onDeleteSelf(); + } + return mutableOperation((tx) => { return backend.deleteItem(tx, options); }); @@ -294,6 +300,7 @@ module.exports = function (state) { updateData: function (_options) { // NOTE: This is a semantically self-describing convenience wrapper for `storeItem` that updates the currently-being-processed item + // TODO: Have a dedicated alias and/or signature (for this function) for the common case of "just add a few attributes to whatever is already there"? ie. a shallow merge let [ options ] = validateArguments(arguments, { options: [ required, wrapValueAsOption("update"), { id: [ defaultTo(item.id), isString ], diff --git a/src/run-task.js b/src/run-task.js index 3e1aaa0..fc53242 100644 --- a/src/run-task.js +++ b/src/run-task.js @@ -8,7 +8,14 @@ const logStatus = require("./util/log-status"); module.exports = function ({ backend }) { return function runTask(task, item) { let queue = []; - let api = backend.forItem({ task: task, item: item, mutationQueue: queue }); + let itemIsDeleted = false; + + let api = backend.forItem({ + task: task, + item: item, + mutationQueue: queue, + onDeleteSelf: () => { itemIsDeleted = true; } + }); return Promise.try(() => { // TODO: Standardize logging control/levels interface, also for library use @@ -30,7 +37,9 @@ module.exports = function ({ backend }) { }); }); }).then(async () => { - await api.internal.markTaskCompleted(); + if (!itemIsDeleted) { + await api.internal.markTaskCompleted(); + } if (!process.env.SRAP_QUIET) { logStatus(task, chalk.bold.green, "completed", item.id);