-
Notifications
You must be signed in to change notification settings - Fork 1
Add task.log method to allow sending task events #26
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
Open
pksunkara
wants to merge
1
commit into
master
Choose a base branch
from
pavan/jj/nptqxkzm
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,184 @@ | ||
| import { Readable } from 'node:stream'; | ||
|
|
||
| import { TaskForCode } from '../types'; | ||
|
|
||
| import { APIResource } from '../core/resource'; | ||
|
|
||
| export class Task extends APIResource { | ||
| private eventQueue: QueuedEvent[] = []; | ||
| private isProcessing = false; | ||
|
|
||
| private flushTimer?: NodeJS.Timeout; | ||
| private readonly maxBatchSize = 50; | ||
| private readonly flushInterval = 5000; // 5 seconds | ||
|
|
||
| constructor(...args: ConstructorParameters<typeof APIResource>) { | ||
| super(...args); | ||
| this.installProcessHandlers(); | ||
| this.startPeriodicFlush(); | ||
| } | ||
|
|
||
| private installProcessHandlers() { | ||
| const cleanupAndReemit = async (signal: NodeJS.Signals) => { | ||
| await this.cleanupAll(signal); | ||
|
|
||
| // Remove our listeners to restore default behavior, then re-emit signal | ||
| process.removeListener('SIGINT', onSigint); | ||
| process.removeListener('SIGTERM', onSigterm); | ||
| process.removeListener('SIGQUIT', onSigquit); | ||
|
|
||
| try { | ||
| process.kill(process.pid, signal); | ||
| } catch { | ||
| // ignore if process already exiting | ||
| } | ||
| }; | ||
|
|
||
| const onSigint = () => cleanupAndReemit('SIGINT'); | ||
| const onSigterm = () => cleanupAndReemit('SIGTERM'); | ||
| const onSigquit = () => cleanupAndReemit('SIGQUIT'); | ||
|
|
||
| process.once('SIGINT', onSigint); | ||
| process.once('SIGTERM', onSigterm); | ||
| process.once('SIGQUIT', onSigquit); | ||
|
|
||
| process.once('beforeExit', async () => { | ||
| await this.cleanupAll('beforeExit'); | ||
| }); | ||
|
|
||
| process.once('unhandledRejection', async (reason) => { | ||
| console.error('Unhandled rejection:', reason); | ||
| await this.cleanupAll('unhandledRejection'); | ||
| }); | ||
|
|
||
| process.once('uncaughtException', async (err) => { | ||
| console.error('Uncaught exception:', err); | ||
| await this.cleanupAll('uncaughtException'); | ||
| throw err; | ||
| }); | ||
| } | ||
|
|
||
| private async cleanupAll(reason: string) { | ||
| try { | ||
| await this.destroy(); | ||
| } catch (e) { | ||
| console.error(`Failed to flush task events during ${reason}:`, e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Log an event for a task. | ||
| * @param body Parameters for logging a task event | ||
| * @param body.task Task to log the event for | ||
| * @param body.event Event to log | ||
| */ | ||
| log(body: TaskLogParams) { | ||
| this.eventQueue.push({ | ||
| ...body, | ||
| event: { | ||
| ...body.event, | ||
| timestamp: Date.now() / 1000, | ||
| }, | ||
| }); | ||
|
|
||
| // Flush immediately if queue is full | ||
| if (this.eventQueue.length >= this.maxBatchSize) { | ||
| this.flush(); | ||
| } | ||
|
|
||
| return { | ||
| queued: true, | ||
| queueSize: this.eventQueue.length, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Manually flush all queued events. | ||
| */ | ||
| async flush() { | ||
| if (this.isProcessing || this.eventQueue.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| this.isProcessing = true; | ||
|
|
||
| try { | ||
| const batch = this.eventQueue.splice(0, this.maxBatchSize); | ||
| await this.sendBatch(batch); | ||
| } catch (error) { | ||
| console.error('Failed to flush task events:', error); | ||
| } finally { | ||
| this.isProcessing = false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Send a batch of events to the server. | ||
| */ | ||
| private async sendBatch(events: QueuedEvent[]) { | ||
| try { | ||
| const response = await this._client.post< | ||
| Readable, | ||
| { events: TaskLogParams[] } | ||
| >('/bot/task/log', { | ||
| events, | ||
| }); | ||
|
|
||
| return response; | ||
| } catch (error) { | ||
| this.eventQueue.unshift(...events); | ||
|
|
||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Start periodic flushing of queued events. | ||
| */ | ||
| private startPeriodicFlush() { | ||
| this.flushTimer = setInterval(() => { | ||
| this.flush().catch((error) => { | ||
| console.error('Periodic flush of task events failed:', error); | ||
| }); | ||
| }, this.flushInterval); | ||
| } | ||
|
|
||
| /** | ||
| * Stop periodic flushing and flush remaining events. | ||
| */ | ||
| async destroy() { | ||
| if (this.flushTimer) { | ||
| clearInterval(this.flushTimer); | ||
| this.flushTimer = undefined; | ||
| } | ||
|
|
||
| // Flush any remaining events | ||
| while (this.eventQueue.length > 0) { | ||
| await this.flush(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get current queue status. | ||
| */ | ||
| getQueueStatus(): { size: number; isProcessing: boolean } { | ||
| return { | ||
| size: this.eventQueue.length, | ||
| isProcessing: this.isProcessing, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| export interface TaskLogParams { | ||
| task: Pick<TaskForCode, 'id' | 'token'>; | ||
| event: { | ||
| type: string; | ||
| [key: string]: any; | ||
|
Check warning on line 176 in packages/bot/src/resources/task.ts
|
||
| }; | ||
| } | ||
|
|
||
| type QueuedEvent = TaskLogParams & { | ||
| event: { | ||
| timestamp: number; | ||
| }; | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.