Skip to content

Instantly share code, notes, and snippets.

@tluyben
Last active November 30, 2017 18:56
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 tluyben/f8bc48ad9463810995fb49f8aec54340 to your computer and use it in GitHub Desktop.
Save tluyben/f8bc48ad9463810995fb49f8aec54340 to your computer and use it in GitHub Desktop.
Hangfire actions without all the serialization issues (won't survive server restart or multiple servers, but easier to make that work as well)
Random r = new Random();
static ConcurrentDictionary<int, Action> actions = new ConcurrentDictionary<int, Action>();
public static void ExecuteAction(int i)
{
Action a;
actions.Remove(i, out a);
a();
}
public void Background(Action a)
{
var i = r.Next();
actions[i] = a;
Hangfire.BackgroundJob.Enqueue(()=>MyCron.ExecuteAction(i));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment