Created
April 11, 2011 09:55
-
-
Save prabirshrestha/913302 to your computer and use it in GitHub Desktop.
JobScheduler using Azure Toolkit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MefServiceLocator : ServiceLocatorImplBase | |
| { | |
| private ExportProvider provider; | |
| public MefServiceLocator(ExportProvider provider) | |
| { | |
| this.provider = provider; | |
| } | |
| protected override object DoGetInstance(Type serviceType, string key) | |
| { | |
| if (key == null) | |
| { | |
| key = AttributedModelServices.GetContractName(serviceType); | |
| } | |
| IEnumerable<Lazy<object>> exports = provider.GetExports<object>(key); | |
| if (exports.Any()) | |
| { | |
| return exports.First().Value; | |
| } | |
| throw new ActivationException(string.Format("Could not locate any instances of contract {0}", key)); | |
| } | |
| protected override IEnumerable<object> DoGetAllInstances(Type serviceType) | |
| { | |
| var exports = provider.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType)); | |
| return exports; | |
| } | |
| public static void InitServiceLocator(RoleEntryPoint roleEntryPoint) | |
| { | |
| InitServiceLocator(roleEntryPoint, new CompositionContainer(new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory))); | |
| } | |
| public static void InitServiceLocator(RoleEntryPoint roleEntryPoint, ExportProvider exportProvider) | |
| { | |
| Trace.WriteLine("Initializing Service Locator"); | |
| ServiceLocator.SetLocatorProvider(() => new MefServiceLocator(exportProvider)); | |
| Trace.WriteLine("Initializing Service Locator ... done"); | |
| Trace.WriteLine("Initializing IInitializer(s)"); | |
| var initializers = ServiceLocator.Current.GetAllInstances<IInitializer>(); | |
| foreach (var initializer in initializers) | |
| { | |
| initializer.Initialize(); | |
| } | |
| Trace.WriteLine("Initializing IInitializer(s) ... done"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [Export] | |
| public class SampleJob : IJob | |
| { | |
| public static void ScheduleJob(CloudEngine engine) | |
| { | |
| // * * * * * | |
| // - - - - - | |
| // | | | | | | |
| // | | | | +----- day of week (0 - 6) (Sunday=0) | |
| // | | | +------- month (1 - 12) | |
| // | | +--------- day of month (1 - 31) | |
| // | +----------- hour (0 - 23) | |
| // +------------- min (0 - 59) | |
| engine.WithJobScheduler() | |
| .ScheduleJob<SampleJob>(c => c.CronSchedule = "*/2 * * * *"); // run every 2min | |
| } | |
| public void Run() | |
| { | |
| Trace.WriteLine(string.Format("Running task at {0}.", DateTime.UtcNow.ToUniversalTime())); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class WorkerRole : RoleEntryPoint | |
| { | |
| public override void Run() | |
| { | |
| MefServiceLocator.InitServiceLocator(this); | |
| var engine = new CloudEngine(); | |
| ScheduleJobs(engine); | |
| engine.Run(); | |
| base.Run(); | |
| } | |
| private void ScheduleJobs(CloudEngine engine) | |
| { | |
| SampleJob.ScheduleJob(engine); | |
| } | |
| public override bool OnStart() | |
| { | |
| // Set the maximum number of concurrent connections | |
| ServicePointManager.DefaultConnectionLimit = 12; | |
| // For information on handling configuration changes | |
| // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357. | |
| return base.OnStart(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment