-
Notifications
You must be signed in to change notification settings - Fork 7.1k
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
fix(core): Ensure graceful shutdown for workers #9547
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,22 +64,22 @@ export class Worker extends BaseCommand { | |
this.logger.info('Stopping n8n...'); | ||
|
||
// Stop accepting new jobs | ||
await Worker.jobQueue.pause(true); | ||
void Worker.jobQueue.pause(true); // do not block so we can report progress | ||
|
||
try { | ||
await this.externalHooks?.run('n8n.stop', []); | ||
|
||
const hardStopTime = Date.now() + this.gracefulShutdownTimeoutInS; | ||
const hardStopTimeMs = Date.now() + this.gracefulShutdownTimeoutInS * 1000; | ||
|
||
// Wait for active workflow executions to finish | ||
let count = 0; | ||
while (Object.keys(Worker.runningJobs).length !== 0) { | ||
if (count++ % 4 === 0) { | ||
const waitLeft = Math.ceil((hardStopTime - Date.now()) / 1000); | ||
const waitLeft = Math.ceil((hardStopTimeMs - Date.now()) / 1000); | ||
this.logger.info( | ||
`Waiting for ${ | ||
Object.keys(Worker.runningJobs).length | ||
} active executions to finish... (wait ${waitLeft} more seconds)`, | ||
} active executions to finish... (max wait ${waitLeft} more seconds)`, | ||
); | ||
} | ||
|
||
|
@@ -483,6 +483,19 @@ export class Worker extends BaseCommand { | |
await this.setupHealthMonitor(); | ||
} | ||
|
||
if (process.stdout.isTTY) { | ||
process.stdin.setRawMode(true); | ||
process.stdin.resume(); | ||
process.stdin.setEncoding('utf8'); | ||
|
||
process.stdin.on('data', (key: string) => { | ||
// ctrl+c | ||
if (key.charCodeAt(0) === 3) { | ||
void this.stopProcess(); | ||
} | ||
}); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could this be moved to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I understand, I think this is only needed for |
||
|
||
// Make sure that the process does not close | ||
await new Promise(() => {}); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we instead update
queue.pause
to pass the second argument (doNotWaitActive
) astrue
instead of leaving a dangling promise here?