public async Task <IActionResult> ExecutePyAsync(string id) { string zmkResponse = ""; string filePath = ""; string reqBody = ""; try { //read cron information from the request body using (var reader = new StreamReader(Request.Body)) { var body = reader.ReadToEnd(); reqBody = body.ToString(); } // foreach (var record in codeResponse) { if (record.Id.ToString() == id) { filePath = record.FilePath; break; } } // if (!string.IsNullOrEmpty(filePath)) { zmkResponse = await pyCompileClient.ExecutePy(filePath, ""); var jsonObj = JsonConvert.DeserializeObject <ExecuteCodeResponse>(zmkResponse); jsonObj.executedAt = DateTime.Now; List <ExecuteCodeResponse> tresp = new List <ExecuteCodeResponse>(); tresp.Add(jsonObj); JObject cronjson = JObject.Parse(reqBody); if (cronjson["recurrence"].ToString() == "REPEAT") { //check if same job is scheduled ISchedulerFactory schfack = new StdSchedulerFactory(); IScheduler scheduler = await schfack.GetScheduler(); var jobKey = new JobKey(filePath); if (await scheduler.CheckExists(jobKey)) { await scheduler.ResumeJob(jobKey); } else { #region create quartz job for execute code ITrigger trigger = TriggerBuilder.Create() .WithIdentity($"Execute Code Job-{DateTime.Now}") .WithCronSchedule(cronjson["cronExpression"].ToString()) .WithPriority(1) .Build(); IJobDetail job = JobBuilder.Create <ExecuteCodeJob>() .WithIdentity(filePath) .Build(); job.JobDataMap["id"] = id; job.JobDataMap["filePath"] = filePath; job.JobDataMap["baseurl"] = Configuration["PyServiceLocation:srvurl"]; await _scheduler.ScheduleJob(job, trigger); //add to scheduler payload SchedulerResponse schJob = new SchedulerResponse() { CreatedOn = DateTime.Now.ToString(), CronExpression = cronjson["cronExpression"].ToString(), DateCreated = DateTime.Now, EditedOn = DateTime.Now.ToString(), FilePath = filePath, Id = id, Name = id, Type = "PYTHON", Url = "", Recurrence = cronjson["recurrence"].ToString(), StartDate = (cronjson["startDate"] == null) ? "" : cronjson["startDate"].ToString(), //cronjson["startDate"].ToString(), StartTimeH = (cronjson["startTimeH"] == null) ? "" : cronjson["startTimeH"].ToString(), StartTimeM = (cronjson["startTimeM"] == null) ? "" : cronjson["startTimeM"].ToString(), // ZMKResponse = tresp.ToList<object>(), History = tresp.ToList <object>() }; SchedulerPayload.Create(schJob); #endregion } } else { //add history JArray jHist = new JArray(); foreach (var r in tresp) { jHist.Add(new JObject() { { "idforData", r.idforData }, { "status", r.status }, { "executedAt", r.executedAt } }); } //add to scheduler payload SchedulerResponse schJob = new SchedulerResponse() { CreatedOn = DateTime.Now.ToString(), CronExpression = "", DateCreated = DateTime.Now, EditedOn = DateTime.Now.ToString(), FilePath = filePath, Id = id, Name = id, Type = "PYTHON", Url = "", Recurrence = "ONE_TIME", StartDate = "", StartTimeH = "", StartTimeM = "", // ZMKResponse = tresp.ToList<object>(), Status = "", History = jHist.ToList <object>() }; SchedulerPayload.Create(schJob); } return(Json(jsonObj)); } else { return(BadRequest(new { message = "File execution failed." })); } } catch (Exception ex) { return(BadRequest(new { message = "File execution failed.", exception = ex.StackTrace })); } }
public async Task <IActionResult> CreateScoringJobAsync(string id, [FromBody] TrainingRequestParam jsonbody) { string filePath = ""; var json = JsonConvert.SerializeObject(jsonbody); JObject jo = JObject.Parse(json); /* TODO: validation for req body */ JObject cronjson = JObject.Parse(jo.ToString()); filePath = cronjson["filePath"].ToString(); /*request json for zmk */ JObject req = JObject.Parse(jo.ToString()); req.Remove("recurrence"); req.Remove("cronExpression"); req.Remove("startDate"); req.Remove("startTimeH"); req.Remove("startTimeM"); /* validate if model exists in the ZMOD directory and loaded. */ if (ModelPayload.Get().Where(m => m.Id == id).Count() == 1) { List <TrainingResponse> tresp = new List <TrainingResponse>(); #region schedule scoring /* check if same job is scheduled */ ISchedulerFactory schfack = new StdSchedulerFactory(); IScheduler scheduler = await schfack.GetScheduler(); var jobKey = new JobKey(filePath); if (await scheduler.CheckExists(jobKey)) { await scheduler.ResumeJob(jobKey); } else { try { #region create quartz job for training model ITrigger trigger = TriggerBuilder.Create() .WithIdentity($"Training Model Job-{DateTime.Now}") .WithCronSchedule(cronjson["cronExpression"].ToString()) .WithPriority(1) .Build(); IJobDetail job = JobBuilder.Create <TrainModelJob>() .WithIdentity(filePath) .Build(); job.JobDataMap["id"] = id; job.JobDataMap["filePath"] = filePath; job.JobDataMap["reqBody"] = req.ToString(); job.JobDataMap["baseurl"] = Configuration["PyServiceLocation:srvurl"]; await _scheduler.ScheduleJob(job, trigger); //add to scheduler payload SchedulerResponse schJob = new SchedulerResponse() { CreatedOn = DateTime.Now.ToString(), CronExpression = cronjson["cronExpression"].ToString(), DateCreated = DateTime.Now, EditedOn = DateTime.Now.ToString(), FilePath = filePath, Id = id, Name = id, Type = "PMML", Url = "", Recurrence = cronjson["recurrence"].ToString(), StartDate = cronjson["startDate"].ToString(), StartTimeH = (cronjson["startTimeH"].ToString() == null) ? "" : cronjson["startTimeH"].ToString(), StartTimeM = (cronjson["startTimeM"].ToString() == null) ? "" : cronjson["startTimeM"].ToString(), // ZMKResponse = tresp.ToList<object>() }; SchedulerPayload.Create(schJob); #endregion } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } } #endregion return(Ok(new { modelId = id, modelState = "model valid", message = "training job scheduled successfully!" })); } else { return(BadRequest(new { modelId = id, modelState = "model not found", message = "model does not exists!" })); } }