c# - MVC 5 Shared Long Running Task -
i have long running action/method called when user clicks button on internal mvc5 application. button shared users, meaning second person can come in , click seconds after has been clicked. long running task updating shared task window clients via signalr.
is there recommended way check if task still busy , notifying user it's still working? there recommended approach? (can't use external windows service work)
currently doing seems bad idea or wrong , it's feasible. see below sample of doing.
public static task workertask { get; set; } public jsonresult senddata() { if (workertask == null) { workertask = task.factory.startnew(async () => { // 2-15 minute long running job }); workertask = null; } else { tempdata["message"] = "data being exported. please see task window status."; } return json(url.action("export", "home"), jsonrequestbehavior.allowget); }
i don't think you're doing work @ all. see 3 issues:
- you storing
workertask
on controller (i think). new controller created every request. therefore, newworkertask
created. - if #1 weren't true, still need wrap instantiation of
workertask
in lock because multiple clients reachworkertask == null
check @ same time. - you shouldn't have long running tasks in web application. app pool restart @ time killing
workertask
.
if want skip best practices advice of "don't long running work in web app", use hostingenvironment.queuebackgroundworkitem
introduced in .net 4.5.2 kick off long running task. store variable in httpapplication.cache
indicate whether long running process has been kicked off.
this solution has more few issues (it won't work in web farm, app pool die, etc.). more robust solution use quartz.net or hangfire.
Comments
Post a Comment