`
[assembly: OwinStartup(typeof(Poc.QueueApp.Web.Startup))]
namespace Poc.QueueApp.Web
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
Framework.Initialize();
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
UnityConfig.RegisterUnity();
ConfigureAuth(app);
HangfireConfig.RegisterHangfire(app);
}
}
}
namespace Poc.QueueApp.Web.App_Start
{
public class HangfireConfig
{
public static void RegisterHangfire(IAppBuilder app)
{
GlobalConfiguration.Configuration.UseSqlServerStorage(Globals.DatabaseHangfire);
app.UseHangfireDashboard();
var options = new BackgroundJobServerOptions { Queues = Globals.QueueNames };
app.UseHangfireServer(options);
}
}
}
namespace Poc.QueueApp.Common
{
public static class Globals
{
public const string BaseAddress = "http://localhost:9900/";
public const string DatabaseHangfire = "Server=kbarrow1-vm;Database=Hangfire;Integrated Security=true";
public static string[] QueueNames => Queues.Select(kvp => kvp.Value).ToArray();
public static readonly Dictionary<int, string> Queues = new Dictionary<int, string>
{
{0, "critical"},
{1, "job_queue"},
{2, "adhoc"},
};
public static string QueueName(QueueName name) { return Queues[(int) name]; }
}
}
namespace Poc.QueueApp.Common.Interfaces
{
public interface IJob
{
bool Execute(string payload);
}
public interface IJobPayload
{
string JobName { get; set; }
string JobQueueName { get; set; }
int Id { get; set; }
JobType JobType { get; set; }
CronExpression Cron { get; set; }
}
}
namespace Poc.QueueApp.Web.Jobs
{
public class CriticalJob : IJob
{
[Queue("critical")]
public bool Execute(string payload)
{
try
{
var command = JsonConvert.DeserializeObject<Payload>(payload);
File.WriteAllText($"c:\\logs\\CriticalJob-CriticalQueue-Success-{DateTime.Now:yy-MMM-dd-HH-mm-ss}.txt",
$"In Queue: {Globals.QueueName(QueueName.Critical)}\r\npublic class CriticalJob\r\n{payload}");
}
catch (Exception ex)
{
File.WriteAllText($"c:\\logs\\CriticalJob-CriticalQueue-Failure-{DateTime.Now:yy-MMM-dd-HH-mm-ss}.txt",
$"In Queue: {Globals.QueueName(QueueName.Critical)}\r\npublic class CriticalJob\r\n{ex.Message}\r\n{ex.StackTrace}");
}
return true;
}
public class Payload : IJobPayload
{
public string JobName { get; set; }
public string JobQueueName { get; set; }
public int Id { get; set; }
public JobType JobType { get; set; }
public CronExpression Cron { get; set; }
//---
public string ExtraData { get; set; }
}
}
}
// Home page portion (for testing) - runs 3 jobs in 3 different ways
namespace Poc.QueueApp.Web.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Title = "Home Page";
// Check the status and reachability of hangfire...
var job = new CriticalJob();
var payload = new CriticalJob.Payload
{
JobType = JobType.AdHoc,
JobQueueName = Globals.QueueName(QueueName.Critical),
Id = 999,
JobName = job.ClassName(),
Cron = new CronExpression(0, 0, 1, "1", "0", 2016),
ExtraData = "This is a test job to make sure the Critical Queue is up and running when this web app first launches!",
};
var payloadString = JsonConvert.SerializeObject(payload);
// Test directly...
var id = BackgroundJob.Enqueue(() => job.Execute(payloadString));
ViewBag.DirectResponse = $"ID: {id}";
// Test by pulling the JOB from Unity...
payload.ExtraData = "This is a test job to make sure the Critical Queue is up and running and reachable by using UNITY when this web app first launches!";
payloadString = JsonConvert.SerializeObject(payload);
var container = UnityConfig.GetConfiguredContainer();
var command = JsonConvert.DeserializeObject<CriticalJob.Payload>(payloadString);
var job2 = container.Resolve<IJob>(command.JobName);
id = BackgroundJob.Enqueue(() => job2.Execute(payloadString)); // <== ENQUEUES IN THE DEFAULT QUEUE BUT DOES NOT RUN
ViewBag.UnityResponse = $"ID: {id}";
// Test via REST service call...
payload.ExtraData = "This is a test job to make sure the Critical Queue is up and running and reachable by a REST call when this web app first launches!";
var response = payload.PostJobToQueue(); // <== ENQUEUES IN THE DEFAULT QUEUE BUT DOES NOT RUN
ViewBag.RestResult = $"REST Response: {response}";
ViewBag.RestResponse = $"ID: {response.Content.ReadAsStringAsync().Result}";
return View();
}
}
}
// REST Handler...
namespace Poc.QueueApp.Web.Controllers
{
public class QueueController : BatcApiController
{
[HttpPost]
public string Post()
{
var json = Request.Content.ReadAsStringAsync().Result;
var queue = new Queue();
return queue.Execute(json);
}
}
}
namespace Poc.QueueApp.Web.Managers
{
public class Queue
{
public string Execute(string payload)
{
var container = UnityConfig.GetConfiguredContainer();
var command = JsonConvert.DeserializeObject<Payload>(payload);
var job = container.Resolve<IJob>(command.JobName);
string id;
switch (command.JobType)
{
case JobType.AdHoc:
id = BackgroundJob.Enqueue(() => job.Execute(payload));
break;
case JobType.Continuation:
// removed for clarity...
break;
case JobType.Delayed:
// removed for clarity...
break;
default:
// removed for clarity...
break;
}
return id;
}
// A generic (base) Payload object to allow JsonConvert to work
private class Payload : IJobPayload
{
public string JobName { get; set; }
public string JobQueueName { get; set; }
public int Id { get; set; }
public JobType JobType { get; set; }
public CronExpression Cron { get; set; }
}
}
}
`
What am I doing wrong?