HangfireIO / Hangfire

An easy way to perform background job processing in .NET and .NET Core applications. No Windows Service or separate process required
https://www.hangfire.io
Other
9.35k stars 1.69k forks source link

[question] persistance how ? how can hangfire backgrund jobs/tasks survive against application restart shutdown ? #2084

Closed blackholeearth closed 2 years ago

blackholeearth commented 2 years ago

i read that hangfire tasks/jobs can survive agains application restart/shutdowns ? it says that for persistance , hangfire uses mssql , sqllite , litedb , postgresql, or your-fav-db.

im more interested in this: how can hangfire convert my task (c# code block, ) into string . is it possible in c# ?? or did i misunderstood ?

morrislaw715 commented 2 years ago

In my project, I keep the jobid, jobtype and cron express in database as a config. Then call back the job from database. Everything work fine after restart.

using (SqlConnection conn = new(strConstr))

{
    conn.Open();
    using (SqlCommand cmd = new("HangFire.RecurringJob", conn))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        using (SqlDataReader dr = cmd.ExecuteReader())
        {
            while (dr.Read())
            {
                string? jobid = dr["jobid"].ToString();
                string? cron = dr["Cron"].ToString();
                RecurringJob.AddOrUpdate(jobid, () => JobType.AutoMail(jobid), cron, TimeZoneInfo.Local);

            };
        };
    };
    conn.Close();

};
blackholeearth commented 2 years ago

Ok. So you are not converting actual piece of code. I'm Relieved.

just the job id. Thanks.