Skip to content

Instantly share code, notes, and snippets.

@Workshopshed
Created October 15, 2019 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Workshopshed/c36b5578884b7566c8a758d865c8de47 to your computer and use it in GitHub Desktop.
Save Workshopshed/c36b5578884b7566c8a758d865c8de47 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Concurrent;
using System.Collections.Specialized;
using System.Threading.Tasks;
using Quartz;
using Quartz.Impl;
namespace QuartzSampleApp {
public class Program {
public static PersistentQueue cq;
private static void Main(string[] args) {
RunProgram().GetAwaiter().GetResult();
}
private static async Task RunProgram() {
try {
cq = new PersistentQueue();
await cq.Load();
IScheduler scheduler = await GetScheduler();
await createJobs(scheduler);
await scheduler.Start();
// wait for the user
await Task.Run(() => {
Console.WriteLine("Press any key");
Console.ReadKey();
}
);
await cq.Save();
await scheduler.Shutdown();
} catch (SchedulerException se) {
Console.WriteLine(se);
}
}
private static async Task createJobs(IScheduler scheduler) {
// define the job and tie it to our HelloJob class
IJobDetail ajob = JobBuilder.Create<AddJob>()
.WithIdentity("addJob", "group1")
.Build();
IJobDetail pjob = JobBuilder.Create<ProcessJob>()
.WithIdentity("processJob", "group1")
.Build();
IJobDetail sjob = JobBuilder.Create<SaveJob>()
.WithIdentity("saveJob", "group1")
.Build();
// Create Triggers
ITrigger trigger10 = CreateTrigger("trigger10", 10);
ITrigger trigger5 = CreateTrigger("trigger5", 5);
ITrigger trigger60 = CreateTrigger("trigger60", 60);
// Tell quartz to schedule the job using our trigger
await scheduler.ScheduleJob(ajob, trigger5);
await scheduler.ScheduleJob(pjob, trigger10);
await scheduler.ScheduleJob(sjob, trigger60);
}
private static async Task<IScheduler> GetScheduler() {
// Grab the Scheduler instance from the Factory
NameValueCollection props = new NameValueCollection
{
{ "quartz.serializer.type", "binary" }
};
StdSchedulerFactory factory = new StdSchedulerFactory(props);
IScheduler scheduler = await factory.GetScheduler();
return scheduler;
}
private static ITrigger CreateTrigger(string name, int seconds) {
return TriggerBuilder.Create()
.WithIdentity(name, "group1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(seconds)
.RepeatForever())
.Build();
}
private static async Task WriteInfo(string msg) {
await Console.Out.WriteLineAsync(string.Format("[{0}] [Info] {1}", DateTime.Now.ToLongTimeString(), msg));
}
public class AddJob : IJob {
public async Task Execute(IJobExecutionContext context) {
var id = Guid.NewGuid();
await WriteInfo(string.Format("Adding to queue {0}", id));
cq.Enqueue(new QueueItem { ID = id });
}
}
public class ProcessJob : IJob {
public async Task Execute(IJobExecutionContext context) {
await WriteInfo("Processing queue");
while (cq.TryDequeue(out QueueItem item)) {
await WriteInfo(string.Format("Processing {0}",item.ID.ToString()));
}
}
}
public class SaveJob : IJob {
public async Task Execute(IJobExecutionContext context) {
await cq.Save();
}
}
public class QueueItem {
public Guid ID { get; set; }
}
public class PersistentQueue : ConcurrentQueue<QueueItem> {
public async Task Load() {
//TODO: load this from storage
await WriteInfo("Loading queue");
}
public async Task Save() {
//TODO: save the queue to storage
await WriteInfo("Saving queue");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment