Skip to content

Instantly share code, notes, and snippets.

@jtabuloc
Last active July 17, 2019 16:54
Show Gist options
  • Save jtabuloc/ca3db40505fa8e513caee38d7a0d78eb to your computer and use it in GitHub Desktop.
Save jtabuloc/ca3db40505fa8e513caee38d7a0d78eb to your computer and use it in GitHub Desktop.
This is a very simple solution to get around with azure scheduled web job idle time (WEBJOBS_IDLE_TIMEOUT).
namespace ScheduleWebJobHandler
{
/// <summary>
/// Example of console app that use WebJobTimeoutHandler
/// </summary>
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("Scheduled Job started.");
using (var webJobTimeoutHandler = new WebJobTimeoutHandler(10))
{
webJobTimeoutHandler.Execute(new ScheduleJob());
}
System.Console.WriteLine("Scheduled Job finished.");
}
}
}
using System.Threading.Tasks;
namespace ScheduleWebJobHandler
{
/// <summary>
/// Example web job that implement IScheduledJob
/// Put your long running process inside execute method
/// You may pass the IWebJobTimeoutHandler in your own class and use SetState to change the state if you want
/// </summary>
public class ScheduleJob : IScheduledJob
{
public void Execute(IWebJobTimeoutHandler webJobTimeoutHandler)
{
webJobTimeoutHandler.SetState("ScheduleJob");
// For this example we put task delay as a representation of long running process.
System.Console.WriteLine("ScheduleJob start running and will take 5 mins to finish");
Task.Delay(50000).Wait();
}
}
}
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ScheduleWebJobHandler
{
public class WebJobTimeoutHandler : IWebJobTimeoutHandler, Disposable
{
private readonly Timer Timer;
private object CurrentState { get; set; }
private object PreviousState { get; set; }
public WebJobTimeoutHandler(int intervalInSeconds)
{
Timer = new Timer((state) => TimerCallBack(), null, 0, intervalInSeconds * 1000);
}
/// <summary>
/// Use this function if you want to change the state. If the state is not changed
/// in certain period of time the TimercallBack will write in the console to
/// signal that we are still alive.
/// </summary>
/// <param name="state"></param>
public void SetState(object state)
{
CurrentState = state;
}
/// <summary>
/// Without task run the timercallback method will not write in console making
/// the console app looks idle and cause web job idle time issue
/// </summary>
/// <param name="scheduledJob"></param>
public void Execute(IScheduledJob scheduledJob)
{
Task.Run(() => scheduledJob.Execute(this)).Wait();
}
/// <summary>
/// This method will be triggered every x time which was indicated in ctor. It ensure that
/// once the current state is not changed because of long running activity in certain period
/// of time we will write or ping in the console to signal that we are still alive and not idle
/// to avoid WEBJOBS_IDLE_TIMEOUT exception
/// </summary>
private void TimerCallBack()
{
if (PreviousState == null || !PreviousState.Equals(CurrentState))
{
PreviousState = CurrentState;
return;
}
System.Console.WriteLine($"{CurrentState} is still running...");
PreviousState = CurrentState;
}
public void Dispose()
{
Timer?.Dispose();
}
}
/// <summary>
/// Implement this interface in your web job class
/// </summary>
public interface IScheduledJob
{
void Execute(IWebJobTimeoutHandler webJobTimeoutHandler);
}
public interface IWebJobTimeoutHandler
{
void SetState(object state);
void Execute(IScheduledJob scheduledJob);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment