Пример #1
0
        private static async Task JobLoop(
            JobBase job,
            bool runContinuously,
            int sleepDuration,
            int?reinitializeAfterSeconds,
            IDictionary <string, string> jobArgsDictionary)
        {
            // Run the job now
            var       stopWatch = new Stopwatch();
            Stopwatch timeSinceInitialization = null;

            while (true)
            {
                _logger.LogInformation("Running {RunType}", (runContinuously ? " continuously..." : " once..."));
                _logger.LogInformation("SleepDuration is {SleepDuration}", PrettyPrintTime(sleepDuration));
                _logger.LogInformation("Job run started...");

                var initialized = false;
                stopWatch.Restart();

                try
                {
                    if (ShouldInitialize(reinitializeAfterSeconds, timeSinceInitialization))
                    {
                        job.Init(ServiceContainer, jobArgsDictionary);
                        timeSinceInitialization = Stopwatch.StartNew();
                    }

                    initialized = true;

                    await job.Run();

                    _logger.LogInformation(JobSucceeded);
                }
                catch (Exception e)
                {
                    _logger.LogError("{JobState}: {Exception}", initialized ? JobFailed : JobUninitialized, e);
                }
                finally
                {
                    _logger.LogInformation("Job run ended...");
                    stopWatch.Stop();
                    _logger.LogInformation("Job run took {RunDuration}", PrettyPrintTime(stopWatch.ElapsedMilliseconds));
                }

                if (!runContinuously)
                {
                    // It is ok that we do not flush the logs here.
                    // Logs will be flushed at the end of Run().
                    break;
                }

                // Wait for <sleepDuration> milliSeconds and run the job again
                _logger.LogInformation("Will sleep for {SleepDuration} before the next Job run", PrettyPrintTime(sleepDuration));

                await Task.Delay(sleepDuration);
            }
        }
Пример #2
0
        private static async Task JobLoop(JobBase job, bool runContinuously, int sleepDuration, bool consoleLogOnly, IDictionary <string, string> jobArgsDictionary)
        {
            // Run the job now
            var stopWatch = new Stopwatch();

            while (true)
            {
                Trace.WriteLine("Running " + (runContinuously ? " continuously..." : " once..."));
                Trace.WriteLine("SleepDuration is " + PrettyPrintTime(sleepDuration));
                Trace.TraceInformation("Job run started...");

                // Force a flush here to create a blob corresponding to run indicating that the run has started
                job.JobTraceListener.Flush();

                stopWatch.Restart();
                var initialized = job.Init(jobArgsDictionary);
                var succeeded   = initialized && await job.Run();

                stopWatch.Stop();

                Trace.WriteLine("Job run ended...");
                if (initialized)
                {
                    Trace.TraceInformation("Job run took {0}", PrettyPrintTime(stopWatch.ElapsedMilliseconds));

                    if (succeeded)
                    {
                        Trace.TraceInformation(JobSucceeded);
                    }
                    else
                    {
                        Trace.TraceWarning(JobFailed);
                    }
                }
                else
                {
                    Trace.TraceWarning(JobUninitialized);
                }

                if (!runContinuously)
                {
                    // It is ok that we do not flush the logs here.
                    // Logs will be flushed at the end of Run().
                    break;
                }

                // Wait for <sleepDuration> milliSeconds and run the job again
                Trace.TraceInformation("Will sleep for {0} before the next Job run", PrettyPrintTime(sleepDuration));

                // Flush all the logs for this run
                job.JobTraceListener.Close();
                Thread.Sleep(sleepDuration);

                SetJobTraceListener(job, consoleLogOnly, jobArgsDictionary);
            }
        }
Пример #3
0
        private static async Task <string> JobLoop(JobBase job, bool runContinuously, int sleepDuration, bool consoleLogOnly)
        {
            // Run the job now
            var  stopWatch = new Stopwatch();
            bool success;

            while (true)
            {
                Trace.WriteLine("Running " + (runContinuously ? " continuously..." : " once..."));
                Trace.WriteLine("SleepDuration is " + PrettyPrintTime(sleepDuration));
                Trace.WriteLine("Job run started...");

                // Force a flush here to create a blob corresponding to run indicating that the run has started
                job.JobTraceListener.Flush();

                stopWatch.Restart();
                success = await job.Run();

                stopWatch.Stop();

                Trace.WriteLine("Job run ended...");
                Trace.TraceInformation("Job run took {0}", PrettyPrintTime(stopWatch.ElapsedMilliseconds));
                if (success)
                {
                    Trace.TraceInformation(_jobSucceeded);
                }
                else
                {
                    Trace.TraceWarning(_jobFailed);
                }

                // At this point, FlushAll is not called, So, what happens when the job is run only once?
                // Since, FlushAll is called right at the end of the program, this is no issue
                if (!runContinuously)
                {
                    break;
                }

                // Wait for <sleepDuration> milliSeconds and run the job again
                Trace.WriteLine(string.Format("Will sleep for {0} before the next Job run", PrettyPrintTime(sleepDuration)));

                // Flush All the logs for this run
                job.JobTraceListener.Close();

                // Use Console.WriteLine when you don't want it to be logged in Azure blobs
                Console.WriteLine("Sleeping for {0} before the next job run", PrettyPrintTime(sleepDuration));
                Thread.Sleep(sleepDuration);

                SetJobTraceListener(job, consoleLogOnly);
            }

            return(success ? _jobSucceeded : _jobFailed);
        }
Пример #4
0
        private static async Task<string> JobLoop(JobBase job, bool runContinuously, int sleepDuration, bool consoleLogOnly)
        {
            // Run the job now
            var stopWatch = new Stopwatch();
            bool success;

            while (true)
            {
                Trace.WriteLine("Running " + (runContinuously ? " continuously..." : " once..."));
                Trace.WriteLine("SleepDuration is " + PrettyPrintTime(sleepDuration));
                Trace.WriteLine("Job run started...");

                // Force a flush here to create a blob corresponding to run indicating that the run has started
                job.JobTraceListener.Flush();

                stopWatch.Restart();
                success = await job.Run();
                stopWatch.Stop();

                Trace.WriteLine("Job run ended...");
                Trace.TraceInformation("Job run took {0}", PrettyPrintTime(stopWatch.ElapsedMilliseconds));
                if(success)
                {
                    Trace.TraceInformation(_jobSucceeded);
                }
                else
                {
                    Trace.TraceWarning(_jobFailed);
                }

                // At this point, FlushAll is not called, So, what happens when the job is run only once?
                // Since, FlushAll is called right at the end of the program, this is no issue
                if (!runContinuously)
                {
                    break;
                }

                // Wait for <sleepDuration> milliSeconds and run the job again
                Trace.WriteLine(string.Format("Will sleep for {0} before the next Job run", PrettyPrintTime(sleepDuration)));

                // Flush All the logs for this run
                job.JobTraceListener.Close();

                // Use Console.WriteLine when you don't want it to be logged in Azure blobs
                Console.WriteLine("Sleeping for {0} before the next job run", PrettyPrintTime(sleepDuration));
                Thread.Sleep(sleepDuration);

                SetJobTraceListener(job, consoleLogOnly);
            }

            return success ? _jobSucceeded : _jobFailed;
        }
Пример #5
0
        private static async Task <int> JobLoop(
            JobBase job,
            bool runContinuously,
            int sleepDuration,
            int?reinitializeAfterSeconds,
            IDictionary <string, string> jobArgsDictionary)
        {
            // Run the job now
            var       stopWatch = new Stopwatch();
            Stopwatch timeSinceInitialization = null;

            int exitCode = 0;

            // This tells Application Insights that, even though a heartbeat is reported,
            // the state of the application is unhealthy when the exitcode is different from zero.
            // The heartbeat metadata is enriched with the job loop exit code.
            _applicationInsightsConfiguration.DiagnosticsTelemetryModule?.AddOrSetHeartbeatProperty(
                HeartbeatProperty_JobLoopExitCode,
                exitCode.ToString(),
                isHealthy: exitCode == 0);

            while (true)
            {
                _logger.LogInformation("Running {RunType}", (runContinuously ? " continuously..." : " once..."));
                _logger.LogInformation("SleepDuration is {SleepDuration}", PrettyPrintTime(sleepDuration));
                _logger.LogInformation("Job run started...");

                var initialized = false;
                stopWatch.Restart();

                try
                {
                    if (ShouldInitialize(reinitializeAfterSeconds, timeSinceInitialization))
                    {
                        job.Init(ServiceContainer, jobArgsDictionary);
                        timeSinceInitialization = Stopwatch.StartNew();
                    }

                    initialized = true;

                    await job.Run();

                    exitCode = 0;
                    _logger.LogInformation(JobSucceeded);
                }
                catch (Exception e)
                {
                    exitCode = 1;
                    _logger.LogError("{JobState}: {Exception}", initialized ? JobFailed : JobUninitialized, e);
                }
                finally
                {
                    _logger.LogInformation("Job run ended...");
                    stopWatch.Stop();
                    _logger.LogInformation("Job run took {RunDuration}", PrettyPrintTime(stopWatch.ElapsedMilliseconds));

                    _applicationInsightsConfiguration.DiagnosticsTelemetryModule?.SetHeartbeatProperty(
                        HeartbeatProperty_JobLoopExitCode,
                        exitCode.ToString(),
                        isHealthy: exitCode == 0);
                }

                if (!runContinuously)
                {
                    // It is ok that we do not flush the logs here.
                    // Logs will be flushed at the end of Run().
                    break;
                }

                // Wait for <sleepDuration> milliSeconds and run the job again
                _logger.LogInformation("Will sleep for {SleepDuration} before the next Job run", PrettyPrintTime(sleepDuration));

                await Task.Delay(sleepDuration);
            }

            return(exitCode);
        }