Do not auto restart cc task on error

Sometimes it is undesirable for a particular cc_task to run more than once, and it is imperative to not allow the task to restart on error, but simply throw an error and quit. What is the best way to do this?

Hi @martin

To be clear, are you talking about overriding the default automatic 5 x retry behavior of CC tasks that fail / error out?

Correct :100:

Hi Tielman. Many update on this?

@martin The most surefire way is to wrap your entire CC execution in a try catch. Something like this

import { TaskContext } from '@journeyapps/cloudcode';

export async function run(this: TaskContext) {
    try {
        await reallyRun.call(this)
    } catch (er) {
        // Catch the error but don't throw anything
        // this avoids the auto retry logic
        console.log(er);
    }
}

export async function reallyRun(this: TaskContext) {
    // Throw some defined error
    throw Error('Some new error')
    
    // Alternatively, throw undefined which will not be handled well by most error handling implementations
    // throw undefined;
}

Please note I use the function.call syntax to allow me to pass the TaskContext into the subsequent function, this allows that subsequent function to execute in the same|similar context as the original run function.

Hi Tielman

I am using this logic currently but it does not seem to work when a task retried because it timed out after 3 minutes, as in my example code.

Regards,
Martin

Hi Martin

CC tasks timing out are not technically errors (there is a physical hard limit on the execution time of each task of 5 minutes, after which the entire process is just killed) and as such need to be handled separately.

Please take a look at this post about how to handle avoiding task timeouts.

Let me know if that does not answer your question