Tuesday, 23 April 2019

How to break Async series on SIGTERM?

Assume I have the following scenario -

 async.series(
            [
                function (cbi) {
                    students.getAll('student',
                        function (err, response) {
                            if (err) {
                                logger.error(err);
                            }
                            cbi(err, response);
                        });
                },
                function (cbi) {
                    students.deleteAll('student',
                        function (err, response) {
                            if (err) {
                                logger.error(err);
                            }
                            cbi(err, response);
                        });
                },
                function (cbi) {
                    teachers.getAll('teacher',
                        function (err, response) {
                            if (err) {
                                logger.error(err);
                            }
                            cbi(err, response);
                        });
                },
                function (cbi) {
                    teachers.deleteAll('teacher',
                        function (err, response) {
                            if (err) {
                                logger.error(err);
                            }
                            cbi(err, response);
                        });
                },
        );
    }
],
);

And I want a graceful cleanup when SIGTERM is sent. That is a clean up of all the students or all teachers whichever is in progress when the signal was sent should complete and the next one should not begin.

function (cbi) {
                    students.getAll('student',
                        function (err, response) {
                            if (err || GLOBAL_VAR_SIGTERM === true) {
                                logger.error(err);
                            }
                            cbi(err, response);
                        });
                },

I was thinking that I should set a global variable to keep track of the SIGTERM signal.

process.on('SIGTERM', function onSigterm () {
  GLOBAL_VAR_SIGTERM = true;
}

Is there any better way to break an async series to break a SIGTERM signal?



from How to break Async series on SIGTERM?

No comments:

Post a Comment