Report this

What is the reason for this report?

How To Make A Worker App Run 24/7

Posted on February 2, 2022

I run my node app from my github using apps - worker on digital ocean. It works just fine with no errors but the problem is it shuts down every 2-3 hour and i would have to use yarn/npm start in console to start it again. I’m confused about what to do to keep it running 24/7



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

This comment has been deleted

Hello,

To ensure that your Node.js application runs 24/7 on the DigitalOcean App Platform, especially when it’s set up as a worker, you should address potential causes for the application to stop and ensure it’s correctly configured for continuous operation. Here are several steps and best practices to achieve this:

  • Ensure your application is designed to run continuously. If it’s a worker processing jobs, it should ideally be waiting for or polling for new jobs when idle. If the application naturally exits after completing its work, you might need to wrap it in a loop or use a job scheduler to keep it running.

  • Uncaught exceptions can cause your Node.js application to crash. Make sure to handle them properly. For example:

    process.on('uncaughtException', (error) => {
      console.error('Uncaught Exception:', error);
      // Optionally, perform a graceful shutdown or restart the process
    });
    
  • While DigitalOcean’s App Platform manages the application lifecycle, for local development or in scenarios where you have more control over the execution environment, using a process manager like PM2 can help restart your application if it crashes. For the App Platform, ensure your application internally is robust and self-sufficient.

  • The App Platform supports HTTP health checks. Even if your application is a worker and not serving HTTP traffic, implementing a basic HTTP server that responds to health check routes can help you keep the app alive. The platform can use these checks to ensure your worker is up and, if configured, restart it if it’s not responding.

    const http = require('http');
    
    const server = http.createServer((req, res) => {
      if (req.url === '/health') {
        res.writeHead(200);
        res.end('ok');
      }
    });
    
    server.listen(process.env.PORT || 3000);
    
  • Check the application’s logs and metrics in the DigitalOcean App Platform dashboard to diagnose why it might be stopping. Logs can provide clues such as errors or exceptions that lead to the shutdown.

  • Ensure your worker component on the App Platform is configured with the appropriate restart policies. While the platform automatically manages and maintains the desired state of your application, including restarting failed components, understanding and configuring these policies can help in certain scenarios.

  • Ensure your application has enough resources (CPU and memory) allocated to it. If your worker is being terminated due to out-of-memory (OOM) errors, consider scaling up the resources.

For applications running on the DigitalOcean App Platform, especially worker apps that don’t serve HTTP traffic, ensuring continuous operation involves proper error handling, potentially implementing health checks, and ensuring the application is designed to run indefinitely or has mechanisms to stay active.

Best,

Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.