1- import { Injectable , Logger , OnModuleInit } from '@nestjs/common' ;
1+ import { Injectable , Logger , OnModuleInit , OnApplicationBootstrap } from '@nestjs/common' ;
22import { ConfigService } from '@nestjs/config' ;
33import { Job , Worker } from 'bullmq' ;
44import { ActionType , BackendConfigDto } from './_dto/backend-config.dto' ;
@@ -7,32 +7,47 @@ import executorTask from '../_common/tasks/executor.task';
77import { BackendResultInterface } from './_interfaces/backend-result.interface' ;
88import { ExecutorConfigInterface } from '~/_common/interfaces/executor-config.interface' ;
99import { join } from 'path' ;
10+ import { ExecutorInterface } from './executors.interface' ;
11+ import { ListBackendsExecutor } from './_executors/list-backends.executor' ;
1012
1113@Injectable ( )
12- export class BackendRunnerService implements OnModuleInit {
13- private readonly backendsConfig : BackendConfigDto [ ] = [ ] ;
14- private readonly logger = new Logger ( BackendRunnerService . name ) ;
14+ export class BackendRunnerService implements OnApplicationBootstrap , OnModuleInit {
15+ private readonly _backendsConfig : BackendConfigDto [ ] = [ ] ;
16+ private readonly _logger = new Logger ( BackendRunnerService . name ) ;
17+
18+ protected executors : Map < string , ExecutorInterface > = new Map < string , ExecutorInterface > ( ) ;
1519
1620 public constructor ( private readonly config : ConfigService ) {
17- this . backendsConfig = this . config . get < BackendConfigDto [ ] > ( 'backendsConfig' ) ;
21+ this . _backendsConfig = this . config . get < BackendConfigDto [ ] > ( 'backendsConfig' ) ;
22+ }
23+
24+ public get backendsConfig ( ) {
25+ return this . _backendsConfig ;
26+ }
27+
28+ public get logger ( ) {
29+ return this . _logger ;
1830 }
1931
2032 public async onModuleInit ( ) {
21- this . logger . log ( 'BackendRunnerService initialized' ) ;
33+ this . executors . set ( ActionType . LIST_BACKENDS , new ListBackendsExecutor ( this ) ) ;
34+ this . logger . log ( 'OnModuleInit initialized 🔴' ) ;
35+ }
2236
37+ public async onApplicationBootstrap ( ) {
2338 const worker = new Worker (
2439 this . config . get < string > ( 'nameQueue' ) ,
2540 async ( job ) => {
2641 let status = 0 ;
2742 const data = [ ] ;
28- for await ( const backend of this . backendsConfig ) {
43+ for await ( const backend of this . _backendsConfig ) {
2944 switch ( job . name ) {
30- case ActionType . LISTBACKEND : {
45+ case ActionType . LIST_BACKENDS : {
3146 return await this . listBackends ( job ) ;
3247 }
3348
3449 default : {
35- if ( backend . active !== 1 ) {
50+ if ( ! backend . active ) {
3651 this . logger . warn ( `backend ${ backend . name } is not active` ) ;
3752 continue ;
3853 }
@@ -59,24 +74,44 @@ export class BackendRunnerService implements OnModuleInit {
5974 } ,
6075 ) ;
6176 await worker . run ( ) ;
77+ this . logger . log ( 'OnApplicationBootstrap initialized 🔴' ) ;
6278 }
6379
6480 public async listBackends ( job : Job ) {
6581 this . logger . log ( 'execute LISTBACKEND' ) ;
6682 return {
6783 status : 0 ,
6884 jobId : job . id ,
69- data : this . backendsConfig ,
85+ data : this . _backendsConfig ,
7086 } ;
7187 }
7288
7389 public async executeBackend ( job : Job , backend : BackendConfigDto ) : Promise < BackendResultInterface > {
7490 const process = await executorTask ( join ( backend . path , 'bin' , backend . actions [ job . name ] . exec ) , job , {
7591 ...this . config . get < ExecutorConfigInterface > ( 'backendExecutorConfig' ) ,
7692 } ) ;
77- return {
78- backend : backend . name ,
79- ...process ,
80- } ;
93+ try {
94+ if ( process . status !== 0 ) {
95+ return {
96+ backend : backend . name ,
97+ status : process . status ,
98+ error : JSON . parse ( process . output ) ,
99+ } ;
100+ }
101+ return {
102+ backend : backend . name ,
103+ status : process . status ,
104+ output : JSON . parse ( process . output ) ,
105+ } ;
106+ } catch ( e ) {
107+ return {
108+ backend : backend . name ,
109+ status : process . status ,
110+ error : {
111+ status : process . status ,
112+ message : process . error ,
113+ } ,
114+ } ;
115+ }
81116 }
82117}
0 commit comments