예제 #1
0
        public async Task <IActionResult> ExecutePyAsync(string id)
        {
            string zmkResponse = "";
            string filePath    = "";

            try
            {
                foreach (var record in codeResponse)
                {
                    if (record.Id.ToString() == id)
                    {
                        filePath = record.FilePath;
                        break;
                    }
                }
                if (!string.IsNullOrEmpty(filePath))
                {
                    zmkResponse = await pyCompileClient.ExecutePy(filePath, "");

                    JObject json = JObject.Parse(zmkResponse);
                    return(Json(json));
                }
                else
                {
                    return(BadRequest(new { message = "File execution failed." }));
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(new { message = "File execution failed.", exception = ex.StackTrace }));
            }
        }
예제 #2
0
        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 }));
            }
        }