public ContentResult Save(string objdata, string value)
        {
            JsonObject js = new JsonObject();

            js.StatusCode = 200;
            js.Message    = "Upload Success";
            try
            {
                ScheduleTask obj = JsonConvert.DeserializeObject <ScheduleTask>(objdata);
                obj = ScheduleTaskManager.Update(obj);
                if (obj.Id == 0)
                {
                    js.StatusCode = 400;
                    js.Message    = "Has Errors. Please contact Admin for more information";
                }
                else
                {
                    js.Data = obj;
                }
            }
            catch (Exception objEx)
            {
                js.StatusCode = 400;
                js.Message    = objEx.Message;
            }

            return(Content(JsonConvert.SerializeObject(js), "application/json"));
        }
 public ActionResult Update(ScheduleTask model)
 {
     try
     {
         if (ModelState.IsValid)
         {
             // TODO: Add insert logic here
             ScheduleTaskManager.Update(model);
             //return RedirectToAction("Index");
         }
         return(View(model));
     }
     catch
     {
         return(View(model));
     }
 }
        public ActionResult Create(ScheduleTask model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    model.CompanyID = CurrentUser.CompanyID;

                    if (model.Id != 0)
                    {
                        //get default value
                        ScheduleTask objOldScheduleTask = ScheduleTaskManager.GetById(model.Id, CurrentUser.CompanyID);
                        if (objOldScheduleTask != null)
                        {
                            model.CreatedDate = objOldScheduleTask.CreatedDate;
                            model.CreatedUser = objOldScheduleTask.CreatedUser;
                        }

                        ScheduleTaskManager.Update(model);
                    }
                    else
                    {
                        // TODO: Add insert logic here
                        model.CreatedUser = CurrentUser.EmployeeCode;
                        model.CreatedDate = SystemConfig.CurrentDate;
                        ScheduleTaskManager.Add(model);
                    }
                    return(View(ViewFolder + "list.cshtml", ScheduleTaskManager.GetAll(CurrentUser.CompanyID)));
                }
            }
            catch (Exception ObjEx)
            {
                //LogHelper.AddLog(new IfindLog() {LinkUrl=Request.Url.AbsoluteUri,Exception= ObjEx.Message,Message = ObjEx.StackTrace});
                return(View(model));
            }
            return(View(model));
        }
Example #4
0
        /// <summary>
        /// Executes the task
        /// </summary>
        /// <param name="throwException">A value indicating whether exception should be thrown if some error happens</param>
        /// <param name="dispose">A value indicating whether all instances should be disposed after task run</param>
        /// <param name="ensureRunOnOneWebFarmInstance">A value indicating whether we should ensure this task is run on one farm node at a time</param>
        public void Execute(bool throwException = false, bool dispose = true, bool ensureRunOnOneWebFarmInstance = true)
        {
            //background tasks has an issue with Autofac
            //because scope is generated each time it's requested
            //that's why we get one single scope here
            //this way we can also dispose resources once a task is completed
            var scope = EngineContext.Current.ContainerManager.Scope();
            //    var scheduleTaskService = EngineContext.Current.ContainerManager.Resolve<IScheduleTaskService>("", scope);
            //var scheduleTask = ScheduleTaskManager.GetByType(this.Type, Biz.Core.Security.CustomerAuthorize.CurrentUser.CompanyID);
            var scheduleTask = ScheduleTaskManager.GetByType(this.Type, 1);

            // scheduleTaskService.GetTaskByType(this.Type); //Example Type = App.Services.Messages.QueuedMessagesSendTask, App.Services

            try
            {
                //flag that task is already executed
                var taskExecuted = false;

                //task is run on one farm node at a time?
                if (ensureRunOnOneWebFarmInstance)
                {
                    //is web farm enabled (multiple instances)?
                    var DNHConfig = EngineContext.Current.ContainerManager.Resolve <AppConfig>("", scope);
                    if (DNHConfig.MultipleInstancesEnabled)
                    {
                        var machineNameProvider = new DefaultMachineNameProvider();// EngineContext.Current.ContainerManager.Resolve<IMachineNameProvider>("", scope);
                        var machineName         = machineNameProvider.GetMachineName();
                        if (String.IsNullOrEmpty(machineName))
                        {
                            throw new Exception("Machine name cannot be detected. You cannot run in web farm.");
                            //actually in this case we can generate some unique string (e.g. Guid) and store it in some "static" (!!!) variable
                            //then it can be used as a machine name
                        }

                        //if (scheduleTask != null)
                        //{
                        //    if (DNHConfig.RedisCachingEnabled)
                        //    {
                        //        //get expiration time
                        //        var expirationInSeconds = scheduleTask.Seconds <= 300 ? scheduleTask.Seconds - 1 : 300;

                        //        var executeTaskAction = new Action(() =>
                        //        {
                        //            //execute task
                        //            taskExecuted = true;
                        //            var task = this.CreateTask(scope);
                        //            if (task != null)
                        //            {
                        //                //update appropriate datetime properties
                        //                scheduleTask.LastStartUtc = DateTime.UtcNow;
                        //                scheduleTaskService.UpdateTask(scheduleTask);
                        //                task.Execute();
                        //                this.LastEndUtc = this.LastSuccessUtc = DateTime.UtcNow;
                        //            }
                        //        });

                        //        ////execute task with lock just use if we have redis server
                        //        //var redisWrapper = EngineContext.Current.ContainerManager.Resolve<IRedisConnectionWrapper>(scope: scope);
                        //        //if (!redisWrapper.PerformActionWithLock(scheduleTask.Type, TimeSpan.FromSeconds(expirationInSeconds), executeTaskAction))
                        //        //    return;
                        //    }
                        //    else
                        //    {
                        //        //lease can't be acquired only if for a different machine and it has not expired
                        //        if (scheduleTask.LeasedUntilUtc.HasValue &&
                        //            scheduleTask.LeasedUntilUtc.Value >= DateTime.UtcNow &&
                        //            scheduleTask.LeasedByMachineName != machineName)
                        //            return;

                        //        //lease the task. so it's run on one farm node at a time
                        //        scheduleTask.LeasedByMachineName = machineName;
                        //        scheduleTask.LeasedUntilUtc = DateTime.UtcNow.AddMinutes(30);
                        //        scheduleTaskService.UpdateTask(scheduleTask);
                        //    }
                        //}
                    }
                }

                //execute task in case if is not executed yet
                if (!taskExecuted)
                {
                    //initialize and execute
                    var task = this.CreateTask(scope);
                    if (task != null)
                    {
                        this.LastStartUtc = DateTime.UtcNow;
                        if (scheduleTask != null)
                        {
                            //update appropriate datetime properties
                            scheduleTask.LastStartUtc = this.LastStartUtc;
                            ScheduleTaskManager.Update(scheduleTask);
                            //  scheduleTaskService.UpdateTask(scheduleTask);
                        }
                        task.Execute();
                        this.LastEndUtc = this.LastSuccessUtc = DateTime.UtcNow;
                    }
                }
            }
            catch (Exception exc)
            {
                this.Enabled    = !this.StopOnError;
                this.LastEndUtc = DateTime.UtcNow;

                //log error
                // var logger = EngineContext.Current.ContainerManager.Resolve<ILogger>("", scope);
                //  logger.Error(string.Format("Error while running the '{0}' schedule task. {1}", this.Name, exc.Message), exc);
                if (throwException)
                {
                    throw;
                }
            }

            if (scheduleTask != null)
            {
                //update appropriate datetime properties
                scheduleTask.LastEndUtc     = this.LastEndUtc;
                scheduleTask.LastSuccessUtc = this.LastSuccessUtc;
                ScheduleTaskManager.Update(scheduleTask);
                //scheduleTaskService.UpdateTask(scheduleTask);
            }

            //dispose all resources
            if (dispose)
            {
                scope.Dispose();
            }
        }