1- import { Injectable } from '@nestjs/common' ;
1+ import { Injectable , Logger } from '@nestjs/common' ;
22import { Worker } from 'bullmq' ;
33import { fdir } from "fdir" ;
44import * as fs from "fs" ;
55import * as YAML from 'yaml'
66import * as process from 'child_process'
7+ import * as mainProcess from 'process'
78import * as path from 'path'
89import { BackendConfigDto } from "./dto/Backend-config.dto" ;
910import { BackendResultDto } from "./dto/Backend-result.dto" ;
@@ -12,20 +13,25 @@ import { ConfigService } from '@nestjs/config';
1213
1314@Injectable ( )
1415export class AppService {
16+ private readonly logger = new Logger ( AppService . name ) ;
1517 backendsConfig :Array < BackendConfigDto > = [ ]
18+
1619 constructor ( private readonly configService : ConfigService ) { }
1720
1821 async runDaemon ( ) {
1922 await this . loadConfig ( )
2023 const backendResults = [ ]
24+
2125 const worker = new Worker ( 'backend' , async job => {
22- console . log ( job . name )
26+
2327 let results = [ ]
2428 let gStatus = 0 ;
29+ this . logger . log ( 'start daemon' )
2530 for await ( const backend of this . backendsConfig ) {
2631 if ( backend . active === 1 ) {
32+ this . logger . log ( 'Execute backend ' + backend . name )
2733 var task = backend . actions [ job . name ]
28- console . log ( backend . path + '/bin/' + task . exec )
34+ this . logger . debug ( backend . path + '/bin/' + task . exec )
2935 const out = process . spawnSync ( backend . path + '/bin/' + task . exec , [ ] , {
3036 input : JSON . stringify ( job . data )
3137 } )
@@ -41,23 +47,32 @@ export class AppService {
4147 }
4248 }
4349 }
44- console . log ( 'fini' )
45- console . log ( results )
50+ this . logger . debug ( results )
4651 return { jobId :job . id , status :gStatus , data :results }
4752 } , { connection :this . configService . get ( 'redis' ) } ) ;
4853 }
4954
5055 async loadConfig ( ) {
56+ this . logger . log ( 'load backends config' )
5157 const crawler = new fdir ( )
5258 . withBasePath ( )
5359 . filter ( ( path , isDirectory ) => path . endsWith ( ".yml" ) )
5460 const files = crawler . crawl ( this . configService . get ( 'backendsPath' ) ) . sync ( ) ;
5561 for await ( const element of files ) {
5662 const file = fs . readFileSync ( element , 'utf8' )
5763 const config = YAML . parse ( file )
64+ try {
65+ const verif = plainToInstance ( BackendConfigDto , config )
66+ } catch ( e ) {
67+ const erreurs = errors . map ( ( e ) => e . toString ( ) ) . join ( ', ' )
68+ this . logger . fatal ( `Erreur fichier de configuration : ${ element } : ${ erreurs } ` )
69+ mainProcess . exit ( 1 )
70+ }
5871 config . path = path . dirname ( element )
5972 this . backendsConfig . push ( config )
73+ this . logger . log ( 'Load ' + config . name )
6074 }
61- console . log ( this . backendsConfig )
75+ this . logger . debug ( this . backendsConfig )
76+
6277 }
6378}
0 commit comments