just get the job done!

Mirosław Pragłowski / @mpraglowski

Who is...

just get the job done...

  • but...

  • make sure it will be done
  • on demand
  • repeat on...
  • ... and if it fails

simplest thing

DEMO

simplest thing

  • command line app
  • windows sheduler
  • WMI to manage it all

all I need is a little more than web request...

in ancient ages..

  • ...somewhere around year 2011...
  • ...it was not easy to run task in background (for web app)

in ancient ages..

    public class JobHost : IRegisteredObject
    {
        public JobHost() {
            HostingEnvironment.RegisterObject(this);
        }
        public void Stop(bool immediate) {
            HostingEnvironment.UnregisterObject(this);
        }
        public void DoWork(Action work) {
            work();
        }
    }
    ...
    new JobHost().DoWork(() => { /* ... get the job done... */ });
    ...
    

code from http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx/

QueueBackgroundWorkItem

  • introduced in .NET 4.5.2
  • to be used for simple long running tasks
  • not very long :)
  • its still in web server process
  • ASP.NET runtime will try to delay AppDomain shutdown

QueueBackgroundWorkItem

DEMO

QBWI limitations

  • only in ASP.NET
  • better be ready in 30s :)
  • no execution context, need to copy values from HttpContext
  • no guarantee that it will be ever executed
  • no guarantee that it will run to completion
  • CancellationToken is only a suggestion to shutdown, if you not obey your job will be killed

Quartz.NET

  • open source library
  • works in any .net app, single or in cluster
  • load balance & failover
  • persistence (configurable)
  • extensible by plugins

just...

    Install-Package Quartz

    public class MyJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            // ... just get the job done here
        }
    }
    

jobs

  • simple class, public ctor, implements IJob interface
  • allow to pass context (IJobExecutionContext)
  • IStatefulJob, IInteruptableJob
  • JJobExecutionContext & IJobDataMap

triggers

  • simple triggers
  • daily time triggers
  • calendar interval triggers
  • cron triggers

listeners

  • allows to execute code when sth happened
  • TriggerListener
  • JobListener
  • SchedulerListener

scheduler

  • use helpers to build jobs & triggers
  • remoting to schedule jobs from app
  • handle with care!
  • ...and remember about security!

Quartz.NET

DEMO

but why not
windows scheduler?

  • simpler API
  • no WMI required (full trust?)
  • persistence & simpler backups
  • custom monitoring & jobs management
  • extensible by plugins & custom implemetation

and why use
windows scheduler?

  • another service to deploy & maintain
  • no code required :)
  • integrated with admin management & monitoring tools

Azure WebJob

  • various process types: node, php, cmd, bash, python
  • continous (running all the time), scheduled & on demand runs
  • windows scheduler scaled to the cloud :)
  • still in preview (install nuget package with -pre option)

Azure WebJob

DEMO

some links

QueueBackgroundWorkItem

Quartz.NET

Azure WebJobs

Available at

https://talks.praglowski.com/get-the-job-done/