Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gitwithravish's avatar

How to handle background tasks while deploying new version

I have a project which runs several background tasks using custom artisan commands and laravel scheduler. Currently my app is in its new born stage and therefore we deploy new version of app very frequently.

Problem

  • A long background task is running for 30 minutes. If the app gets deployed in between, the command execution gets unexpectedly stopped. This causes unexpected behaviors.

SOLUTIONS I have thought of

  • I can make use of console signal handling to gracefully stop the command execution but this does not sound like the best approach for my use case.
  • I can define a timeframe when the commands will be executed and no deployment should take place. However this is still human error prone and sometimes we cannot wait to deploy the app in next timeframe specially in case of priority bug fixes.
0 likes
6 replies
jlrdw's avatar

Announce in advance with a banner site will be down from x to y time frame.

That's the way Humana insurance does it for maintenance. Otherwise don't update so often, you shouldn't have to anyway.

gitwithravish's avatar

@jlrdw These background tasks are not related to maintenance. These are normal background tasks that runs forever. Things like creating reports, sending notifications, sending/collecting data via third party APIs. I do not want my continuous deployments to crash these command executions unexpectedly, especially when a long running task is being executed.

jlrdw's avatar

@gitwithravish You don't need to update so often, just take the site down once a year to update. All sites have to be taken down sometimes, even Fedex is down at times, probably for updates.

I have heard the term hot deployment, but there are limits. You can't just suddenly override running code.

gitwithravish's avatar

@jlrdw I think the question is not understood properly here.

I will frame it again in the simplest words.

  • I have an API project. It has few endpoints as well as some background tasks.
  • These tasks run in background 24/7 and my api project gets deployed everyday twice or thrice.
  • I do not have a standard way to stop the command execution and then deploy the new version of API project
  • Sometimes a cron job would be running in background which takes 30 minutes to complete a task, now that gets crashed if at the same time the app gets deployed. I do not have a way to stop that cron command and then run my CI/CD Pipeline. And I cannot do this by SSH into my server everytime.
jlrdw's avatar

@gitwithravish Why are you having to deploy an app so often, I have never heard of that. But again a crash is going to happen if you override running code.

I worked for the State of Texas and code was maybe updated every other year, at least the Java ee aspect.

my api project gets deployed everyday twice or thrice.

But why. I suggest get it properly working good then deploy it, and leave the good code base alone for at least a year.

Just a suggestion.

And I do understand, but I have never in 34 years of programming to include enterprise java heard of deploying an app that often. Usually the code base is left alone for several years.

gitwithravish's avatar

@jlrdw Man what do you do when you have to update one API endpoint ? Let's say you are now sending user hometown address and office address in /users api endpoint. You need to deploy right ? I think you mean something else by term "deploy".

Please or to participate in this conversation.