public string StartJobRun(TriggeredJob triggeredJob, JobSettings jobSettings, string trigger, Action<string, string> reportAction)
        {
            JobSettings = jobSettings;

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

            if (!_lockFile.Lock(String.Format("Starting {0} triggered WebJob", triggeredJob.Name)))
            {
                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;
            }

            // Return the run ID
            return logger.Id;
        }
        public void StartJobRun(TriggeredJob triggeredJob, JobSettings jobSettings, Action<string, string> reportAction)
        {
            JobSettings = jobSettings;

            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
            {
                if (_currentRunningJobWaitHandle != null)
                {
                    _currentRunningJobWaitHandle.Dispose();
                    _currentRunningJobWaitHandle = null;
                }

                _currentRunningJobWaitHandle = new ManualResetEvent(false);

                ThreadPool.QueueUserWorkItem(_ =>
                {
                    try
                    {
                        InitializeJobInstance(triggeredJob, logger);
                        RunJobInstance(triggeredJob, logger, logger.Id);
                    }
                    catch (Exception ex)
                    {
                        logger.LogError("WebJob run failed due to: " + ex);
                    }
                    finally
                    {
                        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;
            }
        }
Beispiel #3
0
        public void CreateAndDeleteJobSucceeds()
        {
            RunScenario("CreateAndDeleteJobSucceeds", appManager =>
            {
                string zippedJobBinaries = BuildZippedJobBinaries();

                appManager.JobsManager.CreateTriggeredJobAsync("job1", zippedJobBinaries).Wait();
                appManager.JobsManager.CreateTriggeredJobAsync("job2", zippedJobBinaries).Wait();
                appManager.JobsManager.CreateContinuousJobAsync("job1", zippedJobBinaries).Wait();
                appManager.JobsManager.CreateContinuousJobAsync("job2", zippedJobBinaries).Wait();

                // Disabling a continuous job should not affect the job count
                WaitUntilAssertVerified(
                    "disable continuous job",
                    TimeSpan.FromSeconds(60),
                    () => appManager.JobsManager.DisableContinuousJobAsync("job1").Wait());

                // Adding a setting to a triggered job should not affect the job count
                var jobSettings = new JobSettings();
                jobSettings["one"] = 1;
                appManager.JobsManager.SetTriggeredJobSettingsAsync("job1", jobSettings).Wait();

                var triggeredJobs = appManager.JobsManager.ListTriggeredJobsAsync().Result;
                var continuousJobs = appManager.JobsManager.ListContinuousJobsAsync().Result;

                Assert.Equal(2, triggeredJobs.Count());
                Assert.Equal(2, continuousJobs.Count());

                VerifyTriggeredJobTriggers(appManager, "job1", 1, "Success", "echo ");

                appManager.JobsManager.DeleteTriggeredJobAsync("job1").Wait();
                appManager.JobsManager.DeleteTriggeredJobAsync("job2").Wait();
                appManager.JobsManager.DeleteContinuousJobAsync("job1").Wait();
                appManager.JobsManager.DeleteContinuousJobAsync("job2").Wait();

                triggeredJobs = appManager.JobsManager.ListTriggeredJobsAsync().Result;
                continuousJobs = appManager.JobsManager.ListContinuousJobsAsync().Result;

                Assert.Equal(0, triggeredJobs.Count());
                Assert.Equal(0, continuousJobs.Count());
            });
        }
Beispiel #4
0
        public void InPlaceJobRunsInPlace()
        {
            RunScenario("InPlaceJobRunsInPlace", appManager =>
            {
                const string jobName = "joba";
                const string expectedTempStr = "Temp\\jobs\\triggered\\" + jobName;
                const string expectedAppDataStr = "App_Data\\jobs\\triggered\\" + jobName;

                TestTracer.Trace("Create the triggered WebJob");
                appManager.JobsManager.CreateTriggeredJobAsync(jobName, "run.cmd", "cd\n").Wait();

                TestTracer.Trace("Trigger the triggered WebJob");
                VerifyTriggeredJobTriggers(appManager, jobName, 1, "Success", expectedTempStr);

                var jobSettings = new JobSettings();

                TestTracer.Trace("Set triggered WebJob to is_in_place true");
                jobSettings.SetSetting("is_in_place", true);
                appManager.JobsManager.SetTriggeredJobSettingsAsync(jobName, jobSettings).Wait();

                TestTracer.Trace("Trigger the triggered WebJob");
                VerifyTriggeredJobTriggers(appManager, jobName, 2, "Success", expectedAppDataStr);

                TestTracer.Trace("Set triggered WebJob to is_in_place false");
                jobSettings.SetSetting("is_in_place", false);
                appManager.JobsManager.SetTriggeredJobSettingsAsync(jobName, jobSettings).Wait();

                TestTracer.Trace("Trigger the triggered WebJob");
                VerifyTriggeredJobTriggers(appManager, jobName, 3, "Success", expectedTempStr);

                TestTracer.Trace("Create the continuous WebJob");
                appManager.JobsManager.CreateContinuousJobAsync(jobName, "run.cmd", "cd > %WEBROOT_PATH%\\..\\..\\LogFiles\\verification.txt.%WEBSITE_INSTANCE_ID%\n").Wait();

                WaitUntilAssertVerified(
                    "verification file",
                    TimeSpan.FromSeconds(30),
                    () => VerifyVerificationFile(appManager, new[] { expectedTempStr }));

                TestTracer.Trace("Set continuous WebJob to is_in_place true");
                jobSettings.SetSetting("is_in_place", true);
                appManager.JobsManager.SetContinuousJobSettingsAsync(jobName, jobSettings).Wait();

                WaitUntilAssertVerified(
                    "verification file",
                    TimeSpan.FromSeconds(30),
                    () => VerifyVerificationFile(appManager, new[] { expectedAppDataStr }));

                TestTracer.Trace("Set continuous WebJob to is_in_place false");
                jobSettings.SetSetting("is_in_place", false);

                appManager.JobsManager.SetContinuousJobSettingsAsync(jobName, jobSettings).Wait();
                WaitUntilAssertVerified(
                    "verification file",
                    TimeSpan.FromSeconds(30),
                    () => VerifyVerificationFile(appManager, new[] { expectedTempStr }));
            });
        }
        public void RefreshJob(ContinuousJob continuousJob, JobSettings jobSettings, bool logRefresh)
        {
            if (logRefresh)
            {
                _continuousJobLogger.LogInformation("Detected WebJob file/s were updated, refreshing WebJob");
            }

            StopJob();
            JobSettings = jobSettings;
            StartJob(continuousJob);
        }
Beispiel #6
0
 public void RefreshJob(ContinuousJob continuousJob, JobSettings jobSettings)
 {
     StopJob();
     _jobSettings = jobSettings;
     StartJob(continuousJob);
 }
Beispiel #7
0
        public void RefreshJob_Continuous_LogsWarning_WhenAlwaysOnNotEnabled()
        {
            System.Environment.SetEnvironmentVariable(ContinuousJobRunner.WebsiteSCMAlwaysOnEnabledKey, "0");
            Assert.True(ContinuousJobRunner.AlwaysOnNotEnabled());

            JobSettings settings = new JobSettings();
            _runner.RefreshJob(_job, settings, false);

            VerifyAlwaysOnWarningWritten();

            System.Environment.SetEnvironmentVariable(ContinuousJobRunner.WebsiteSCMAlwaysOnEnabledKey, null);
        }