Esempio n. 1
0
        //public Func<I_ASBaseC, T> ToRun { get; set; }


        /*
         * public virtual object Run(int timeSleep = 10)
         * {
         *  return ToRun(this);
         * }*/

        public void Launch_Task(Func <object> Ending = null, int delay = 50)
        {
            TaskRunning = Task.Run(
                async
                    () =>
            {
                await Task.Delay(delay);
                TaskToRun();
            }
                , Objet.CancelToken);

            if (Ending != null)
            {
                var kwa = TaskRunning.ContinueWith((ant) => Ending());
            }
        }
Esempio n. 2
0
        /// <summary>运行任务</summary>
        protected bool ProcessTask(Job task, DateTime dt)
        {
            // 当它不存在
            if (!task.Enable)
            {
                return(true);
            }

            // 是否处于运行时间内
            if (!task.Schedule.InTime(dt))
            {
                return(false);
            }

            // 成功状态,且未到下次运行时间
            if (task.Status == JobStatus.Success && !NeedToRunAgain(task.LastRunDt, task.Success, dt))
            {
                return(true);
            }

            // 失败状态,但又要开始一个周期了
            if (task.Status == JobStatus.Failure && NeedToRunAgain(task.LastRunDt, task.Success, dt))
            {
                task.Failure.TryTimes = 0;
                task.Status           = JobStatus.Waiting;
            }

            // 处于等待状态;或与上次运行成功的间隔时间足够了;或上次运行失败的间隔时间足够了(且失败次数未饱和);
            if (
                (task.Status == JobStatus.Waiting && NeedToRunAgain(task.LastRunDt, task.Success, dt)) ||
                (task.Status == JobStatus.Success && NeedToRunAgain(task.LastRunDt, task.Success, dt)) ||
                (task.Status == JobStatus.Failure && NeedToRunAgain(task.LastRunDt, task.Failure, dt) && task.Failure.TryTimes < task.Failure.MaxTimes)
                )
            {
                TaskRunning?.Invoke(task, "");

                // 先运行依赖任务
                if (task.Dependency != null && task.Dependency.Count > 0)
                {
                    foreach (var t in task.Dependency)
                    {
                        if (!ProcessTask(t, dt))
                        {
                            return(false);
                        }
                    }
                }

                // 再运行本任务
                task.LastRunDt = dt;
                task.Status    = JobStatus.Waiting;
                try
                {
                    var runner = task.JobRunner;

                    // TODO: 改为异步任务或者线程
                    if (runner.Run(dt, task.Data))
                    {
                        task.Status           = JobStatus.Success;
                        task.Failure.TryTimes = 0;
                        TaskSuccess?.Invoke(task, "");
                        return(true);
                    }
                    else
                    {
                        task.Status = JobStatus.Failure;
                        task.Failure.TryTimes++;
                        TaskFailure?.Invoke(task, "");
                        return(false);
                    }
                }
                catch (Exception ex)
                {
                    task.Status = JobStatus.Failure;
                    task.Failure.TryTimes++;
                    TaskFailure?.Invoke(task, ex.Message);
                }
            }

            return(false);
        }
Esempio n. 3
0
        /// <summary>
        /// Implements the template on the provided path
        /// </summary>
        /// <param name="ImplementationPath">The path to implement the template on</param>
        /// <param name="Data">Data to replace on the files while preprocessing</param>
        /// <returns>void</returns>
        public async Task ImplementAsync(String ImplementationPath, IDictionary <String, String> Data)
        {
            ImplementationPath = Path.GetFullPath(ImplementationPath);
            // Checks if the implementation folder exists
            Directory.CreateDirectory(ImplementationPath);

            // List all files and ignores the ones that should be
            var files = GetFiles(TPath, TConfig.ignore
                                 .Concat(new String[1] {
                "template.json"
            }));

            // Copies all files to their targets
            var copier = new FileCopier( );

            copier.FileCopied += Copier_FileCopied;
            await copier.CopyFilesAsync(
                files.Select(file => Path.Combine(TPath, file)),
                files.Select(file => Path.Combine(ImplementationPath, file))
                );

            // List files to process
            var processable = GetFiles(ImplementationPath, TConfig.ignore
                                       .Concat(TConfig.processIgnore)
                                       .Concat(new String[1] {
                "template.json"
            }));

            // Processes the files
            for (var i = 0; i < processable.Length; i++)
            {
                ProcessFile(Path.Combine(ImplementationPath, processable[i]), Data);
                FileProcessed?.Invoke(processable[i], i, processable.Length);
            }

            foreach (var command in TConfig.setupTasks)
            {
                TaskRunning?.Invoke(command);
                Process task = null;
                try
                {
                    task = Process.Start(new ProcessStartInfo
                    {
                        FileName         = command.Before(' '),
                        Arguments        = command.After(' '),
                        WindowStyle      = ProcessWindowStyle.Hidden,
                        CreateNoWindow   = false,
                        UseShellExecute  = true,
                        LoadUserProfile  = true,
                        WorkingDirectory = ImplementationPath
                    });
                    // Runs the process for 5 minutes in max

                    task.WaitForExit(TConfig.tasksTimeout);

                    if (task.ExitCode != 0)
                    {
                        throw new Exception($"Exit code was not 0: {task.ExitCode}.");
                    }

                    var elapsed = task.ExitTime - task.StartTime;
                    if (elapsed.TotalMilliseconds >= TConfig.tasksTimeout - 2)
                    {
                        throw new Exception($"Process achieved the timeout: {elapsed.ToString ( )}");
                    }
                }
                catch (Exception ex)
                {
                    TaskInterrupted?.Invoke(command, ex);
                }
                finally
                {
                    task.Close( );
                    task.Dispose( );
                }
            }
        }
Esempio n. 4
0
 public static void DoingSomething(bool doing, string description = "", int percentComplete = 0)
 {
     TaskRunning?.Invoke(doing, description, percentComplete);
 }