示例#1
0
        private static void JobSetup(JobBase job, IDictionary <string, string> jobArgsDictionary, ref int?sleepDuration)
        {
            if (JobConfigurationManager.TryGetBoolArgument(jobArgsDictionary, "dbg"))
            {
                throw new ArgumentException("-dbg is a special argument and should be the first argument...");
            }

            if (JobConfigurationManager.TryGetBoolArgument(jobArgsDictionary, "ConsoleLogOnly"))
            {
                throw new ArgumentException("-ConsoleLogOnly is a special argument and should be the first argument (can be the second if '-dbg' is used)...");
            }

            if (sleepDuration == null)
            {
                Trace.TraceWarning("SleepDuration is not provided or is not a valid integer. Unit is milliSeconds. Assuming default of 5000 ms...");
                sleepDuration = 5000;
            }

            // Initialize the job once with everything needed.
            // JobTraceListener(s) are already initialized
            if (!job.Init(jobArgsDictionary))
            {
                // If the job could not be initialized successfully, STOP!
                Trace.TraceError("Exiting. The job could not be initialized successfully with the arguments passed");
            }
        }
示例#2
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);
            }
        }
示例#3
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);
            }
        }
示例#4
0
        private static void JobSetup(JobBase job, IDictionary<string, string> jobArgsDictionary, ref int? sleepDuration)
        {
            if (JobConfigurationManager.TryGetBoolArgument(jobArgsDictionary, "dbg"))
            {
                throw new ArgumentException("-dbg is a special argument and should be the first argument...");
            }

            if (JobConfigurationManager.TryGetBoolArgument(jobArgsDictionary, "ConsoleLogOnly"))
            {
                throw new ArgumentException("-ConsoleLogOnly is a special argument and should be the first argument (can be the second if '-dbg' is used)...");
            }

            if (sleepDuration == null)
            {
                Trace.TraceWarning("SleepDuration is not provided or is not a valid integer. Unit is milliSeconds. Assuming default of 5000 ms...");
                sleepDuration = 5000;
            }

            // Initialize the job once with everything needed.
            // JobTraceListener(s) are already initialized
            if (!job.Init(jobArgsDictionary))
            {
                // If the job could not be initialized successfully, STOP!
                Trace.TraceError("Exiting. The job could not be initialized successfully with the arguments passed");
            }
        }
示例#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);
        }