@@ -108,33 +108,53 @@ public async Task<bool> RunAsync(CancellationToken cancellationToken = default)
108108 if ( _logger . IsEnabled ( LogLevel . Information ) )
109109 _logger . LogInformation ( "Starting job type {JobName} on machine {MachineName}..." , _jobName , Environment . MachineName ) ;
110110
111- if ( _options . InitialDelay . HasValue && _options . InitialDelay . Value > TimeSpan . Zero )
112- await SystemClock . SleepAsync ( _options . InitialDelay . Value , cancellationToken ) . AnyContext ( ) ;
113-
114- if ( _options . RunContinuous && _options . InstanceCount > 1 ) {
115- var tasks = new List < Task > ( _options . InstanceCount ) ;
116- for ( int i = 0 ; i < _options . InstanceCount ; i ++ ) {
117- tasks . Add ( Task . Run ( async ( ) => {
118- try {
119- var jobInstance = _options . JobFactory ( ) ;
120- await jobInstance . RunContinuousAsync ( _options . Interval , _options . IterationLimit , cancellationToken ) . AnyContext ( ) ;
121- } catch ( TaskCanceledException ) {
122- } catch ( Exception ex ) {
123- if ( _logger . IsEnabled ( LogLevel . Error ) )
124- _logger . LogError ( ex , "Error running job instance: {Message}" , ex . Message ) ;
125- throw ;
126- }
127- } , cancellationToken ) ) ;
128- }
129-
130- await Task . WhenAll ( tasks ) . AnyContext ( ) ;
131- } else if ( _options . RunContinuous && _options . InstanceCount == 1 ) {
132- await job . RunContinuousAsync ( _options . Interval , _options . IterationLimit , cancellationToken ) . AnyContext ( ) ;
133- } else {
134- var result = await job . TryRunAsync ( cancellationToken ) . AnyContext ( ) ;
135- _logger . LogJobResult ( result , _jobName ) ;
111+ var jobLifetime = job as IAsyncLifetime ;
112+ if ( jobLifetime != null ) {
113+ if ( _logger . IsEnabled ( LogLevel . Information ) )
114+ _logger . LogInformation ( "Initializing job lifetime {JobName} on machine {MachineName}..." , _jobName , Environment . MachineName ) ;
115+ await jobLifetime . InitializeAsync ( ) . AnyContext ( ) ;
116+ if ( _logger . IsEnabled ( LogLevel . Information ) )
117+ _logger . LogInformation ( "Done initializing job lifetime {JobName} on machine {MachineName}." , _jobName , Environment . MachineName ) ;
118+ }
136119
137- return result . IsSuccess ;
120+ try {
121+ if ( _options . InitialDelay . HasValue && _options . InitialDelay . Value > TimeSpan . Zero )
122+ await SystemClock . SleepAsync ( _options . InitialDelay . Value , cancellationToken ) . AnyContext ( ) ;
123+
124+ if ( _options . RunContinuous && _options . InstanceCount > 1 ) {
125+ var tasks = new List < Task > ( _options . InstanceCount ) ;
126+ for ( int i = 0 ; i < _options . InstanceCount ; i ++ ) {
127+ tasks . Add ( Task . Run ( async ( ) => {
128+ try {
129+ var jobInstance = _options . JobFactory ( ) ;
130+ await jobInstance . RunContinuousAsync ( _options . Interval , _options . IterationLimit , cancellationToken ) . AnyContext ( ) ;
131+ } catch ( TaskCanceledException ) {
132+ } catch ( Exception ex ) {
133+ if ( _logger . IsEnabled ( LogLevel . Error ) )
134+ _logger . LogError ( ex , "Error running job instance: {Message}" , ex . Message ) ;
135+ throw ;
136+ }
137+ } , cancellationToken ) ) ;
138+ }
139+
140+ await Task . WhenAll ( tasks ) . AnyContext ( ) ;
141+ } else if ( _options . RunContinuous && _options . InstanceCount == 1 ) {
142+ await job . RunContinuousAsync ( _options . Interval , _options . IterationLimit , cancellationToken ) . AnyContext ( ) ;
143+ } else {
144+ var result = await job . TryRunAsync ( cancellationToken ) . AnyContext ( ) ;
145+ _logger . LogJobResult ( result , _jobName ) ;
146+
147+ return result . IsSuccess ;
148+ }
149+ } finally {
150+ var jobDisposable = job as IAsyncDisposable ;
151+ if ( jobDisposable != null ) {
152+ if ( _logger . IsEnabled ( LogLevel . Information ) )
153+ _logger . LogInformation ( "Disposing job lifetime {JobName} on machine {MachineName}..." , _jobName , Environment . MachineName ) ;
154+ await jobDisposable . DisposeAsync ( ) . AnyContext ( ) ;
155+ if ( _logger . IsEnabled ( LogLevel . Information ) )
156+ _logger . LogInformation ( "Done disposing job lifetime {JobName} on machine {MachineName}." , _jobName , Environment . MachineName ) ;
157+ }
138158 }
139159 }
140160
0 commit comments