Forum Discussion

Deepak_Kumar's avatar
Deepak_Kumar
Ideator I
6 months ago

How to check my schedular is running?

Hi,

I have a method that need to run via schedular on every 1 minute. I need to check and hold the schedular until previous trigger (schedular execution) got completed.

if the previous schedular execution is in process then we got error for next trigger, i.e. SQL transection error.

Please suggest how to overcome this situation. I do not want to increase the schedular frequency.

Thanks in Advance

Deepak

2 Replies

  • Hi Deepak,

    that´s a very good question! I did something like this in the past, but would be very happy to hear how other solved this one!

    A very simply solution: Track the last execution time with a Variable item. So you create a new Variable and update it´s value with the last execution date. You can use this Variable to check the last execution date. The required code is already part of the user guide used for Scheduler. Maybe you can use this variant to also track if your Method was completed. 

    Another idea to avoid the transaction error: Do the transactions by yourself! I right now don´t remember exactly the code. But there are functions available for BeginTransaction() and HasActiveTransaction(). So you basically control the stuff by yourself. This allows you to create some conversion server like behavior of the Scheduler.

    • Suhas's avatar
      Suhas
      Ideator I

      Instead of variable item, I will suggest to implement your own custom method lock mechanism like below.  I'm not sure how well it will work in your specific case, but it's just a suggestion.

      [embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:3aa478b3-55ce-4822-8018-c1367379d3d4:type=csharp&text=Innovator%20inn%20%3D%20this.getInnovator%28%29%3B%0D%0Astring%20methodName%20%3D%20%22YourMethodName%22%3B%0D%0A%0D%0A%2F%2F%20Check%20if%20lock%20exists%20and%20is%20active%0D%0AItem%20lockItem%20%3D%20inn.newItem%28%22Method_Lock%22%2C%20%22get%22%29%3B%0D%0AlockItem.setAttribute%28%22select%22%2C%20%22id%2C%20is_locked%2C%20start_time%22%29%3B%0D%0AlockItem.setProperty%28%22method_name%22%2C%20methodName%29%3B%0D%0AlockItem%20%3D%20lockItem.apply%28%29%3B%0D%0A%0D%0Aif%20%28%21lockItem.isError%28%29%20%26%26%20lockItem.getProperty%28%22is_locked%22%29%20%3D%3D%20%221%22%29%0D%0A%7B%0D%0A%20%20%20%2F%2F%20Optionally%20check%20timeout%20%28e.g.%2C%20stuck%20lock%29%0D%0A%20%20%20DateTime%20start%20%3D%20DateTime.Parse%28lockItem.getProperty%28%22start_time%22%29%29%3B%0D%0A%20%20%20if%20%28%28DateTime.Now%20-%20start%29.TotalMinutes%20%3C%205%29%0D%0A%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%2F%2F%20Another%20execution%20is%20running%3B%20exit%20safely%0D%0A%20%20%20%20%20%20%20return%20inn.newResult%28%22Previous%20execution%20still%20in%20progress%22%29%3B%0D%0A%20%20%20%7D%0D%0A%7D%0D%0A%0D%0A%2F%2F%20Set%20lock%0D%0AItem%20updateLock%20%3D%20inn.newItem%28%22Method_Lock%22%2C%20%22update%22%29%3B%0D%0AupdateLock.setProperty%28%22method_name%22%2C%20methodName%29%3B%0D%0AupdateLock.setProperty%28%22is_locked%22%2C%20%221%22%29%3B%0D%0AupdateLock.setProperty%28%22start_time%22%2C%20DateTime.Now.ToString%28%22s%22%29%29%3B%0D%0AupdateLock.apply%28%29%3B%0D%0A%0D%0Atry%0D%0A%7B%0D%0A%20%20%20%2F%2F%20----------%20Your%20Method%20Logic%20Here%20----------%0D%0A%0D%0A%7D%0D%0Acatch%20%28Exception%20ex%29%0D%0A%7B%0D%0A%20%20%20throw%20new%20Exception%28%22Execution%20failed%3A%20%22%20%2B%20ex.Message%29%3B%0D%0A%7D%0D%0Afinally%0D%0A%7B%0D%0A%20%20%20%2F%2F%20Release%20lock%0D%0A%20%20%20Item%20unlock%20%3D%20inn.newItem%28%22Method_Lock%22%2C%20%22update%22%29%3B%0D%0A%20%20%20unlock.setProperty%28%22method_name%22%2C%20methodName%29%3B%0D%0A%20%20%20unlock.setProperty%28%22is_locked%22%2C%20%220%22%29%3B%0D%0A%20%20%20unlock.apply%28%29%3B%0D%0A%7D]