-
-
Notifications
You must be signed in to change notification settings - Fork 716
Fix build: restore wa-automate SessionManager + Windows rimraf globs #3329
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
t0g3pii
wants to merge
5
commits into
open-wa:master
Choose a base branch
from
t0g3pii:fix/build-sessionmanager-and-rimraf
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9e7a6e8
fix: restore wa-automate SessionManager + windows rimraf globs
t0g3pii 61fa3b3
fix: validate s3Sync config in wa-automate SessionManager
t0g3pii a3172df
fix: prevent S3 sync timer leaks and overlap
t0g3pii 553988a
fix: make turbo filter scripts cross-platform
t0g3pii 861e128
fix: harden SessionManager stop and S3 startup guard
t0g3pii 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| import { createLogger } from '@open-wa/logger'; | ||
| import { LocalSessionCompression, S3SyncManager } from '@open-wa/session-sync'; | ||
| import type { S3Config } from '@open-wa/session-sync'; | ||
| import type { Config } from '@open-wa/config'; | ||
|
|
||
| type SessionManagerConfig = { | ||
| sessionId: string; | ||
| dataDir: string; | ||
| s3Config?: S3Config; | ||
| syncInterval?: number; | ||
| enableLocalCompression?: boolean; | ||
| enableS3Backup?: boolean; | ||
| }; | ||
|
|
||
| export class SessionManager { | ||
| private config: SessionManagerConfig; | ||
| private logger = createLogger({ component: 'SessionManager' }); | ||
| private localCompression: LocalSessionCompression | null = null; | ||
| private s3Sync: S3SyncManager | null = null; | ||
| private syncTimer: NodeJS.Timeout | null = null; | ||
| private syncInFlight = false; | ||
|
|
||
| constructor(config: SessionManagerConfig) { | ||
| this.config = config; | ||
| this.logger.info('SessionManager initialized', { sessionId: config.sessionId }); | ||
| } | ||
|
|
||
| async start(): Promise<void> { | ||
| if (this.config.enableLocalCompression !== false) { | ||
| this.localCompression = new LocalSessionCompression({ | ||
| sessionPath: this.config.dataDir, | ||
| sessionId: this.config.sessionId, | ||
| intervalMs: this.config.syncInterval || 600_000, | ||
| }); | ||
| await this.localCompression.start(); | ||
| this.logger.info('Local session compression started'); | ||
| } | ||
|
|
||
| // S3 backups upload the `.data.zst` artifact produced by LocalSessionCompression. | ||
| // If local compression is disabled, periodic S3 sync would target a non-existent file. | ||
| if (this.config.enableLocalCompression !== false && this.config.enableS3Backup && this.config.s3Config) { | ||
| this.s3Sync = new S3SyncManager(this.config.s3Config); | ||
| this.startPeriodicSync(); | ||
| this.logger.info('S3 session sync started'); | ||
| } | ||
| } | ||
|
|
||
| async stop(): Promise<void> { | ||
| if (this.localCompression) { | ||
| try { | ||
| await this.localCompression.stop(); | ||
| } catch (error) { | ||
| const message = error instanceof Error ? error.message : String(error); | ||
| this.logger.warn('Local session compression stop failed', { error: message }); | ||
| } | ||
| this.localCompression = null; | ||
| } | ||
|
|
||
| if (this.syncTimer) { | ||
| clearTimeout(this.syncTimer); | ||
| this.syncTimer = null; | ||
| } | ||
| this.syncInFlight = false; | ||
|
|
||
| if (this.s3Sync) { | ||
| this.s3Sync = null; | ||
| } | ||
|
t0g3pii marked this conversation as resolved.
|
||
|
|
||
| this.logger.info('SessionManager stopped'); | ||
| } | ||
|
|
||
| async backupSession(): Promise<string | null> { | ||
| if (!this.s3Sync) { | ||
| this.logger.warn('S3 sync not configured'); | ||
| return null; | ||
| } | ||
|
|
||
| const sessionZstPath = `${this.config.dataDir}/${this.config.sessionId}.data.zst`; | ||
| try { | ||
| const filename = await this.s3Sync.backupSession(sessionZstPath); | ||
| this.logger.info('Session backed up to S3', { filename }); | ||
| return filename; | ||
| } catch (error) { | ||
| const message = error instanceof Error ? error.message : String(error); | ||
| this.logger.error('Failed to backup session to S3', { error: message }); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| async restoreSession(filename: string): Promise<void> { | ||
| if (!this.s3Sync) { | ||
| this.logger.warn('S3 sync not configured'); | ||
| throw new Error('S3 sync not configured for session restore'); | ||
| } | ||
|
|
||
| try { | ||
| await this.s3Sync.restoreSession(filename, this.config.dataDir); | ||
| this.logger.info('Session restored from S3', { filename }); | ||
| } catch (error) { | ||
| const message = error instanceof Error ? error.message : String(error); | ||
| this.logger.error('Failed to restore session from S3', { error: message }); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| async getSessionBackupUrl(filename: string, expiresIn = 3600): Promise<string | null> { | ||
| if (!this.s3Sync) { | ||
| this.logger.warn('S3 sync not configured'); | ||
| return null; | ||
| } | ||
|
|
||
| try { | ||
| const url = await this.s3Sync.getDownloadUrl(filename, expiresIn); | ||
| this.logger.info('Generated session backup URL', { filename, expiresIn }); | ||
| return url; | ||
| } catch (error) { | ||
| const message = error instanceof Error ? error.message : String(error); | ||
| this.logger.error('Failed to generate backup URL', { error: message }); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| private startPeriodicSync(): void { | ||
| if (!this.s3Sync) return; | ||
|
|
||
| const syncInterval = this.config.syncInterval || 600_000; | ||
|
|
||
| // Defensive: stop any previous schedule before starting a new one | ||
| if (this.syncTimer) { | ||
| clearTimeout(this.syncTimer); | ||
| this.syncTimer = null; | ||
| } | ||
|
|
||
| const tick = async () => { | ||
| if (!this.s3Sync) return; | ||
|
|
||
| if (!this.syncInFlight) { | ||
| this.syncInFlight = true; | ||
| try { | ||
| await this.backupSession(); | ||
| } catch (error) { | ||
| const message = error instanceof Error ? error.message : String(error); | ||
| this.logger.error('Periodic sync failed', { error: message }); | ||
| } finally { | ||
| this.syncInFlight = false; | ||
| } | ||
| } | ||
|
|
||
| if (!this.s3Sync) return; | ||
| this.syncTimer = setTimeout(() => void tick(), syncInterval); | ||
| }; | ||
|
|
||
| this.syncTimer = setTimeout(() => void tick(), syncInterval); | ||
| } | ||
|
|
||
| private static parseS3Config(input: unknown): S3Config | null { | ||
| if (!input || typeof input !== 'object') return null; | ||
|
|
||
| const obj = input as Record<string, unknown>; | ||
| const bucket = obj.bucket; | ||
| const region = obj.region; | ||
| const accessKeyId = obj.accessKeyId; | ||
| const secretAccessKey = obj.secretAccessKey; | ||
|
|
||
| const hasRequired = | ||
| typeof bucket === 'string' && | ||
| bucket.length > 0 && | ||
| typeof region === 'string' && | ||
| region.length > 0 && | ||
| typeof accessKeyId === 'string' && | ||
| accessKeyId.length > 0 && | ||
| typeof secretAccessKey === 'string' && | ||
| secretAccessKey.length > 0; | ||
|
|
||
| if (!hasRequired) return null; | ||
|
|
||
| const endpoint = typeof obj.endpoint === 'string' ? obj.endpoint : undefined; | ||
| const host = typeof obj.host === 'string' ? obj.host : undefined; | ||
| const url = typeof obj.url === 'string' ? obj.url : undefined; | ||
|
|
||
| return { bucket, region, accessKeyId, secretAccessKey, endpoint, host, url }; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| static createFromConfig(clientConfig: Config): SessionManager { | ||
| const validatedS3Config = SessionManager.parseS3Config(clientConfig.s3Sync); | ||
| return new SessionManager({ | ||
| sessionId: clientConfig.sessionId || 'session', | ||
| dataDir: clientConfig.sessionDataPath || './.wwebjs', | ||
| s3Config: validatedS3Config ?? undefined, | ||
| syncInterval: clientConfig.s3Sync?.syncInterval, | ||
| enableLocalCompression: clientConfig.s3Sync?.enableLocalCompression !== false, | ||
| enableS3Backup: validatedS3Config !== null, | ||
| }); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
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.