Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If schedular is stateless, how do I invoke the stop() function at a later time? #211

Open
khan-zia opened this issue Dec 18, 2023 · 3 comments

Comments

@khan-zia
Copy link

I am sorry to be asking a rather simple question here, I tried and went through the codebase but I figured I still need some input from your side.

I have a simple use case. I have a node.js CLI app. When I run a certain command, I schedule a few jobs and the node execution that queues and starts the jobs exits of course. Now, I would like to have another CLI command let's say myapp stop that should be able to stop all previously enqueued jobs. As per my understanding, the new node process executing the myapp stop command would have no link whatsoever to anything the toad schedular is doing in memory. Take a look at this snippent.

export const beginScheduledJobs = (): void => {
  // App must be configured.
  if (getConfig('state') !== 'ready') {
    return;
  }

  // Instantiate new schedular.
  const scheduler = new ToadScheduler();

  // Queue some jobs
  scheduler.addSimpleIntervalJob(idleCheckJob);
};

/**
 * Stops all scheduled jobs.
 */
export const stopScheduledJobs = (): void => {
  // What should I do here ?
};

As you can see, the beginScheduledJobs() method would be called by my apps start command, and the stopScheduledJobs is to be called on-demand by the user later by my app's stop command. Any help would be greatly appreciated.

@khan-zia
Copy link
Author

khan-zia commented Dec 18, 2023

@kibertoad The only thing I have in mind, which feels a bit of a hack, is to use an external json file to set a start/stop flag. Although I use a json file for config etc anyway. So what I have in mind is that my stop command would set a stop flag in the json config file, and then each of my running jobs, before doing the actual job would check for the flag and abandon if the flag is set. Something along those lines...

@kibertoad
Copy link
Owner

@khan-zia Can you explain a bit more what you are trying to achieve? If you want to stop all jobs at once, you can call scheduler.stop(), and this will stop all of the jobs. And if you want to stop some specific jobs, you can store their instances somewhere, or lookup the one you need via scheduler.getAllJobs()

@khan-zia
Copy link
Author

khan-zia commented Feb 3, 2024

@kibertoad my use case is a bit different. I have a node CLI. When I invoke the myapp start command, I schedule some background jobs. But I don't want to keep the terminal occupied or require my user to keep the terminal open so I have to exit the main process that just executed the myapp start command. To achieve this, I spawn a new child process, I run the schedular on the child and then I completely detach the child from the parent allowing the parent process to exit.

Now, the other command I have is myapp stop which allows my users to stop the background jobs. The only problem is, the new node process that executes the stop command, has no idea, no link whatsoever to the now independent process from the previous command, let alone having a reference to the toad schedular's object to call the .stop() method on. So what is your suggestion for such a use case?

PS.
I ended up writing a C program that accurately identifies the process running my jobs and kills it by PID. I invoke the C binary from node whenever a user tries the myapp stop command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants