Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Task Scheduler

Well in an application I am creating I wanted to have a scheduler that holds a data about incoming jobs and the time remaining for them to be executed so I have created a very naive implementation that I think is far from what it should be.

Implementation

Interfaces

public interface IQueue
{
    string Name { get; set; }
    int Priority { get; set; }
}

Classes

public class TaskScheduler : Queue, IQueue
{
    public string Name { get; set; }
    public int Priority { get; set; }

    public TaskScheduler(string name, int priority)
    {
        Name = name;
        Priority = priority;
    }

    public void Add(string work, TimeSpan execution)
    {
        Enqueue(new Job {Work = work, Execution = execution});
    }

    public Job Get()
    {
        return (Job) Dequeue();
    }
}

public class Job
{
    public string Work { get; set; }
    public TimeSpan Execution { get; set; }

    public override string ToString()
    {
        return string.Format("{0} will be excuted in {1}", Work, Execution);
    }
}

Usage

    var schedulerss = new List<TaskScheduler>
                         {
                             new TaskScheduler("Meetings", 2), 
                             new TaskScheduler("Jobs", 1)
                         };

    schedulerss = schedulerss.OrderBy(element => element.Priority).ToList(); //ORDER THE schedulers according to the Priority

    schedulerss.Find(schedulers => schedulers.Name == "Meetings").Add("Meet Barack Obama", new TimeSpan(1, 0, 0, 15));
    schedulerss.Find(schedulers => schedulers.Name == "Jobs").Add("Make a cheese sandwich :D", new TimeSpan(0, 2, 0, 15));

    var meetingschedulers = schedulerss.Find(schedulers => schedulers.Name == "Meetings");

    if (null != meetingschedulers)
    {
        foreach (var job in meetingschedulers)
        {
            Console.WriteLine(job);
        }
    }

    Console.Read();

Question(s)

Will this code work well or I just missed all the thing out and there is better approach(s) to do such thing?

Request

Please I would like a very detailed answer about what is the drawback(s) of the code I have provided and how could I create a better reused code (just for other people who may find this topic useful).

like image 649
Roman Ratskey Avatar asked Nov 03 '22 18:11

Roman Ratskey


1 Answers

Well as you can see by your usage, having the name of the scheduler inside the scheduler makes the code a bit funky... Does the scheduler really needs to know his own name? I'd use a Dictionary<string, TaskScheduler>() instead, and put their names there..

Also your queue always returns a Job, so you should use a generic queue, Queue<Job>

I've made some modifications

Implementation

Interfaces

public interface IQueue
{
    int Priority { get; set; }
}

Classes

public class TaskScheduler : Queue<Job>, IQueue
{
    public int Priority { get; set; }

    public TaskScheduler(int priority)
    {
        Priority = priority;
    }

    public void Add(string work, TimeSpan execution)
    {
        Enqueue(new Job { Work = work, Execution = execution });
    }

    public Job Get()
    {
        return Dequeue();
    }
}

public class Job
{
    public string Work { get; set; }
    public TimeSpan Execution { get; set; }

    public override string ToString()
    {
        return string.Format("{0} will be excuted in {1}", Work, Execution);
    }
}

Usage

        var schedulers = new Dictionary<string, TaskScheduler>();

        schedulers.Add("Meetings", new TaskScheduler(2));
        schedulers.Add("Jobs", new TaskScheduler(1));

        schedulers["Meetings"].Add("Meet Barack Obama", new TimeSpan(1, 0, 0, 15));
        schedulers["Jobs"].Add("Make a cheese sandwich :D", new TimeSpan(0, 2, 0, 15));

        if (schedulers.ContainsKey("Meetings"))
        {
            foreach (var job in schedulers["Meetings"])
            {
                Console.WriteLine(job);
            }
        }

Other suggestions

Since you do work with multiple schedulers, I'd suggest to make some sort of schedulerController, which will handle prioritizing for you, and other things you might want to add.

Edit example per request

I don't know exactly you want to do with your schedulers, but I meant something like this, a utility class:

public class TaskSchedulerController
{
    private Dictionary<string, TaskScheduler> _scedulers;

    public TaskSchedulerController()
    {
        _scedulers = new Dictionary<string, TaskScheduler>();
    }

    public void Add(string name, int priority)
    {
        _scedulers.Add(name, new TaskScheduler(priority));
    }

    public IEnumerable<string> GetJobsOfScheduler(string name)
    {
        if (_scedulers.ContainsKey(name))
        {
            foreach (var job in _scedulers[name])
            {
                yield return job.ToString();
            }
        }
    }
}
like image 77
Ron Sijm Avatar answered Nov 08 '22 04:11

Ron Sijm