Пример #1
0
        public ActionResult Save(RegisterModel model)
        {
            var job = model.Id.HasValue ? _db.SqlCommandJob.Find(model.Id) : new SqlCommandJob();

            if (job == null)
            {
                return(NotFound());
            }

            var oldName = job.Name;

            job.Name           = model.Name.Trim();
            job.Description    = model.Description;
            job.Cron           = model.Cron;
            job.Enabled        = model.Enabled;
            job.DatabaseInfoId = model.DatabaseInfoId;
            job.Command        = model.Command;

            if (!model.Id.HasValue)
            {
                _db.Jobs.Add(job);
            }

            _db.SaveChanges();

            if (!string.IsNullOrEmpty(oldName) && (oldName != job.Name || !job.Enabled))
            {
                RecurringJob.RemoveIfExists(oldName);
            }

            if (job.Enabled)
            {
                RecurringJob.AddOrUpdate(job.Name, () => JobHelper.Execute(_db, job.Name), job.Cron, TimeZoneInfo.Local);
            }

            return(Json(new { job.Id }));
        }