Exemplo n.º 1
0
        private static void ExecuteTask(IScheduleTask scheduleTask, bool manualstart = false)
        {
            try
            {
                if (!scheduleTask.Enabled && !manualstart)
                {
                    return;
                }

                bool runTask = true;
                //is web farm enabled (multiple instances)?
                var grandConfig = EngineContext.Current.Resolve <GrandConfig>();
                if (grandConfig.MultipleInstancesEnabled)
                {
                    var machineNameProvider = EngineContext.Current.Resolve <IMachineNameProvider>();
                    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 (!string.IsNullOrEmpty(scheduleTask.LeasedByMachineName) && (machineName != scheduleTask.LeasedByMachineName))
                    {
                        runTask = false;
                    }
                }
                if (runTask == true)
                {
                    scheduleTask.LastStartUtc = DateTime.UtcNow;
                    scheduleTask.Execute();
                    scheduleTask.LastSuccessUtc       = DateTime.UtcNow;
                    scheduleTask.LastNonSuccessEndUtc = null;
                }
            }
            catch (Exception exc)
            {
                scheduleTask.Enabled = !scheduleTask.StopOnError;
                scheduleTask.LastNonSuccessEndUtc = DateTime.UtcNow;
                scheduleTask.LastSuccessUtc       = null;

                //log error
                var logger = EngineContext.Current.Resolve <ILogger>();
                logger.Error(string.Format("Error while running the '{0}' schedule task. {1}", scheduleTask.ScheduleTaskName, exc.Message), exc);
            }
            finally
            {
                var scheduleTaskService = EngineContext.Current.Resolve <IScheduleTaskService>();
                var taskToUpdate        = scheduleTaskService.GetTaskByType(scheduleTask.Type);

                taskToUpdate.Enabled              = scheduleTask.Enabled;
                taskToUpdate.LastStartUtc         = scheduleTask.LastStartUtc;
                taskToUpdate.LastNonSuccessEndUtc = scheduleTask.LastNonSuccessEndUtc;
                taskToUpdate.LastSuccessUtc       = scheduleTask.LastSuccessUtc;
                taskToUpdate.LeasedUntilUtc       = scheduleTask.LeasedUntilUtc;
                scheduleTaskService.UpdateTask(taskToUpdate);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initialize and execute task
        /// </summary>
        private void ExecuteTask()
        {
            IScheduleTaskService scheduleTaskService = ServiceProviderFactory.ServiceProvider.GetService <IScheduleTaskService>();

            if (!Enabled)
            {
                return;
            }

            Type type = Type.GetType(ScheduleTask.Type) ??
                        //ensure that it works fine when only the type name is specified (do not require fully qualified names)
                        AppDomain.CurrentDomain.GetAssemblies()
                        .Select(a => a.GetType(ScheduleTask.Type))
                        .FirstOrDefault(t => t != null);

            if (type == null)
            {
                throw new Exception($"Schedule task ({ScheduleTask.Type}) cannot by instantiated");
            }

            object instance = null;

            try
            {
                instance = ServiceProviderFactory.ServiceProvider.GetService(type);;
            }
            catch
            {
                //try resolve
            }

            if (instance == null)
            {
                //not resolved
                instance = ServiceProviderFactory.ServiceProvider.GetService(type);
            }

            IScheduleTask task = instance as IScheduleTask;

            if (task == null)
            {
                return;
            }

            ScheduleTask.LastStartUtc = DateTime.UtcNow;
            //update appropriate datetime properties
            scheduleTaskService.UpdateTask(ScheduleTask);
            task.Execute();
            ScheduleTask.LastEndUtc = ScheduleTask.LastSuccessUtc = DateTime.UtcNow;
            //update appropriate datetime properties
            scheduleTaskService.UpdateTask(ScheduleTask);
        }
Exemplo n.º 3
0
        private static void ExecuteTask(IScheduleTask scheduleTask, bool throwException = false, bool dispose = true, bool ensureRunOnOneWebFarmInstance = true)
        {
            //var scope = EngineContextExperimental.Current.ContainerManager.Scope();
            try
            {
                //task is run on one farm node at a time?
                if (ensureRunOnOneWebFarmInstance) //ten kod odpowiada za odpalaanie tylko na 1 serwerze
                {
                    ////is web farm enabled (multiple instances)?
                    //var grandConfig = EngineContextExperimental.Current.ContainerManager.Resolve<GrandConfig>("", scope);
                    //if (grandConfig.MultipleInstancesEnabled)
                    //{
                    //    var machineNameProvider = EngineContextExperimental.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
                    //    }

                    //    //lease can't be aquired 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);
                    //}
                }

                scheduleTask.LastStartUtc = DateTime.UtcNow;
                scheduleTask.Execute();
                scheduleTask.LastSuccessUtc       = DateTime.UtcNow;
                scheduleTask.LastNonSuccessEndUtc = null;
            }
            catch (Exception exc)
            {
                scheduleTask.Enabled = !scheduleTask.StopOnError;
                scheduleTask.LastNonSuccessEndUtc = DateTime.UtcNow;
                scheduleTask.LastSuccessUtc       = null;

                //log error
                //var logger = EngineContextExperimental.Current.ContainerManager.Resolve<ILogger>("", scope);
                //logger.Error(string.Format("Error while running the '{0}' schedule task. {1}", scheduleTask.ScheduleTaskName, exc.Message), exc);
                //if (throwException)
                //    throw;
            }
            finally
            {
                var scheduleTaskService = EngineContextExperimental.Current.Resolve <IScheduleTaskService>();
                var taskToUpdate        = scheduleTaskService.GetTaskByType(scheduleTask.Type);

                taskToUpdate.Enabled              = scheduleTask.Enabled;
                taskToUpdate.LastStartUtc         = scheduleTask.LastStartUtc;
                taskToUpdate.LastNonSuccessEndUtc = scheduleTask.LastNonSuccessEndUtc;
                taskToUpdate.LastSuccessUtc       = scheduleTask.LastSuccessUtc;
                taskToUpdate.LeasedByMachineName  = scheduleTask.LeasedByMachineName;
                taskToUpdate.LeasedUntilUtc       = scheduleTask.LeasedUntilUtc;
                scheduleTaskService.UpdateTask(taskToUpdate);

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