Skip to content

Commit

Permalink
Improved the implementation of the TaskHelper class
Browse files Browse the repository at this point in the history
  • Loading branch information
abjerner committed Dec 15, 2021
1 parent 2d8891f commit 6292480
Showing 1 changed file with 106 additions and 18 deletions.
124 changes: 106 additions & 18 deletions src/Skybrud.Essentials.Umbraco/Scheduling/TaskHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,51 @@
using System.IO;
using System.Linq;
using System.Text;
using Skybrud.Essentials.Time;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Sync;

namespace Skybrud.Essentials.Umbraco.Scheduling {

/// <summary>
/// Helper class for running tasks inside Umbraco.
/// </summary>
public class TaskHelper {


private readonly IRuntimeState _runtimeState;
private readonly IServerRoleAccessor _serverRoleAccessor;
private readonly IHostingEnvironment _hostingEnvironment;

#region Properties

/// <summary>
/// Gets the current runtime level, via the injected <see cref="IRuntimeState"/>.
/// </summary>
public RuntimeLevel RuntimeLevel => _runtimeState.Level;

/// <summary>
/// Gets the current server role, via the injected <see cref="IServerRoleAccessor"/>.
/// </summary>
public ServerRole ServerRole => _serverRoleAccessor.CurrentServerRole;

#endregion

#region Constructors

public TaskHelper(IHostingEnvironment hostingEnvironment) {
/// <summary>
/// Initializes a new task helper instance.
/// </summary>
/// <param name="runtimeState">A reference to the current <see cref="IRuntimeState"/>.</param>
/// <param name="serverRoleAccessor">A reference to the current <see cref="IServerRoleAccessor"/>.</param>
/// <param name="hostingEnvironment">A reference to the current <see cref="IHostingEnvironment"/>.</param>
public TaskHelper(IRuntimeState runtimeState, IServerRoleAccessor serverRoleAccessor, IHostingEnvironment hostingEnvironment) {
_runtimeState = runtimeState;
_serverRoleAccessor = serverRoleAccessor;
_hostingEnvironment = hostingEnvironment;
}

#endregion

#region Member methods
Expand All @@ -44,7 +71,7 @@ protected virtual string GetTaskName(object task) {

default:
return task.GetType().FullName;

}

}
Expand All @@ -60,11 +87,26 @@ public DateTime GetLastRunTime(object task) {
return File.Exists(path) ? File.GetLastWriteTime(path) : DateTime.MinValue;
}

/// <summary>
/// Returns the last run time of the specified <paramref name="task"/>. If the task has not yet been run,
/// <see cref="DateTime.MinValue"/> is returned instead.
/// </summary>
/// <param name="task">The task.</param>
/// <returns>The last run time of the task, or <see cref="DateTime.MinValue"/> if the task has not yet run.</returns>
public DateTime GetLastRunTimeUtc(object task) {
string path = Path.Combine(GetTaskDirectoryPath(GetTaskName(task)), "LastRunTime.txt");
return File.Exists(path) ? File.GetLastWriteTimeUtc(path) : DateTime.MinValue;
}

/// <summary>
/// Returns whether the specified <paramref name="task"/> should run.
/// </summary>
/// <param name="task">The task.</param>
/// <param name="now">The current time.</param>
/// <param name="hour">The hour the task should run.</param>
/// <param name="minute">The minute the task should run.</param>
/// <param name="weekdays">The days of the week the task should run.</param>
/// <returns><c>true</c> if the task should run; otherwise <c>false</c>.</returns>
public bool ShouldRun(object task, DateTime now, int hour, int minute, DayOfWeek[] weekdays) {

// Determine when the task is supposed to run on the current day
Expand All @@ -84,18 +126,54 @@ public bool ShouldRun(object task, DateTime now, int hour, int minute, DayOfWeek

}

/// <summary>
/// Returns whether the specified <paramref name="task"/> should run.
/// </summary>
/// <param name="task">The task.</param>
/// <param name="hour">The hour the task should run.</param>
/// <returns><c>true</c> if the task should run; otherwise <c>false</c>.</returns>
public bool ShouldRun(object task, int hour) {
return ShouldRun(GetTaskName(task), DateTime.Now, hour, 0, null);
}


/// <summary>
/// Returns whether the specified <paramref name="task"/> should run.
/// </summary>
/// <param name="task">The task.</param>
/// <param name="hour">The hour the task should run.</param>
/// <param name="minute">The minute the task should run.</param>
/// <returns><c>true</c> if the task should run; otherwise <c>false</c>.</returns>
public bool ShouldRun(object task, int hour, int minute) {
return ShouldRun(GetTaskName(task), DateTime.Now, hour, minute, null);
}


/// <summary>
/// Returns whether the specified <paramref name="task"/> should run.
/// </summary>
/// <param name="task">The task.</param>
/// <param name="hour">The hour the task should run.</param>
/// <param name="minute">The minute the task should run.</param>
/// <param name="weekdays">The days of the week the task should run.</param>
/// <returns><c>true</c> if the task should run; otherwise <c>false</c>.</returns>
public bool ShouldRun(object task, int hour, int minute, params DayOfWeek[] weekdays) {
return ShouldRun(GetTaskName(task), DateTime.Now, hour, minute, weekdays);
}

/// <summary>
/// Returns whether the specified <paramref name="task"/> should run.
/// </summary>
/// <param name="task">The task.</param>
/// <param name="interval">The interval by which the task should run.</param>
/// <returns><c>true</c> if the task should run; otherwise <c>false</c>.</returns>
public bool ShouldRun(object task, TimeSpan interval) {
string taskName = GetTaskName(task);
return GetLastRunTimeUtc(taskName) < DateTime.UtcNow.Subtract(interval);
}

/// <summary>
/// Sets the last run time of the specified <paramref name="task"/>.
/// </summary>
/// <param name="task">The task.</param>
public void SetLastRunTime(object task) {

// Get the task name
Expand All @@ -106,12 +184,17 @@ public void SetLastRunTime(object task) {

// Calculate the path to the fiule
string path = Path.Combine(directory, "LastRunTime.txt");

// Save the file to the disk
File.WriteAllText(path, "Hello there!", Encoding.UTF8);
File.WriteAllLines(path, new [] { "Hello there!", EssentialsTime.UtcNow.Iso8601 }, Encoding.UTF8);

}

/// <summary>
/// Appends the value of <paramref name="stringBuilder"/> to the log of the specified <paramref name="task"/>.
/// </summary>
/// <param name="task">The task</param>.
/// <param name="stringBuilder">The string builder with the value to be logged.</param>
public void AppendToLog(object task, StringBuilder stringBuilder) {

// Get the task name
Expand All @@ -128,22 +211,27 @@ public void AppendToLog(object task, StringBuilder stringBuilder) {

}

public bool ShouldRun(object task, TimeSpan interval) {
string taskName = GetTaskName(task);
return GetLastRunTimeUtc(taskName) < DateTime.UtcNow.Subtract(interval);
}

protected string GetTaskDirectoryPath(string taskName) {
/// <summary>
/// Returns the path for the directory for the specified task name.
/// </summary>
/// <param name="taskName">The name of the task.</param>
/// <returns>The directory path for the task.</returns>
protected virtual string GetTaskDirectoryPath(string taskName) {
string directory = taskName.IndexOf("Limbo.", StringComparison.Ordinal) == 0 ? "Limbo" : "Skybrud";
string path = Path.Combine(Constants.SystemDirectories.Umbraco, directory, "Tasks", taskName);
return _hostingEnvironment.MapPathContentRoot(path);
}

private void EnsureTaskDirectory(string taskName, out string path) {

/// <summary>
/// Ensures that there is a valid directory for the task with the specified <paramref name="taskName"/>.
/// </summary>
/// <param name="taskName">The name of the task.</param>
/// <param name="path">The path to the task directory.</param>
protected virtual void EnsureTaskDirectory(string taskName, out string path) {
path = GetTaskDirectoryPath(taskName);
if (!Directory.Exists(path)) Directory.CreateDirectory(path);
}

#endregion

}
Expand Down

0 comments on commit 6292480

Please sign in to comment.