Drain jobs cleanly on shutdown

This commit is contained in:
Alvaro 2023-02-24 12:03:17 -07:00 committed by GitHub
parent a83a85d557
commit b5849f872a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 301 additions and 30 deletions

View file

@ -272,6 +272,8 @@ export class ConversationModel extends window.Backbone
private privVerifiedEnum?: typeof window.textsecure.storage.protocol.VerifiedStatus;
private isShuttingDown = false;
override defaults(): Partial<ConversationAttributesType> {
return {
unreadCount: 0,
@ -3580,14 +3582,19 @@ export class ConversationModel extends window.Backbone
return validateConversation(attributes);
}
queueJob<T>(
async queueJob<T>(
name: string,
callback: (abortSignal: AbortSignal) => Promise<T>
): Promise<T> {
this.jobQueue = this.jobQueue || new PQueue({ concurrency: 1 });
const logId = `conversation.queueJob(${this.idForLogging()}, ${name})`;
if (this.isShuttingDown) {
log.warn(`${logId}: shutting down, can't accept more work`);
throw new Error(`${logId}: shutting down, can't accept more work`);
}
this.jobQueue = this.jobQueue || new PQueue({ concurrency: 1 });
const taskWithTimeout = createTaskWithTimeout(callback, logId);
const abortController = new AbortController();
@ -5683,6 +5690,30 @@ export class ConversationModel extends window.Backbone
return this.get('storySendMode') ?? StorySendMode.IfActive;
}
async shutdownJobQueue(): Promise<void> {
log.info(`conversation ${this.idForLogging()} jobQueue shutdown start`);
if (!this.jobQueue) {
log.info(`conversation ${this.idForLogging()} no jobQueue to shutdown`);
return;
}
// If the queue takes more than 10 seconds to get to idle, we force it by setting
// isShuttingDown = true which will reject incoming requests.
const to = setTimeout(() => {
log.warn(
`conversation ${this.idForLogging()} jobQueue stop accepting new work`
);
this.isShuttingDown = true;
}, 10 * SECOND);
await this.jobQueue.onIdle();
this.isShuttingDown = true;
clearTimeout(to);
log.info(`conversation ${this.idForLogging()} jobQueue shutdown complete`);
}
}
window.Whisper.Conversation = ConversationModel;