public ActionResult Create(TenantScheduledJobViewModel vm)
        {
            if (ModelState.IsValid)
            {
                var jobInfo = new JobInfoModel
                {
                    JobType        = "Scheduled",
                    CronExpression = vm.CronExpression,
                    Url            = vm.Url
                };

                var newScheduledJob = new TenantScheduledJob
                {
                    TenantId = vm.TenantId,
                    Name     = vm.Name,
                    JobInfo  = JsonConvert.SerializeObject(jobInfo)
                };

                db.TenantScheduledJobs.Add(newScheduledJob);
                db.SaveChanges();


                // Create Hangfire Job
                var selectedTenant = db.Tenants.FirstOrDefault(t => t.Id == vm.TenantId);
                if (selectedTenant != null)
                {
                    RecurringJob.AddOrUpdate(vm.Name, () => WebClientHelper.InvokeUrl(vm.Url), vm.CronExpression);
                }

                return(RedirectToAction("Details", "Tenants", new { id = vm.TenantId }));
            }

            return(View(vm));
        }
Ejemplo n.º 2
0
        public ActionResult Create(ScheduledJobViewModel vm)
        {
            if (ModelState.IsValid)
            {
                var jobInfo = new JobInfoModel
                {
                    JobType        = "Scheduled",
                    CronExpression = vm.CronExpression,
                    Url            = vm.Url
                };

                var newScheduledJob = new ScheduledJob
                {
                    Name    = vm.Name,
                    JobInfo = JsonConvert.SerializeObject(jobInfo)
                };

                _db.ScheduledJobs.Add(newScheduledJob);
                _db.SaveChanges();

                try
                {
                    // Create Hangfire Job
                    RecurringJob.AddOrUpdate(vm.Name, () => WebClientHelper.InvokeUrl(vm.Url), vm.CronExpression);
                    return(RedirectToAction("Index"));
                }
                catch (Exception ex) {
                    ViewBag.jobException = "Invalid cron expression";
                }
            }

            return(View(vm));
        }