Search code examples
hangfirehangfire-sqlhangfire-console

Trigger recurring Hangfire job from database script


I have a pretty complicated sql script which cleans up my database and will be run manually from time to time. Immediatelly after the script has run I have to start a recurring hangfire job which is usually executed only once per day.

In order not to forget to run the script I would like to trigger the execution directly from the sql script. Is there a simple way to modify one of the hangfire tables to achieve this task?

Hangfire core version is 1.7.5

UPDATE: Unfortunately I have not found any hangfire table containing the recurring jobs. Therefore I guess they are not persisted at all. Would it be possible to run a job by just inserting a new line in the HangFire.Job table? Or is this a bad idea?


Solution

  • Sorry for not following up this question here on stackoverflow. Due to the lack of a pure hangfire solution I have finally implemented some custom logic with the help of a separate "DatabaseState" table.

    My DatabaseState table has the following fields: DateOfDbUpdate(datetime) and HangfireExecuted(bool). Whenever I run my complicated SQL-script I just insert a new entry in the table with the current time and HangfireExecuted = false.

    In the code I have created a recurring hangfire job which checks every few seconds if there is a new entry in the DatabaseState table with HangfireExecuted == false. If it finds such an entry it will start the cleanup job.

    Unfortunately this is not a very clean and direct solution. I would be happy if someone else comes up with a real hangfire solution.