V
V
Vasily Vasilyev2021-03-02 11:05:20
Node.js
Vasily Vasilyev, 2021-03-02 11:05:20

How to check if a process has terminated on the server?

I am running an application that, on the back, every, for example, 5 minutes, performs some asynchronous action that can be performed at different times. Due to technical limitations, the action will be triggered by a server ping from the outside (i.e. a loop, each iteration of which is triggered by a server ping. In this case, the iteration may take different times, sometimes more than five minutes). How to check when pinging whether the previous cycle has completed or not? Or maybe there is a smarter solution?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sergey Sokolov, 2021-03-02
@sergiks

Semaphore: write process_id (pid) to a text file.
The ping handler looks to see if the file exists, reads the pid from it, and sees if the process is alive. If not, it starts the task and writes its pid to a file.
When the task is processed, the file is deleted.

A
aarifkhamdi, 2021-03-02
@aarifkhamdi

can your backend itself determine whether it has completed an asynchronous action or not?
if your back does not know, then another service that uses the back will not be able to find out!
and if the back knows, then when you ping you simply answer 400 "the previous operation was not completed" and 200 "got to work", respectively

K
Kovalsky, 2021-03-02
@lazalu68

From the text of the question, it is clear that the TS is trying to solve the problem that has already been solved more than once within the framework of Heroku (because the service is already quite a few years old), on this occasion I decided to check for myself how it all works: I created an account for Heroku, created a task for Heroku Scheduler that pulls from the Internet a list of characters from Rick and Morty, brought up an application that gives a random character from a downloaded list.
From all this, it follows that Heroku's native tools do an excellent job. If you want to have an interval of 5 minutes, then the benefit of Heroku Scheduler is not the only one, you can use, for example, Advanced Scheduler or even Custom clock process. Clock process is a very powerful thing, you can implement anything with it

I
Igor Scherbakov, 2021-03-03
@mitgolm

Try using node-schedule as your task scheduler . Wrote an example of how you can solve your problem

const schedule = require('node-schedule');
const lowdb = require('lowdb');
const FileSync = require('lowdb/adapters/FileSync');

// Local database for saving jobs state
const db = lowdb(new FileSync('./jobs.json'));
db.defaults({ jobs: [] }).write();

const jobManager = {
  // Retrieving jobs from database and check if has active job
  hasActive: () => db.get('jobs').find((job) => job.isActive).value(),
  // Add active job
  add: () => {
    const [lastJob] = db.get('jobs').takeRight(1).value();
    db.get('jobs').push({ id: lastJob ? lastJob.id + 1 : 0, isActive: true }).write();
  },
  // Finish active job
  finish: () => {
    const [lastJob] = db.get('jobs').takeRight(1).value();
    db.get('jobs').find({ id: lastJob.id }).assign({ isActive: false }).write();
  }
};

// Your heavy async job
const job = async () => {
  if (!jobManager.hasActive()) {
    jobManager.add()
    console.log('Heavy job ...');
    jobManager.finish()
  } else {
    return 'Skipped, because you have active job now. Expect for current job finish';
  }
};

// Recurrence job at every 5 minutes
schedule.scheduleJob('*/5 * * * *', async () => {
  job().then((response) => console.log(response));
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question