示例#1
0
        public void StartJobRun(TriggeredJob triggeredJob, JobSettings jobSettings, string trigger, Action <string, string> reportAction)
        {
            JobSettings = jobSettings;

            if (Settings.IsWebJobsStopped())
            {
                throw new WebJobsStoppedException();
            }

            if (!_lockFile.Lock())
            {
                throw new ConflictException();
            }

            TriggeredJobRunLogger logger = TriggeredJobRunLogger.LogNewRun(triggeredJob, trigger, Environment, TraceFactory, Settings);

            Debug.Assert(logger != null);

            try
            {
                if (_currentRunningJobWaitHandle != null)
                {
                    _currentRunningJobWaitHandle.Dispose();
                    _currentRunningJobWaitHandle = null;
                }

                _currentRunningJobWaitHandle = new ManualResetEvent(false);

                var tracer = TraceFactory.GetTracer();
                var step   = tracer.Step("Run {0} {1}", triggeredJob.JobType, triggeredJob.Name);
                ThreadPool.QueueUserWorkItem(_ =>
                {
                    try
                    {
                        InitializeJobInstance(triggeredJob, logger);
                        RunJobInstance(triggeredJob, logger, logger.Id, trigger, tracer);
                    }
                    catch (Exception ex)
                    {
                        logger.LogError("WebJob run failed due to: " + ex);
                    }
                    finally
                    {
                        step.Dispose();
                        logger.ReportEndRun();
                        _lockFile.Release();
                        reportAction(triggeredJob.Name, logger.Id);
                        _currentRunningJobWaitHandle.Set();
                    }
                });
            }
            catch (Exception ex)
            {
                logger.LogError("Failed to start job due to: " + ex);
                _lockFile.Release();
                throw;
            }
        }
示例#2
0
        private LockFile AcquireSessionLock(MonitoringSession session, string methodName = "")
        {
            string sessionFilePath = (session.EndDate != DateTime.MinValue.ToUniversalTime()) ? GetCpuMonitoringPath(MonitoringSessionDirectories.Completed) : GetCpuMonitoringPath(MonitoringSessionDirectories.Active);
            string lockFilePath    = sessionFilePath + ".lock";

            LockFile _lockFile = new LockFile(lockFilePath);
            int      loopCount = 0;
            int      lognum    = 1;
            int      maximumWaitTimeInSeconds = 15 * 60;

            while (!_lockFile.Lock($"AcquireSessionLock by {methodName} on {Environment.MachineName}") && loopCount <= maximumWaitTimeInSeconds)
            {
                ++loopCount;
                if (loopCount > lognum * 120)
                {
                    ++lognum;
                    Logger.LogCpuMonitoringVerboseEvent($"Waiting to acquire the lock on session file , loop {lognum}", session.SessionId);
                }
                Thread.Sleep(1000);
            }
            if (loopCount == maximumWaitTimeInSeconds)
            {
                Logger.LogCpuMonitoringVerboseEvent($"Deleting the lock file as it seems to be in an orphaned stage", session.SessionId);
                _lockFile.Release();
                return(null);
            }
            return(_lockFile);
        }
示例#3
0
        public void AsyncLock_BasicTest()
        {
            for (int i = 0; i < 2; i++)
            {
                // Acquire
                Assert.Equal(false, _lockFile.IsHeld);
                Assert.Equal(true, _lockFile.Lock());

                // Test
                Assert.Equal(true, _lockFile.IsHeld);
                Assert.Equal(false, _lockFile.Lock());

                // Release
                _lockFile.Release();
                Assert.Equal(false, _lockFile.IsHeld);
            }
        }
示例#4
0
        public void StartJobRun(TriggeredJob triggeredJob)
        {
            if (Settings.IsWebJobsStopped())
            {
                throw new WebJobsStoppedException();
            }

            if (!_lockFile.Lock())
            {
                throw new ConflictException();
            }

            TriggeredJobRunLogger logger = TriggeredJobRunLogger.LogNewRun(triggeredJob, Environment, TraceFactory, Settings);

            Debug.Assert(logger != null);

            try
            {
                InitializeJobInstance(triggeredJob, logger);

                ThreadPool.QueueUserWorkItem(_ =>
                {
                    try
                    {
                        RunJobInstance(triggeredJob, logger, logger.Id);
                    }
                    finally
                    {
                        logger.ReportEndRun();
                        _lockFile.Release();
                    }
                });
            }
            catch (Exception ex)
            {
                logger.LogError("Failed to start job due to: " + ex);
                _lockFile.Release();
                throw;
            }
        }
示例#5
0
        public void SaveSession(MonitoringSession session, LockFile lockFile = null)
        {
            if (lockFile == null)
            {
                lockFile = AcquireSessionLock(session);
            }

            string cpuMonitorCompletedPath = GetCpuMonitoringPath(MonitoringSessionDirectories.Completed);

            cpuMonitorCompletedPath = Path.Combine(cpuMonitorCompletedPath, session.SessionId + ".json");
            session.ToJsonFile(cpuMonitorCompletedPath);
            if (lockFile != null)
            {
                lockFile.Release();
            }
        }
示例#6
0
 private void ReleaseSingletonLock()
 {
     _singletonLock.Release();
 }
示例#7
0
        private void ReleaseSingletonLock()
        {
            _singletonLock.Release();

            LogInformation("WebJob singleton lock is released");
        }