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;
        }
Esempio n. 2
0
        public void StartJobRun(TriggeredJob triggeredJob)
        {
            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);
                    }
                    finally
                    {
                        logger.ReportEndRun();
                        _lockFile.Release();
                    }
                });
            }
            catch (Exception ex)
            {
                logger.LogError("Failed to start job due to: " + ex);
                _lockFile.Release();
                throw;
            }
        }
Esempio n. 3
0
        public TriggeredJobSchedule(TriggeredJob triggeredJob, Action<TriggeredJobSchedule> onSchedule, TriggeredJobSchedulerLogger logger)
        {
            TriggeredJob = triggeredJob;
            _onSchedule = onSchedule;
            Logger = logger;

            _timer = new Timer(OnTimer, triggeredJob, Timeout.Infinite, Timeout.Infinite);
        }
        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;
            }
        }
Esempio n. 5
0
        public static TriggeredJobRunLogger LogNewRun(TriggeredJob triggeredJob, IEnvironment environment, IFileSystem fileSystem, ITraceFactory traceFactory, IDeploymentSettingsManager settings)
        {
            OldRunsCleanup(triggeredJob.Name, fileSystem, environment, traceFactory, settings);

            string id = DateTime.UtcNow.ToString("yyyyMMddHHmmssffff");
            var logger = new TriggeredJobRunLogger(triggeredJob.Name, id, environment, fileSystem, traceFactory);
            var triggeredJobStatus = new TriggeredJobStatus()
            {
                Status = "Initializing",
                StartTime = DateTime.UtcNow
            };
            logger.ReportStatus(triggeredJobStatus);
            return logger;
        }
Esempio n. 6
0
        public void JobsScriptsShouldBeChosenByOrder()
        {
            RunScenario("JobsScriptsShouldBeChosenByOrder", appManager =>
            {
                const string jobName = "job1";

                TestTracer.Trace("Adding all scripts");

                var scriptFileNames = new string[]
                {
                    "run.cmd",
                    "run.bat",
                    "run.exe",
                    "run.ps1",
                    "run.sh",
                    //"run.py",
                    //"run.php",
                    "run.js",
                    "go.cmd",
                    "do.bat",
                    "console.exe",
                    "execute.ps1",
                    "invoke.sh",
                    //"respond.py",
                    //"request.php",
                    "execute.js"
                };

                foreach (string scriptFileName in scriptFileNames)
                {
                    appManager.VfsManager.WriteAllText(TriggeredJobBinPath + "/" + jobName + "/" + scriptFileName, JobScript);
                }

                foreach (string scriptFileName in scriptFileNames)
                {
                    TestTracer.Trace("Verify - " + scriptFileName);

                    var expectedTriggeredJob = new TriggeredJob()
                    {
                        Name = jobName,
                        JobType = "triggered",
                        RunCommand = scriptFileName
                    };

                    TriggeredJob triggeredJob = appManager.JobsManager.GetTriggeredJobAsync(jobName).Result;
                    AssertTriggeredJob(expectedTriggeredJob, triggeredJob);

                    appManager.VfsManager.Delete(TriggeredJobBinPath + "/" + jobName + "/" + scriptFileName);
                }
            });
        }
Esempio n. 7
0
        public void TriggeredJobAcceptsArguments()
        {
            RunScenario("TriggeredJobAcceptsArguments", appManager =>
            {
                const string jobName = "job1";

                TestTracer.Trace("Copying the script to the triggered job directory");

                appManager.JobsManager.CreateTriggeredJobAsync(jobName, "run.cmd", "echo %*").Wait();

                var expectedTriggeredJob = new TriggeredJob()
                {
                    Name = jobName,
                    JobType = "triggered",
                    RunCommand = "run.cmd"
                };

                TestTracer.Trace("Trigger the job");

                VerifyTriggeredJobTriggers(appManager, jobName, 1, "Success", "echo test arguments", expectedError: null, arguments: "test arguments");
            });
        }
Esempio n. 8
0
        public void TriggeredJobTriggers()
        {
            RunScenario("TriggeredJobTriggers", appManager =>
            {
                const string jobName = "job1";

                TestTracer.Trace("Copying the script to the triggered job directory");

                appManager.JobsManager.CreateTriggeredJobAsync(jobName, "run.cmd", JobScript).Wait();

                var expectedTriggeredJob = new TriggeredJob()
                {
                    Name = jobName,
                    JobType = "triggered",
                    RunCommand = "run.cmd"
                };

                TestTracer.Trace("Verify triggered job exists");

                TriggeredJob triggeredJob = appManager.JobsManager.GetTriggeredJobAsync(jobName).Result;
                AssertTriggeredJob(expectedTriggeredJob, triggeredJob);

                TestTracer.Trace("Trigger the job");

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

                VerifyVerificationFile(appManager, new string[] { ExpectedVerificationFileContent });

                TestTracer.Trace("Trigger the job again");

                VerifyTriggeredJobTriggers(appManager, jobName, 2, "Success", "echo ");

                VerifyVerificationFile(appManager, new string[] { ExpectedVerificationFileContent, ExpectedVerificationFileContent });

                TestTracer.Trace("Trigger the job 5 more times to make sure history is trimmed");

                TestTracer.Trace("Disable all WebJobs");
                appManager.SettingsManager.SetValue(SettingsKeys.WebJobsStopped, "1").Wait();
                VerifyTriggeredJobDoesNotTrigger(appManager, jobName).Wait();

                TestTracer.Trace("Enable all WebJobs");
                appManager.SettingsManager.SetValue(SettingsKeys.WebJobsStopped, "0").Wait();
                VerifyTriggeredJobTriggers(appManager, jobName, 3, "Success", "echo ");

                appManager.SettingsManager.SetValue(SettingsKeys.WebJobsHistorySize, "5").Wait();

                VerifyTriggeredJobTriggers(appManager, jobName, 4, "Success", "echo ");
                VerifyTriggeredJobTriggers(appManager, jobName, 5, "Success", "echo ");
                VerifyTriggeredJobTriggers(appManager, jobName, 5, "Success", "echo ");
                VerifyTriggeredJobTriggers(appManager, jobName, 5, "Success", "echo ");
            });
        }
Esempio n. 9
0
 private void AssertTriggeredJob(TriggeredJob expectedTriggeredJob, TriggeredJob actualTriggeredJob)
 {
     AssertJob(expectedTriggeredJob, actualTriggeredJob);
     Assert.NotNull(actualTriggeredJob);
     Assert.NotNull(actualTriggeredJob.HistoryUrl);
 }
Esempio n. 10
0
        public void DnxWebJobRunsCorrectly()
        {
            RunScenario("DnxWebJobRunsCorrectly", appManager =>
            {
                const string jobName = "dnxJob";
                var program = @"using System;
namespace WebJob
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(""Hello Dnx!!"");
        }
    }
}";
                var project = @"{
    ""dependencies"": {
    },
    ""frameworks"": {
        ""dnx451"": { }
    }
}";

                appManager.VfsManager.WriteAllText(TriggeredJobBinPath + "/" + jobName + "/" + "Program.cs", program);
                appManager.VfsManager.WriteAllText(TriggeredJobBinPath + "/" + jobName + "/" + "project.json", project);

                var expectedWebJob = new TriggeredJob
                {
                    Name = jobName,
                    JobType = "triggered",
                    RunCommand = "project.json"
                };

                var webjob = appManager.JobsManager.GetTriggeredJobAsync(jobName).Result;
                AssertTriggeredJob(expectedWebJob, webjob);
                appManager.JobsManager.InvokeTriggeredJobAsync(jobName).Wait();

                TriggeredJobRun jobRun = null;

                WaitUntilAssertVerified(
                    "Dnx Triggered job",
                    TimeSpan.FromSeconds(30),
                    () =>
                    {
                        var jobHistory = appManager.JobsManager.GetTriggeredJobHistoryAsync(jobName).Result;
                        jobRun = jobHistory.TriggeredJobRuns.First();
                        Assert.Equal("Success", jobRun.Status);
                    });

                var logPath = jobRun.OutputUrl.AbsoluteUri.Split(new[] { "/vfs/" }, StringSplitOptions.RemoveEmptyEntries).Last();
                var jobOutput = appManager.VfsManager.ReadAllText(logPath);
                Assert.True(jobOutput.Contains("Hello Dnx!!"));
            });
        }
Esempio n. 11
0
        public void TriggeredJobTriggers()
        {
            RunScenario("TriggeredJobTriggers", appManager =>
            {
                const string jobName = "job1";

                TestTracer.Trace("Copying the script to the triggered job directory");

                WaitUntilAssertVerified(
                    "writing file",
                    TimeSpan.FromSeconds(10),
                    () => appManager.VfsManager.WriteAllText(TriggeredJobBinPath + "/" + jobName + "/run.cmd", JobScript));

                var expectedTriggeredJob = new TriggeredJob()
                {
                    Name = jobName,
                    JobType = "triggered",
                    RunCommand = "run.cmd"
                };

                TestTracer.Trace("Verify triggered job exists");

                TriggeredJob triggeredJob = appManager.JobsManager.GetTriggeredJobAsync(jobName).Result;
                AssertTriggeredJob(expectedTriggeredJob, triggeredJob);

                TestTracer.Trace("Trigger the job");

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

                VerifyVerificationFile(appManager, new string[] { ExpectedVerificationFileContent });

                TestTracer.Trace("Trigger the job again");

                VerifyTriggeredJobTriggers(appManager, jobName, 2, "Success", "echo ");

                VerifyVerificationFile(appManager, new string[] { ExpectedVerificationFileContent, ExpectedVerificationFileContent });

                TestTracer.Trace("Trigger the job 5 more times to make sure history is trimmed");

                appManager.SettingsManager.SetValue(SettingsKeys.MaxJobRunsHistoryCount, "5").Wait();

                VerifyTriggeredJobTriggers(appManager, jobName, 3, "Success", "echo ");
                VerifyTriggeredJobTriggers(appManager, jobName, 4, "Success", "echo ");
                VerifyTriggeredJobTriggers(appManager, jobName, 5, "Success", "echo ");
                VerifyTriggeredJobTriggers(appManager, jobName, 5, "Success", "echo ");
                VerifyTriggeredJobTriggers(appManager, jobName, 5, "Success", "echo ");
            });
        }