Trigger recurring Hangfire job from database script - hangfire

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?

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.

Related

How To Trigger Update In Database Table Records Using Quartz.Net

I am working on an application, where the requirements is, I have to store EmailSchedules in a Database Table which contains some columns along with the EmailingDate. I have used Quartz.NET Scheduler to Schedule the Email saved in table. I created a service where there is a function name StartAsync() to Manage scheduling. I am calling the function from Startup.cs class. That means, when the application is run, then it call the function. Now the problem is, once the application is run, and then I insert a new schedule in table, the Quartz Schedular do trigger the changes as it do not know about the changes. That's why I am looking for something which will continuously monitor the table and if any changes occurred (Insert/Update/Delete) then it'll inform the Scheduler.
I tried with Worker Service but did not work.
Is there any that can help in this situation? Please help if you have any solution.

Trigger on update a table

I have a stored proc which has complicated logic. Upon completion of which I want to run another logic to calculate something. But the second logic is independent and I want to return back the control to user once the stored proc is complete. What is the best way to do this?
Right now, I am using a log table and have created a trigger on update of a column "end_time". But this does not release the thread execution.
Let me know if the question is not clear.
Update triggers are synchronous and run un the context of the UPDATE transaction. If you need to run an asynchronous process using T-SQL alone, consider Service Broker. Be aware there's a bit of a learning curve if you haven't used SB before.

SQL Server: Using triggers for workflow automation

In a media management system my task is to create a workflow automation. Currently, i have created it using SQL Server triggers and the UI using ASP.NET with JQuery.
For Ex:
When a new file enters the system the trigger works and it will update the database metadata table with some data for that file.
Millions of assets get through the system. Is it ideal to have triggers to do this process.
Is there a better way to create this automation?
Is there a "best practice" to do this kind of works?
I'm having the same issue and data enters my central asset database on several ways (may differ from client to client).
So I also want to create an easily customizable workflow in the data layer (no other dependencies)
As the other people mention, triggers may affect the parent activity.
That is overcome by writing your action that should be performed away to a queue table.
Example Trigger. Hardware.Status = "Issue Work Order"
INSERT INTO Queue (Created, Task, Completed) VALUES (GETUTCDATE(),"EXEC dbo.IssueWorkOrder(123)",0);
The insert of a record into your queue table will reduce the problems as highlighted by other user comments.
The you build a scheduling tool (hangfire, sql tasks, or whatever), that execute tasks in the queue in the data order it wAS added.
Now, of course in practice it's not as simple as that. You will have to address the following:
What if the step fails2
Dependencies of previous steps to first have been completed
Multiple operators changing a record. (the deploy time between the job step being executed, and another person updating the same record.
I guess #2 and #3 is an issue with any workflow engine / pipleline. To address this a locking mechanism must be put in place.

Database Job Scheduling

I have a procedure written in PLJava that sends out updates over JMS in my postgres database.
What I would like to do is have that function called on an interval (every 15 seconds) internally in the database (preferably not from an outside process). Is this possible? Any ideas?
If you need no external access, you are presumably able to modify the database design so that you don't need the update at all. Can you explain more about what the update is doing?
As depesz said, you could use either cron or pgAgent, but they are only able to go down to a one minute granularity, not 15 seconds. Considering sleeping inside the stored procedure until the next iteration is not a good idea, because you will have an open transaction for all that time which is a really bad idea.
Strict answer: it is not possible. Since you don't want outside process, and PostgreSQL doesn't support jobs - you are out of luck.
If you'll reconsider using outside processes, then you're most likely want something like cron, or better yet pgagent.
On absolutely other hand - what do you need to do that has to happen every 30 seconds? this seems like a problem with design.
First, you'll spend the least amount of effort if you just go with a cron job.
However, if you were starting from scracth: You are trying to periodically replicate rows from your database. I think you are looking at a replication queue.
The PGQ project (used for Londiste replication, both from Skype's SkyTools) has a queue that you can use independently. When configuring it, you set a maximum event count, and a loop delay, before batched events are generated. You can get batches spaced by no more than 15 seconds that way. You now have to produce the events that will be batched, using a trigger that calls pgq.insert_event; and consume the queues. The consumer can call your PL/Java stored proc; you'll have to rewrite the procedure to send everything in the batch instead of scanning the base table for new events.
As far as I know postgresql doesn't support scheduled tasks. You'll need to use a script with cron or at (depending on your operating system.)
Sounds like you're doing sort of replication? Every 15s sounds like a lot of updates. Could you setup a trigger (or a number of triggers) instead of polling?
If you are using JMS why not just have th task wait for input on the queue?
Per your depesz comment, you have a PL/Java stored procedure that "flushes out database tables (updates) as java objects". Since you want it to run in 15 second intervals, it must be processing a batch of updates each time. Rather than processing a batch of updates in a stored procedure every 15 seconds, why not process them one at a time when they happen via an after update trigger and eliminate the need for a timed interval. If you are aggregrating data from multiple tables to build your objects than add the triggers to you upper most tables only.
In my case the problem was that agent couldn't authorize to database so after I've made all connections trusted from localhost the service started successfully and job works fine
for more information about error you should see into windows event viewer or eq in unix based system. see my config file C:\Program Files\PostgreSQL\10\data\pg_hba.conf

What is the best way to execute an MS SQL Server 2000 DTS package from a trigger?

I looked around and found some ideas about how to do this, but no definitive best way. One of the ideas was to use sp_start_job to kick off an SQL Server Agent job that runs the DTS package. If this is the best way to do it, then the next question would be, "How do I schedule a DTS package from a job and make it non-recurring?"
Thanks,
Tim
xp_cmdshell would allow you to execute dtsrun.
I wouldn't suggest tying this kind of functionality to a trigger. Triggers are supposed to be fast. I don't think there is any way to launch a DTS package that will be as fast as I would want a trigger to be. If this resonates with you, then I would suggest having your trigger simply insert a row into a special table, and then have a job that executes as often as you need for your purpose (every minute? every 10 seconds?) that monitors this table and kicks off the appropriate DTS package as needed.
Instead of using xp_cmdshell, I did this:
When a certain value in a table changes, the trigger uses msdb.sp_start_job to start a job. This job should not run on a schedule, only when initiated by a user. I set the job schedule to run one time, which is now in the past, and I unchecked the enabled box.
This job has one step, which is DTSRun /~Z0xHEXENCRYPTEDVALUE. The DTS package copies some rows from this server to another server on a different platform and on success resets values in the table with the trigger for next time. The trigger checks a table value before calling sp_start_job, so that the job starts only under certain conditions, not every time.
Since sp_start_job runs asyhchronously the trigger completes quickly. The only drawback to this is that I need to poll the value that was reset on success and either let the user know it worked, or after some time out period, it did not work.
The alternative would be to use xp_cmdshell if I needed synchronous operation, which might not be a good idea from inside of a trigger.