Terminating a Worker

Is there a way to terminate a worker?

I have a worker I put an setInterval timer into. The timer unfortunately defers promises and is extraordinarily light weight. As a result once the worker starts it can run a very long time before consuming all its CPU allotment and dieing, sometimes up to 10 minutes. Pushing a new version of the worker using the online editor does not kill the existing worker. I know this because in the new worker I put console.log(event) as the first line in the fetch listener in the new code and nothing prints out. Prior to saving the new code a line was logged to the console on every invocation of the interval timer. After pushing console logging stopped, but the original code was still running in the background. After a while a get the standard timeout exception from Cloudflare. I submit a new request and the event logs.

I have figured out how to write my code without a setInterval, but if I ever need to use one in the future, it sure would be nice to be able to terminate the worker early from an external utility or with standardized Cloudflare provided safety code inside the worker (I have figured out how to write my own). Something like a non-standard maxTime(ms) in global scope would do it.

For 5 or 6 code change iterations, I did not realize my setInterval was causing a problem … twiddling my thumbs swearing at Cloudflare for not seeming to push my code changes to production :wink:

I can’t imagine how bad this would have been if I was running a Workers Unbound worker!

If you have added a feature to do this in Workers Unbound, happy to beta test. I did submit an application.

1 Like

It would be odd if unbound, didn’t have a timeout feature just like Lambdas.

At first I thought you where deliberately trying to keep them alive, for long-running tasks :smiley:

But I bet that would be against ToS or something.