Scheduler in MP ? (for a plugin) (1 Viewer)

Boris

Portal Pro
June 5, 2005
69
0
France
I'm doing a plugin which have one thing to do every day. How can I do it the "better" way in MediaPortal ?

Actually, I think I will do it like this :
- When Mp start, start a timer with the elaps time to the next time.
- If MP stop, I kill the timer and end the plugin.
- If timer ok, I will do the work and then start a new timer.

explain with an example :wink:
==> job every day at 9:00
- start MP at 0:00 => start a timer for 9:00
- start MP at 5:00 => start a timer for 4:00
- timer trigger : do the job and start a new timer for 24:00
- start MP at 10:00 => do the job and start a timer for 23:00

Is it ok ? How can I do this in an other better way ?
 

Smirnuff

Portal Pro
December 7, 2004
630
3
United Kingdom
I've been working on a new job dispatcher/scheduler for MP that may be of interest to you...

As the low down on how its implemented:

  • scheduler runs in its own thread
    use a priority queue
    thread safe
    uses .NET thread pool for worker threads
    based on ideas presented by .NET 2's BackgroundWorker object
    reports correct stack for worker thread exceptions

Currently it is working very well and is very easy to work with, the major limitation right now is that it doesn't persist the outstanding jobs, something that it needs to be truly useful and suitable for use in a service.

Examples of how to schedule a job:

Code:
		Job job = new Job();

		job.DoWork += new DoWorkEventHandler(RefreshWeather);
		job.RunWorkerCompleted += new RunWorkerCompletedEventHandler(RefreshWeatherCompleted);
		job.Name = "RefreshWeather";
		job.Argument = "JAXX0085";
		job.Dispatch();

		...
		...

The work handler:

Code:
		static void RefreshWeather(object sender, DoWorkEventArgs e)
		{
			using(WeatherReader reader = new WeatherReader((string)e.Argument))
				e.Result = reader.Read(); 
		}

The completion handler:

Code:
		static void RefreshWeatherCompleted(object sender, RunWorkerCompletedEventArgs e)
		{
			try
			{
				foreach(WeatherForecast weather in (WeatherCollection)e.Result)
				{
					Console.WriteLine("  Location: {0}", weather.Location.Name);
					Console.WriteLine("      Time: {0}", weather.Location.Time);
					Console.WriteLine("       Day: {0}", weather.Day);
					Console.WriteLine("  Latitude: {0}", weather.Location.Latitude);
					Console.WriteLine("Longtitude: {0}", weather.Location.Longtitude);
					Console.WriteLine("   Sunrise: {0}", weather.Location.Sunrise);
					Console.WriteLine("    Sunset: {0}", weather.Location.Sunset);
				}
			}
			catch(Exception exception)
			{
				Console.WriteLine("Application.RefreshWeatherCompleted: {0}", exception.Message);
			}


			// re-schedule for 30 minutes time			
			((Job)sender).Dispatch(TimeSpan.FromMinutes(30));
		}

I'm happy to zip up the code and the test app if you would like to play around with this, once persistance has been implemented I will be presenting this to the other team members in the hope that it will be used as the defacto sheduler in MP.

At some point in the future I hope to to add ISynchronizeInvoke support for the controls used in MP to simplify manipulating UI elements from within the worker threads.

The Dispatch method allows you to work with absolute and relative times.

If it is to be used in the project then I'll also be adding support for waking the computer from standby.

Cheers,
Smirnoff.
 

Boris

Portal Pro
June 5, 2005
69
0
France
Cool ! It seems to me that it is a really good thing (and simple). Yes you can send it for me to implement ( boris [at] play-different.net ).

But if I use it, how can I then release my plugin ?
 
A

Anonymous

Guest
Great idea a scheduler in MP....but to do what ?

An idea for your plugin...

I use PowerScheduler Plugin to hibernate and wakeup (or shutdown) MP when a TV record is scheduled.
It runs fine but it would be great if different tasks could be scheduled before and after recording.

After a record :

When you are programing some TV records, MP could ask for each record if you want to hibernate or shutdown your PC after....For example, hibernate after the first record, than wakeup before the next one and then shutdown when the least record is done.

Before a record :

The easy way is to stay on MP home and then your PC hibernate or shutdown (like in PowerScheduler), or to have a "orange button" to put PC on hibernation (and waiting for scheduled tasks like TV records).

Another way is to check when MP starts if a scheduled task is on the way (for this day, or week, or month ?)...and if "yes" (some TV records for example), MP could ask what to do : hibernate now, hibernate when on home for xxx minutes, do nothing... etc ?

Of course, a scheduler can be used for several tasks but TV record is an obvious one...

Best Regards !
 

Smirnuff

Portal Pro
December 7, 2004
630
3
United Kingdom
Sorry for the delay Boris! The code has been added to CVS and hopefully should be available to you via anonymous CVS in a few hours time.

To use you need to make sure that core.dll is added as a reference to your plugin, this is almost certainely the case already.

The namespace for the dispatcher is MediaPortal.Dispatcher and the rest as per the above.

I hope to be able to improve upon this implementation with the priority on adding the ability to persist the job queue, followed by resume from standby functionality and more (with the ultimate aim of ditching the current Power scheduler but that could be sometime away).
 

Boris

Portal Pro
June 5, 2005
69
0
France
Smirnoff said:
Sorry for the delay Boris! The code has been added to CVS and hopefully should be available to you via anonymous CVS in a few hours time.

To use you need to make sure that core.dll is added as a reference to your plugin, this is almost certainely the case already.

The namespace for the dispatcher is MediaPortal.Dispatcher and the rest as per the above.

I hope to be able to improve upon this implementation with the priority on adding the ability to persist the job queue, followed by resume from standby functionality and more (with the ultimate aim of ditching the current Power scheduler but that could be sometime away).

No problem at all ! Thanks a lot. I will check the CVS some time later. I will post here if I succeed or have a problem using it.
 
B

BoLzI

Guest
Hello,

how much time do you need to make a final release of your plugin?

I need this to schedule some Diashows, Movies and Slideshows (implemented with the Plugin My Slideshows).

All this 3 things should start at a fixed time, which i can set in the scheduler.

Although it will be very nice if you can control and run external programs from the MP Scheduler.

Thx, Waiting for a reply,

Kind Regards,

BoLzI
 

Boris

Portal Pro
June 5, 2005
69
0
France
BoLzI said:
...how much time do you need to make a final release of your plugin?...
The scheduler is not a plugin, it is a native implementation in MediaPortal. I'm using it since Smirnoff say it is available and it is working great !
Here is my code using the scheduler :
Code:
TimeSpan next = main.NextTimeSpanToDownload();
Job job = new Job();
job.DoWork += new System.ComponentModel.DoWorkEventHandler(job_DoWork);
job.Name = "RefreshEPGfr";
job.Dispatch(next);

and then ...

private void job_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) {
// do the job :)
}
"main.NextTimeSpanToDownload()" is one method calculating the TimeSpan I need.
 
B

BoLzI

Guest
how is this code than implemented and started/used?
 

bradsjm

Retired Team Member
  • Premium Supporter
  • October 28, 2005
    77
    0
    New York City, USA
    Home Country
    United States of America United States of America
    Is anyone using this code? I've run into issues with trying to use it - it kicks of my method but then it is generating exceptions.
     

    Users who are viewing this thread

    Top Bottom