public static void Execute(string jobName, bool continuous, IDictionary<string, string> overrideSettings) { // Create the settings manager var settings = new Settings(overrideSettings); // Get a job runner JobRunner runner = LoadJobRunner(settings); // See which mode we're in if (String.IsNullOrWhiteSpace(jobName)) { // Run ALL THE JOBS! runner.OnStart(); runner.Run(); Console.WriteLine("Worker is running. Press ENTER to stop"); Console.ReadLine(); runner.Stop(); runner.OnStop(); } else { // Run JUST ONE JOB! if (!continuous) { runner.RunSingleJob(jobName); } else { runner.RunSingleJobContinuously(jobName); } } }
public JobRunner([Import(AllowDefault=true)] Settings settings, [ImportMany] IEnumerable<WorkerJob> jobs) { _settings = settings ?? new Settings(); Jobs = jobs.ToDictionary(j => j.GetType().Name, StringComparer.OrdinalIgnoreCase); _subject.OnNext(Unit.Default); foreach (var job in Jobs.Values) { job.Initialize(_settings); } }
public WorkerRole(Settings settings) { // Configure NLog LoggingConfiguration config = new LoggingConfiguration(); ColoredConsoleTarget consoleTarget = new ColoredConsoleTarget(); config.AddTarget("console", consoleTarget); consoleTarget.Layout = "${date:format=HH\\:MM\\:ss} [${logger}] ${message}"; LoggingRule rule = new LoggingRule("*", NLog.LogLevel.Debug, consoleTarget); config.LoggingRules.Add(rule); if (RoleEnvironment.IsAvailable) { ConfigureAzureLogging(config); } LogManager.Configuration = config; _runner = LoadJobRunner(settings); }
public static void Initialize(Settings settings) { lock (LockObject) { CloudBlobClient blobClient = settings.ReportStorage.CreateCloudBlobClient(); CloudBlobContainer blobContainer = blobClient.GetContainerReference("ops"); blobContainer.CreateIfNotExists(); CloudBlockBlob webPageBlob = blobContainer.GetBlockBlobReference("dashboard.html"); if (!webPageBlob.Exists()) { MemoryStream stream = new MemoryStream(); TextWriter writer = new StreamWriter(stream); writer.Write(LoadFile("NuGetGallery.Operations.Worker.Dashboard.html")); writer.Flush(); stream.Seek(0, SeekOrigin.Begin); webPageBlob.Properties.ContentType = "text/html"; webPageBlob.Properties.CacheControl = "no-cache, no-store, must-revalidate"; webPageBlob.UploadFromStream(stream); } CloudBlockBlob jobsReportBlob = blobContainer.GetBlockBlobReference("jobs.json"); if (!jobsReportBlob.Exists()) { MemoryStream stream = new MemoryStream(); TextWriter writer = new StreamWriter(stream); writer.Write("{}"); writer.Flush(); stream.Seek(0, SeekOrigin.Begin); jobsReportBlob.Properties.ContentType = "application/json"; jobsReportBlob.Properties.CacheControl = "no-cache, no-store, must-revalidate"; jobsReportBlob.UploadFromStream(stream); } } }
public static void Update(Settings settings, params JobStatusReport[] reports) { lock (LockObject) { CloudBlobClient blobClient = settings.ReportStorage.CreateCloudBlobClient(); CloudBlobContainer blobContainer = blobClient.GetContainerReference("ops"); blobContainer.CreateIfNotExists(); CloudBlockBlob jobsReportBlob = blobContainer.GetBlockBlobReference("jobs.json"); // read the current report JObject currentReport; try { MemoryStream downloadStream = new MemoryStream(); jobsReportBlob.DownloadToStream(downloadStream); downloadStream.Seek(0, SeekOrigin.Begin); using (TextReader reader = new StreamReader(downloadStream)) { string downloadJson = reader.ReadToEnd(); currentReport = (JObject)JToken.Parse(downloadJson); } } catch (StorageException e) { // we don't need to fail if the blob has been deleted if (e.RequestInformation.HttpStatusCode == 404) { currentReport = new JObject(); } else { throw; } } // update the appropriate entries string processId; try { processId = Process.GetCurrentProcess().Id.ToString(); } catch (Exception) { processId = string.Empty; } foreach (JobStatusReport report in reports) { JObject latestJobStatusEntry = new JObject(); latestJobStatusEntry["pid"] = processId; latestJobStatusEntry["at"] = report.At; latestJobStatusEntry["duration"] = report.Duration; latestJobStatusEntry["status"] = report.Status; latestJobStatusEntry["message"] = report.Message; JArray jobStatus; JToken tok; if (currentReport.TryGetValue(report.JobName, out tok)) { jobStatus = (JArray)tok; } else { jobStatus = new JArray(); } if (jobStatus.Count == 7) { jobStatus.RemoveAt(0); } jobStatus.Add(latestJobStatusEntry); currentReport[report.JobName] = jobStatus; } // write the whole thing back again MemoryStream uploadStream = new MemoryStream(); TextWriter writer = new StreamWriter(uploadStream); string uploadJson = currentReport.ToString(); writer.Write(uploadJson); writer.Flush(); uploadStream.Seek(0, SeekOrigin.Begin); jobsReportBlob.Properties.ContentType = "application/json"; jobsReportBlob.Properties.CacheControl = "no-cache, no-store, must-revalidate"; jobsReportBlob.UploadFromStream(uploadStream); } }
private static JobRunner LoadJobRunner(Settings settings) { AssemblyCatalog catalog = new AssemblyCatalog(typeof(WorkerRole).Assembly); var container = new CompositionContainer(catalog); // Load settings settings = settings ?? new Settings(); container.ComposeExportedValue(settings); // Get the job runner return container.GetExportedValue<JobRunner>(); }