Home > Back-end >  Is there any framework/design pattern for storing scheduled tasks in database and retrieve them as n
Is there any framework/design pattern for storing scheduled tasks in database and retrieve them as n

Time:06-15

I want to develop an application that manages tasks that have been assigned either by a user or by the core logic itself, but these tasks are not going to be invoke immediately (maybe even a year later).

So, these tasks need to be stored in a database somehow. I want something that acts as a scheduler (like TaskScheduler) that manages tasks, but also interacts with database simultaneously.

I appreciate anyone to point me in the right direction by name some design pattern to implement this by myself or a library that is been written for this exact purpose.

CodePudding user response:

Try Hangfire (https://www.hangfire.io). Maybe there is functional you need.

CodePudding user response:

Is this going to be restricted to Windows machines? If so, consider this open source project that acts as a managed wrapper to the Windows Task Manager: https://github.com/dahall/taskscheduler

You can manage extra details in your database, and let the Windows Task Scheduler take care of the triggering logic, recording history, etc.

CodePudding user response:

This can be easily done with Newtonsoft.

First, declare an interface that represents a task factory:

public interface ITaskFactory<T> {
    Task<T> CreateTask(UnserializableContext context);
}

(where UnserializableContext is any kind of object which can't be serialized but that you need to reference in your task)

Then create a concrete class that implements it for each one of the different tasks you have to manage, for example:

public class MultiplyTaskFactory: ITaskFactory<int> {
    public int A { get; set; }
    public int B { get; set; }
    public Task<int> CreateTask(UnserializableContext context) {
        return Task.FromResult(A * B);
    }
}

public class SumTaskFactory: ITaskFactory<int> {
    public int C { get; set; }
    public int D { get; set; }
    public Task<int> CreateTask(UnserializableContext context) {
        return Task.FromResult(C   D);
    }
}

In order to store the task factories in a database we can convert them to a json.

var taskFactory1 = new MultiplyTaskFactory { A = 10, B = 2 };
var taskFactory2 = new SumTaskFactory { C = 3, D = 4 };

var taskFactory1AsJson = JsonConvert.SerializeObject(taskFactory1, Formatting.Indented, new  JsonSerializerSettings
{
    TypeNameHandling = TypeNameHandling.All
});
var taskFactory2AsJson = JsonConvert.SerializeObject(taskFactory2, Formatting.Indented, new  JsonSerializerSettings
{
    TypeNameHandling = TypeNameHandling.All
});

You can now store taskFactory1AsJson and taskFactory2AsJson in your db.

Thanks to TypeNameHandling.All settings, you can easily deserialize the task factories without warrying about the concrete type and without losing the properties they had when you stored them:

 var taskFactory1 = JsonConvert.DeserializeObject<ITaskFactory>(taskFactory1AsJson, new JsonSerializerSettings
{
    TypeNameHandling = TypeNameHandling.All
});
 await taskFactory1.CreateTask(context);
  • Related