And have my cleanup code in this.stop(). If I start this using node myFile.js, it works just fine. If I run it in a meteor process, my callbacks are not triggered by the signals. I can’t find any examples of a meteor specific way of accomplishing this either. Does anyone have any similar experiences, or have successfully accomplished something to the same effect as what I’m trying?
I’ve used the ddp-graceful-shutdown package in many projects before without any issues. Its code is pretty small, so @jlugner might want to take a look at its repository.
I just installed it in a simple project to test it on Galaxy, and it worked!
I’m using the code below that I added in a new file named shutdown-handler.js. The purpose of the package is to shut the DDP connections down gradually, so I’m overriding installSIGTERMHandler() so I can do something before they are closed.
import { Meteor } from 'meteor/meteor';
import { DDPGracefulShutdown } from '@meteorjs/ddp-graceful-shutdown';
class SimpleDDPGracefulShutdown extends DDPGracefulShutdown {
installSIGTERMHandler() {
process.on(
'SIGTERM',
Meteor.bindEnvironment(() => {
const gracePeriod =
process.env.METEOR_SIGTERM_GRACE_PERIOD_SECONDS || 30;
console.log(
`Received SIGTERM. Shutting down in ${gracePeriod} seconds.`
);
this.closeConnections({ log: true });
})
);
}
}
new SimpleDDPGracefulShutdown({
gracePeriodMillis: 1000 * process.env.METEOR_SIGTERM_GRACE_PERIOD_SECONDS,
server: Meteor.server,
}).installSIGTERMHandler();
I’m importing it in main.js, which is my server entry point. And that’s it.
import { Meteor } from 'meteor/meteor';
import { Migrations } from 'meteor/percolate:migrations';
import './shutdown-handler'; //Here
import './db/migrations';
import './tasks/tasks.publications';
import './tasks/tasks.methods';
/**
* This is the server-side entry point
*/
Meteor.startup(() => {
Migrations.migrateTo('latest');
});
When my container is being stopped, I can see the message below on Galaxy logs:
I can’t seem to replicate… Every now and then I reach the callbacks (~10% of the time?), but even in those cases it immediately exits. I’m trying to use it in my background worker. Idea is to let it finish any running jobs when a signal is received, but not start any new. This is virtually what it’s doing, but with sleeps in place of any actual job logic: