1+ import { Logger } from '@nestjs/common' ;
12import { BuilderContext } from './context' ;
23import { BuildNode , BuildSequence , BuildStep } from './types' ;
4+ import { v4 as uuidv4 } from 'uuid' ;
35
46export class BuildSequenceExecutor {
57 constructor ( private context : BuilderContext ) { }
68
9+ private logger : Logger = new Logger ( `BuildSequenceExecutor-${ uuidv4 ( ) } ` ) ;
710 private async executeNode ( node : BuildNode ) : Promise < void > {
811 try {
912 if ( this . context . getState ( ) . completed . has ( node . id ) ) {
1013 return ;
1114 }
1215
1316 if ( ! this . context . canExecute ( node . id ) ) {
14- console . log ( `Waiting for dependencies: ${ node . requires ?. join ( ', ' ) } ` ) ;
15- await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ; // 添加小延迟
17+ this . logger . log (
18+ `Waiting for dependencies: ${ node . requires ?. join ( ', ' ) } ` ,
19+ ) ;
20+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
1621 return ;
1722 }
1823
19- await this . context . run ( node . id ) ;
24+ const dependenciesResults = node . requires ?. map ( ( depId ) =>
25+ this . context . getResult ( depId ) ,
26+ ) ;
27+
28+ this . logger . log (
29+ `Executing node ${ node . id } with dependencies:` ,
30+ dependenciesResults ,
31+ ) ;
32+
33+ await this . context . run ( node . id , dependenciesResults ) ;
2034 } catch ( error ) {
21- console . error ( `Error executing node ${ node . id } :` , error ) ;
35+ this . logger . error ( `Error executing node ${ node . id } :` , error ) ;
2236 throw error ;
2337 }
2438 }
2539
2640 private async executeStep ( step : BuildStep ) : Promise < void > {
27- console . log ( `Executing build step: ${ step . id } ` ) ;
41+ this . logger . log ( `Executing build step: ${ step . id } ` ) ;
2842
2943 if ( step . parallel ) {
3044 let remainingNodes = [ ...step . nodes ] ;
@@ -84,7 +98,7 @@ export class BuildSequenceExecutor {
8498
8599 if ( ! this . context . getState ( ) . completed . has ( node . id ) ) {
86100 // TODO: change to error log
87- console . warn (
101+ this . logger . warn (
88102 `Failed to execute node ${ node . id } after ${ maxRetries } attempts` ,
89103 ) ;
90104 }
@@ -93,7 +107,7 @@ export class BuildSequenceExecutor {
93107 }
94108
95109 async executeSequence ( sequence : BuildSequence ) : Promise < void > {
96- console . log ( `Starting build sequence: ${ sequence . id } ` ) ;
110+ this . logger . log ( `Starting build sequence: ${ sequence . id } ` ) ;
97111
98112 for ( const step of sequence . steps ) {
99113 await this . executeStep ( step ) ;
@@ -104,7 +118,7 @@ export class BuildSequenceExecutor {
104118
105119 if ( incompletedNodes . length > 0 ) {
106120 // TODO: change to error log
107- console . warn (
121+ this . logger . warn (
108122 `Step ${ step . id } failed to complete nodes: ${ incompletedNodes
109123 . map ( ( n ) => n . id )
110124 . join ( ', ' ) } `,
@@ -113,7 +127,7 @@ export class BuildSequenceExecutor {
113127 }
114128 }
115129
116- console . log ( `Build sequence completed: ${ sequence . id } ` ) ;
117- console . log ( 'Final state:' , this . context . getState ( ) ) ;
130+ this . logger . log ( `Build sequence completed: ${ sequence . id } ` ) ;
131+ this . logger . log ( 'Final state:' , this . context . getState ( ) ) ;
118132 }
119133}
0 commit comments