Esempio n. 1
0
    /// <summary>
    /// Resume the task.
    /// </summary>
    public void Resume()
    {
        TaskWrapper.Log(
            this,
            "Resuming.",
            TaskWrapper.TaskWrapperLogType.Info);

        this.Enabled = true;
    }
Esempio n. 2
0
    /// <summary>
    /// Pause the task.
    /// </summary>
    public void Pause()
    {
        TaskWrapper.Log(
            this,
            "Paused.",
            TaskWrapper.TaskWrapperLogType.Info);

        this.Enabled = false;
    }
Esempio n. 3
0
    /// <summary>
    /// Remove the task from the list of entries.
    /// </summary>
    public void Remove()
    {
        this.Pause();

        TaskWrapper.Log(
            this,
            "Removing.",
            TaskWrapper.TaskWrapperLogType.Info);

        var entry = TaskWrapper.Entries.SingleOrDefault(n => n.Name == this.Name);

        if (entry == null)
        {
            return;
        }

        TaskWrapper.Entries.Remove(entry);

        this.Interval  = null;
        this.DateTimes = null;
    }
Esempio n. 4
0
    /// <summary>
    /// Attempt to run the action now.
    /// </summary>
    public async Task Run()
    {
        if (this.Removed)
        {
            return;
        }

        if (!this.Enabled)
        {
            TaskWrapper.Log(
                this,
                "Skipping run since task is disabled.",
                TaskWrapper.TaskWrapperLogType.Warning);

            this.QueueNextRun(null);
            return;
        }

        TaskWrapper.Log(
            this,
            "Preparing to run function.",
            TaskWrapper.TaskWrapperLogType.Info);

        this.LastAttemptedRun = DateTime.Now;

        // Run VerifyAtRuntime
        if (this.VerifyAtRuntime != null)
        {
            TaskWrapper.Log(
                this,
                "Found verify function, running.",
                TaskWrapper.TaskWrapperLogType.Info);

            var postpone = this.VerifyAtRuntime.Invoke(this);

            if (postpone.Ticks > 0)
            {
                TaskWrapper.Log(
                    this,
                    string.Format(
                        "Postponing the function {0} seconds.",
                        postpone.TotalSeconds),
                    TaskWrapper.TaskWrapperLogType.Warning);

                this.QueueNextRun(postpone);
                return;
            }
        }

        try {
            this.LastRunStarted = DateTime.Now;
            this.IsRunning      = true;

            TaskWrapper.Log(
                this,
                "Running main function.",
                TaskWrapper.TaskWrapperLogType.Info);

            var task = new Task <bool>(() => this.Action(this));
            task.Start();
            await task;

            this.IsRunning    = false;
            this.LastRunEnded = DateTime.Now;

            var span = this.LastRunEnded - this.LastRunStarted;

            TaskWrapper.Log(
                this,
                string.Format(
                    "Run completed successfully. Took {0} seconds.",
                    span.TotalSeconds),
                TaskWrapper.TaskWrapperLogType.Info);
        }
        catch (Exception ex) {
            TaskWrapper.Log(
                this,
                ex.Message,
                TaskWrapper.TaskWrapperLogType.Exception,
                ex);

            this.Exceptions.Add(ex);
            this.IsRunning    = false;
            this.LastRunEnded = DateTime.Now;
        }

        // Queue up the next run.
        this.QueueNextRun(null);
    }
Esempio n. 5
0
    /// <summary>
    /// Queue up the next run.
    /// </summary>
    public async Task QueueNextRun(TimeSpan?postpone)
    {
        TimeSpan?timeSpan = null;

        // Queue up based on postpone time-span.
        if (postpone.HasValue)
        {
            timeSpan = postpone.Value;
        }

        // Queue up the next interval.
        if (timeSpan == null &&
            this.Interval.HasValue)
        {
            timeSpan = this.Interval.Value;
        }

        // Queue up the next time-to-run.
        if (timeSpan == null &&
            this.DateTimes != null &&
            this.DateTimes.Any())
        {
            var now = DateTime.Now;

            foreach (var dateTime in this.DateTimes.OrderBy(t => t))
            {
                var dt = new DateTime(
                    now.Year,
                    now.Month,
                    now.Day,
                    dateTime.Hour,
                    dateTime.Minute,
                    dateTime.Second);

                if (dt <= now)
                {
                    continue;
                }

                timeSpan = dt - now;
                break;
            }
        }

        // Task up the next run.
        if (timeSpan == null)
        {
            return;
        }

        TaskWrapper.Log(
            this,
            string.Format(
                "Next run will be in {0}.",
                timeSpan),
            TaskWrapper.TaskWrapperLogType.Info);

        // Wait the alotted time.
        await Task.Delay(timeSpan.Value);

        // Attempt to run the action now.
        Run();
    }