Example #1
0
        public void StopJob(bool isShutdown = false)
        {
            Interlocked.Exchange(ref _started, 0);

            bool logStopped = false;

            if (_continuousJobThread != null)
            {
                logStopped = true;

                if (isShutdown)
                {
                    _continuousJobLogger.LogInformation("WebJob is stopping due to website shutting down");
                }

                _continuousJobLogger.ReportStatus(ContinuousJobStatus.Stopping);

                NotifyShutdownJob();

                // By default give the continuous job 5 seconds before killing it (after notifying the continuous job)
                if (!_continuousJobThread.Join(JobSettings.GetStoppingWaitTime(DefaultContinuousJobStoppingWaitTimeInSeconds)))
                {
                    _continuousJobThread.Abort();
                }

                _continuousJobThread = null;
            }

            SafeKillAllRunningJobInstances(_continuousJobLogger);

            if (logStopped)
            {
                UpdateStatusIfChanged(ContinuousJobStatus.Stopped);
            }
        }
Example #2
0
        protected override void OnShutdown()
        {
            TimeSpan maxTimeout  = TimeSpan.MinValue;
            var      waitHandles = new List <WaitHandle>();

            foreach (TriggeredJobRunner triggeredJobRunner in _triggeredJobRunners.Values)
            {
                WaitHandle waitHandle = triggeredJobRunner.CurrentRunningJobWaitHandle;
                if (waitHandle != null)
                {
                    waitHandles.Add(waitHandle);

                    // Determine the maximum timeout for all currently running jobs
                    JobSettings jobSettings = GetJobSettings(triggeredJobRunner.JobName);

                    // By default wait for 30 seconds until the triggered WebJob is done
                    TimeSpan stoppingWaitTime = jobSettings.GetStoppingWaitTime(DefaultTriggeredJobStoppingWaitTimeInSeconds);
                    maxTimeout = stoppingWaitTime > maxTimeout ? stoppingWaitTime : maxTimeout;
                }
            }

            // Wait until all running jobs are finished up to the maximum timeout
            if (waitHandles.Any())
            {
                WaitHandle.WaitAll(waitHandles.ToArray(), maxTimeout);
            }
        }
Example #3
0
        public void StopJob(bool isShutdown = false)
        {
            Interlocked.Exchange(ref _started, 0);

            bool logStopped = false;

            if (_continuousJobThread != null)
            {
                logStopped = true;

                if (isShutdown)
                {
                    LogInformation("WebJob is stopping due to website shutting down");
                }

                _continuousJobLogger.ReportStatus(ContinuousJobStatus.Stopping);

                NotifyShutdownJob();

                // By default give the continuous job 5 seconds before killing it (after notifying the continuous job)
                if (!_continuousJobThread.Join(JobSettings.GetStoppingWaitTime(DefaultContinuousJobStoppingWaitTimeInSeconds)))
                {
                    _continuousJobThread.KuduAbort(String.Format("Stopping {0} {1} job", JobName, Constants.ContinuousPath));

                    // to avoid overlapping new and old thread, we will wait for aborting thread to
                    // actually exit since it may take a few seconds after Thread.Abort is called.
                    _continuousJobThread.Join(TimeSpan.FromMinutes(1));
                }

                _continuousJobThread = null;
            }

            SafeKillAllRunningJobInstances(_continuousJobLogger);

            if (logStopped)
            {
                UpdateStatusIfChanged(ContinuousJobStatus.Stopped);
            }
        }
Example #4
0
        public void StopJob()
        {
            Interlocked.Exchange(ref _started, 0);

            _continuousJobLogger.ReportStatus(ContinuousJobStatus.Stopping);

            NotifyShutdownJob();

            if (_continuousJobThread != null)
            {
                // By default give the continuous job 5 seconds before killing it (after notifying the continuous job)
                if (!_continuousJobThread.Join(_jobSettings.GetStoppingWaitTime(DefaultContinuousJobStoppingWaitTimeInSeconds)))
                {
                    _continuousJobThread.Abort();
                }

                _continuousJobThread = null;
                _continuousJobLogger.ReportStatus(ContinuousJobStatus.Stopped);
            }

            SafeKillAllRunningJobInstances(_continuousJobLogger);
        }