Instance Not Shutting Down Despite Idle Timeout

tylerjnewman
PRO

3 months ago

Issue: Worker instance not shutting down after idle timeout

Service: Redis Worker Instance

Description: I have a simple Redis-based worker that processes jobs from a queue. The worker is designed to shut down after being idle for 5 minutes (300 seconds), but it seems to stay running indefinitely even when there are no jobs to process.

Below is a simplified version of my worker code that illustrates the core functionality:

// Core worker functionality
(async function worker() {
  console.log('Worker starting...');
  
  // Main processing loop
  while (true) {
    try {
      console.log(`Waiting for job on queue (timeout 300s)...`);
      // Block for up to 300 seconds waiting for a job
      const result = await redis.brpop(QUEUE_NAME, 300);

      if (!result) {
        console.log('Idle for 5 minutes, shutting down worker...');
        await redis.quit();
        process.exit(0); // Should exit the process after timeout
      }
      
      // Process job if one is received
      console.log('Processing job...');
    } catch (error) {
      console.error(`Error: ${error}`);
    }
  }
})();

Expected Behavior: When no jobs are received for 300 seconds (5 minutes), the redis.brpop() call should timeout, return null, and trigger the process to exit with process.exit(0).

Actual Behavior: The instance continues to run indefinitely even when there are no inbound or outbound network requests for extended periods (well over 5 minutes).

Questions:

  1. Why isn't my worker shutting down after the 300-second timeout?

  2. Is there anything specific about how Railway handles Node.js processes that could be preventing the clean shutdown?

  3. Are there any Railway-specific best practices for implementing worker shutdown after idle periods?

I've confirmed in my logs that there is no network activity during these idle periods, so I'm confused as to why the instance continues to run.

Thank you for your assistance!

Solved

1 Replies

tylerjnewman
PRO

3 months ago

Nevermind, it looks like they do shut down, sorry


Status changed to Solved brody 3 months ago


Instance Not Shutting Down Despite Idle Timeout - Railway Help Station