@@ -72,7 +72,9 @@ public static async Task Run(JobBase job, string[] commandLineArgs)
7272 loggerFactory = ConfigureLogging ( job ) ;
7373
7474 var runContinuously = ! JobConfigurationManager . TryGetBoolArgument ( jobArgsDictionary , JobArgumentNames . Once ) ;
75+ var reinitializeAfterSeconds = JobConfigurationManager . TryGetIntArgument ( jobArgsDictionary , JobArgumentNames . ReinitializeAfterSeconds ) ;
7576 var sleepDuration = JobConfigurationManager . TryGetIntArgument ( jobArgsDictionary , JobArgumentNames . Sleep ) ; // sleep is in milliseconds
77+
7678 if ( ! sleepDuration . HasValue )
7779 {
7880 sleepDuration = JobConfigurationManager . TryGetIntArgument ( jobArgsDictionary , JobArgumentNames . Interval ) ;
@@ -88,12 +90,19 @@ public static async Task Run(JobBase job, string[] commandLineArgs)
8890 sleepDuration = 5000 ;
8991 }
9092
93+ if ( ! reinitializeAfterSeconds . HasValue )
94+ {
95+ _logger . LogInformation (
96+ $ "{ JobArgumentNames . ReinitializeAfterSeconds } command line argument is not provided or is not a valid integer. " +
97+ "The job will reinitialize on every iteration" ) ;
98+ }
99+
91100 // Ensure that SSLv3 is disabled and that Tls v1.2 is enabled.
92101 ServicePointManager . SecurityProtocol &= ~ SecurityProtocolType . Ssl3 ;
93102 ServicePointManager . SecurityProtocol |= SecurityProtocolType . Tls12 ;
94103
95104 // Run the job loop
96- await JobLoop ( job , runContinuously , sleepDuration . Value , jobArgsDictionary ) ;
105+ await JobLoop ( job , runContinuously , sleepDuration . Value , reinitializeAfterSeconds , jobArgsDictionary ) ;
97106 }
98107 catch ( Exception ex )
99108 {
@@ -123,22 +132,34 @@ private static string PrettyPrintTime(double milliSeconds)
123132 $ "'{ milliSeconds : F3} ' ms (or '{ seconds : F3} ' seconds or '{ minutes : F3} ' mins)";
124133 }
125134
126- private static async Task JobLoop ( JobBase job , bool runContinuously , int sleepDuration , IDictionary < string , string > jobArgsDictionary )
135+ private static async Task JobLoop (
136+ JobBase job ,
137+ bool runContinuously ,
138+ int sleepDuration ,
139+ int ? reinitializeAfterSeconds ,
140+ IDictionary < string , string > jobArgsDictionary )
127141 {
128142 // Run the job now
129143 var stopWatch = new Stopwatch ( ) ;
144+ Stopwatch timeSinceInitialization = null ;
130145
131146 while ( true )
132147 {
133148 _logger . LogInformation ( "Running {RunType}" , ( runContinuously ? " continuously..." : " once..." ) ) ;
134149 _logger . LogInformation ( "SleepDuration is {SleepDuration}" , PrettyPrintTime ( sleepDuration ) ) ;
135150 _logger . LogInformation ( "Job run started..." ) ;
136-
151+
137152 var initialized = false ;
138153 stopWatch . Restart ( ) ;
154+
139155 try
140156 {
141- job . Init ( jobArgsDictionary ) ;
157+ if ( ShouldInitialize ( reinitializeAfterSeconds , timeSinceInitialization ) )
158+ {
159+ job . Init ( jobArgsDictionary ) ;
160+ timeSinceInitialization = Stopwatch . StartNew ( ) ;
161+ }
162+
142163 initialized = true ;
143164
144165 await job . Run ( ) ;
@@ -169,5 +190,23 @@ private static async Task JobLoop(JobBase job, bool runContinuously, int sleepDu
169190 await Task . Delay ( sleepDuration ) ;
170191 }
171192 }
193+
194+ private static bool ShouldInitialize ( int ? reinitializeAfterSeconds , Stopwatch timeSinceInitialization )
195+ {
196+ // If there is no wait time between reinitializations, always reinitialize.
197+ if ( ! reinitializeAfterSeconds . HasValue )
198+ {
199+ return true ;
200+ }
201+
202+ // A null time since last initialization indicates that the job hasn't been initialized yet.
203+ if ( timeSinceInitialization == null )
204+ {
205+ return true ;
206+ }
207+
208+ // Otherwise, only reinitialize if the reinitialization threshold has been reached.
209+ return ( timeSinceInitialization . Elapsed . TotalSeconds > reinitializeAfterSeconds . Value ) ;
210+ }
172211 }
173212}
0 commit comments