-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: improve worker reliability in various means
Squashed commit of the following: commit 06edb2f Author: Wojtek Majewski <[email protected]> Date: Tue Jan 14 13:55:09 2025 +0100 refactor(worker): Introduce WorkerState enum for improved state management Replace string literals with strongly-typed WorkerState enum to enhance type safety and readability of worker state transitions. Modify worker state checks and assignments to use the new enum, improving code maintainability and reducing potential runtime errors. - Introduced WorkerState enum with states: Idle, Starting, Running, Stopping - Replaced string literal state checks with enum comparisons - Updated worker state assignments to use enum values - Enhanced error handling in start method by throwing an error instead of silently returning commit 78eb845 Author: Wojtek Majewski <[email protected]> Date: Tue Jan 14 13:20:26 2025 +0100 refactor(executor): Improve execution and lifecycle management Refactor MessageExecutor and ExecutionController to enhance execution tracking and error handling: - Introduce Promise.withResolvers for better promise management - Move executor tracking and semaphore release to more robust lifecycle - Ensure cleanup happens consistently via finally() handler - Add hasStarted flag to prevent multiple execution attempts commit 78bb094 Author: Wojtek Majewski <[email protected]> Date: Tue Jan 14 12:48:33 2025 +0100 feat(supaworker): Implement batch archiving mechanism for message processing Enhance message handling with BatchArchiver class to support batched message archiving. Added methods for adding messages, scheduling archives, and flushing pending messages. Updated ExecutionController and MessageExecutor to integrate batch archiving functionality. Included a temporary helper function for testing batch archiver synchronization. commit ff168dd Author: Wojtek Majewski <[email protected]> Date: Tue Jan 14 12:23:36 2025 +0100 feat(worker): Add batch archiving and improve execution logging Implement BatchArchiver for efficient message batch processing Enhance ExecutionController with batch archiving and detailed logging Update message execution flow to support new archiving mechanism - Create BatchArchiver class for configurable batch message archiving - Modify ExecutionController to integrate BatchArchiver - Add logging for message execution start and synchronous failure - Improve type generics for better type safety commit 51535f9 Author: Wojtek Majewski <[email protected]> Date: Tue Jan 14 09:24:06 2025 +0100 feat(supaworker): adjust max concurrency and delay settings Modify Supabase worker function to: - Increase max concurrent workers from 20 to 40 - Reduce delay from random duration to fixed 100ms - Remove unnecessary sequence creation and extra nextval call commit 0d466ee Author: Wojtek Majewski <[email protected]> Date: Mon Jan 13 23:51:46 2025 +0100 chore(config): Increase Postgres connection pool settings Doubled default pool size and max client connections for improved database performance
- Loading branch information
Showing
12 changed files
with
328 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { Queue } from './Queue.ts'; | ||
import { Json } from './types.ts'; | ||
|
||
interface BatchConfig { | ||
batchSize?: number; | ||
timeoutMs?: number; | ||
} | ||
|
||
/** | ||
* A class that manages the archiving of messages in batches. | ||
*/ | ||
export class BatchArchiver<MessagePayload extends Json> { | ||
private pending = new Set<number>(); | ||
private timeoutId?: number; | ||
private config: Required<BatchConfig>; | ||
|
||
constructor(private queue: Queue<MessagePayload>, config: BatchConfig = {}) { | ||
this.config = { | ||
batchSize: 100, | ||
timeoutMs: 1000, | ||
...config, | ||
}; | ||
} | ||
|
||
/** | ||
* Adds a message ID to the pending set and schedules an archive for the next batch. | ||
* @param msgId The message ID to add to the pending set | ||
* @returns Promise that resolves when the message has been added to the pending set | ||
*/ | ||
async add(msgId: number): Promise<void> { | ||
this.pending.add(msgId); | ||
await this.archiveImmediatelyOrSchedule(); | ||
} | ||
|
||
/** | ||
* Clears any pending timeout. | ||
*/ | ||
private clearTimeout() { | ||
if (this.timeoutId) { | ||
clearTimeout(this.timeoutId); | ||
this.timeoutId = undefined; | ||
} | ||
} | ||
|
||
/** | ||
* Archives the current batch of pending messages immediately if the batch size | ||
* is less than or equal to the configured batch size. | ||
* | ||
* Otherwise, schedules an archive for the next batch. | ||
*/ | ||
private async archiveImmediatelyOrSchedule(): Promise<void> { | ||
if (this.pending.size >= this.config.batchSize) { | ||
this.clearTimeout(); | ||
await this.archiveBatch(); | ||
} else { | ||
this.setupTimeout(); | ||
} | ||
} | ||
|
||
/** | ||
* Sets up a timeout to archive the current batch of pending messages. | ||
* | ||
* If the timeout is already set, it will clear the existing timeout. | ||
*/ | ||
private setupTimeout() { | ||
if (this.timeoutId) return; | ||
|
||
this.timeoutId = setTimeout(async () => { | ||
this.clearTimeout(); | ||
try { | ||
await this.archiveBatch(); | ||
} catch (error) { | ||
console.error('Timeout-triggered archive failed:', error); | ||
} | ||
}, this.config.timeoutMs); | ||
} | ||
|
||
/** | ||
* Archives the current batch of pending message IDs using the queue. | ||
* Clears the pending set after successful archival. | ||
* | ||
* @throws Will throw an error if archiving fails | ||
* @returns Promise that resolves when the batch has been archived | ||
* @private | ||
*/ | ||
private async archiveBatch(): Promise<void> { | ||
if (this.pending.size === 0) return; | ||
|
||
const batch = Array.from(this.pending); | ||
|
||
try { | ||
await this.queue.archiveBatch(batch); | ||
this.pending.clear(); | ||
} catch (error) { | ||
console.error('Failed to archive batch:', error); | ||
throw error; | ||
} | ||
} | ||
|
||
/** | ||
* Archives all pending messages immediately and cleans up any scheduled timeouts. | ||
* | ||
* Used during shutdown to ensure all messages are archived before stopping. | ||
* @returns Promise that resolves when all pending messages have been archived | ||
*/ | ||
async flush(): Promise<void> { | ||
this.clearTimeout(); | ||
await this.archiveBatch(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.