Create an API to schedule jobs dynamically after runtime. Your API can accept input via an HTTP Get/Put/Post/Delete etc, then run an instance of anything within your code upon the API call, using the data that you give it.
For example, say you have a hard coded Task A and Task B in your code and you want to schedule them to run dynamically using different parameters. You can create an API that will run the desired task at the specified time, using the parameters that you choose.
[HttpPost]
public IHttpActionResult Post([FromBody]TaskDto task)
{
var job = "";
if(task.TaskName == "TaskA"){
job = BackgroundJob.Schedule(() => RunTaskA(task.p1,task.p2), task.StartTime);
}
if(task.TaskName == "TaskB"){
job = BackgroundJob.Schedule(() => RunTaskB(task.p1,task.p2), task.StartTime);
}
if(!string.IsNullOrWhiteSpace(task.ContinueWith) && !string.IsNullOrWhiteSpace(job)){
if(task.ContinueWith == "TaskB"){
BackgroundJob.ContinueWith(job, () => RunTaskB(task.p3,task.p4));
}
if(task.ContinueWith == "TaskA"){
BackgroundJob.ContinueWith(job, () => RunTaskA(task.p3,task.p4));
}
}
return Ok(job)
}
Then you can call the API using a JSON POST (example using javascript)
// Sending JSON data to start scheduled task via POST
//
var xhr = new XMLHttpRequest();
var url = "https://www.example.com/api/scheduletask";
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
var json = JSON.parse(xhr.responseText);
}
};
var data = JSON.stringify({"TaskName": "TaskA", "ContinueWith": "TaskB",
"StartTime": "2-26-2018 10:00 PM", "p1": "myParam1", "p2": true,
"p3": "myParam3", "p4": false});
xhr.send(data);
And for completeness of the example here is the TaskDto class for this example
public class TaskDto
{
public string TaskName { get; set; }
public string ContinueWith { get; set; }
public DateTime StartTime { get; set; }
public string p1 { get; set; }
public bool p2 { get; set; }
public string p3 { get; set; }
public bool p4 { get; set; }
}
BackgroundJob.Enqueue(() => Console.WriteLine("Fire-and-forget"));
, but that looks like code to me. How can one add a new task, change an existing one, or remove one without changing C# source code? It seems that adding a task should be possible through a menu, a JSON file, or similar. – Iodism