Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ NODE_ENV=production
API_PORT=2785
LOG_LEVEL=info # error | warn | info | debug

# Auto-start previously authenticated sessions on server boot
AUTO_START_SESSIONS=false

# Domain Configuration
DOMAIN=localhost
DASHBOARD_PORT=2886
Expand Down
3 changes: 3 additions & 0 deletions .env.minimal
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
# This is a minimal configuration for development
# and single-session personal bots using SQLite.

# Auto-start previously authenticated sessions on server boot
AUTO_START_SESSIONS=false

# Server
PORT=2785
NODE_ENV=development
Expand Down
41 changes: 39 additions & 2 deletions src/modules/session/session.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import {
BadRequestException,
OnModuleDestroy,
OnModuleInit,
OnApplicationBootstrap,
} from '@nestjs/common';
import { InjectRepository, InjectDataSource } from '@nestjs/typeorm';
import { Repository, In, DataSource } from 'typeorm';
import { Repository, In, Not, IsNull, DataSource } from 'typeorm';
import { Session, SessionStatus } from './entities/session.entity';
import { CreateSessionDto } from './dto';
import { EngineFactory } from '../../engine/engine.factory';
Expand All @@ -25,7 +26,7 @@ interface ReconnectState {
}

@Injectable()
export class SessionService implements OnModuleDestroy, OnModuleInit {
export class SessionService implements OnModuleDestroy, OnModuleInit, OnApplicationBootstrap {
private readonly logger = createLogger('SessionService');

// In-memory map of active engine instances
Expand Down Expand Up @@ -70,6 +71,38 @@ export class SessionService implements OnModuleDestroy, OnModuleInit {
}
}

async onApplicationBootstrap(): Promise<void> {
if (process.env.AUTO_START_SESSIONS !== 'true') return;

const sessions = await this.sessionRepository.find({
where: { phone: Not(IsNull()), status: SessionStatus.DISCONNECTED },
});

if (sessions.length === 0) return;

this.logger.log(`Auto-starting ${sessions.length} previously authenticated session(s)`, {
action: 'auto_start',
count: sessions.length,
});

for (const session of sessions) {
try {
await this.start(session.id);
this.logger.log(`Auto-started session: ${session.name}`, {
sessionId: session.id,
action: 'auto_start_success',
});
} catch (error: unknown) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
this.logger.error(`Auto-start failed for session: ${session.name}`, errorMessage, {
sessionId: session.id,
action: 'auto_start_failed',
});
}
await this.delay(2000);
}
}

async onModuleDestroy(): Promise<void> {
// Clean up all engines on shutdown
for (const [sessionId, engine] of this.engines) {
Expand Down Expand Up @@ -537,4 +570,8 @@ export class SessionService implements OnModuleDestroy, OnModuleInit {
isActive(id: string): boolean {
return this.engines.has(id);
}

private delay(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}
}