public void AddJob(string schedule, Guid id, JobModel job) { var cj = new CronJob(schedule, job); if (_cronJobs.ContainsKey(id)) { _cronJobs[id] = cj; } else { _cronJobs.TryAdd(id, cj); } }
public CronJob(string schedule, JobModel job) { _cronSchedule = new CronSchedule(schedule); _job = job; }
private void AddOrUpdateJob() { try { var argumentObject = (JObject) _packet.Args[0]; var jobId = Guid.Parse(argumentObject["Guid"].ToString()); var base64ScriptContents = argumentObject["Base64ScriptContents"].ToString(); var data = Convert.FromBase64String(base64ScriptContents); var decodedString = Encoding.UTF8.GetString(data); var scriptSchedule = argumentObject["Schedule"].ToString(); var scriptName = argumentObject["Name"].ToString(); var scriptType = argumentObject["Type"].ToString(); var newPath = Path.Combine(_cronJobService.JobScriptsPath, scriptName); if (_cronJobService.JobList.ContainsKey(jobId)) { var oldPath = _cronJobService.JobList[jobId].Name; _cronJobService.JobList[jobId].Name = newPath; if (!oldPath.Equals(newPath) && File.Exists(oldPath)) { File.Delete(oldPath); } _cronJobService.JobList[jobId].Schedule = scriptSchedule; _cronJobService.JobList[jobId].Type = scriptType; var path = _cronJobService.JobList[jobId].Name; //job exist, lets update its contents File.WriteAllText(path, decodedString); _cronJobService.AddOrUpdateJob(jobId, _cronJobService.JobList[jobId]); _cronJobService.Save(); } else { File.WriteAllText(newPath, decodedString); var job = new JobModel { Schedule = scriptSchedule, Type = scriptType, Name = newPath }; _cronJobService.AddOrUpdateJob(jobId, job); _cronJobService.JobList.TryAdd(jobId, job); _cronJobService.Save(); } var response = new { AddedOrUpdated = true, Message = $"{jobId} was added to the server.", SavedTo = newPath }; _builder.WriteMessage(response); } catch (Exception ex) { var exceptionResponse = new { AddedOrUpdated = false, Message = ex.Message, Exception = ex.StackTrace }; _builder.WriteMessage(exceptionResponse); } }
public void AddOrUpdateJob(Guid id, JobModel job) { CronDaemon.AddJob(job.Schedule, id, job); }