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

Parents
  • 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.

  • 0 オフライン in reply to AngelaIp

    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.

    Innovator inn = this.getInnovator();
    string methodName = "YourMethodName";
    
    // Check if lock exists and is active
    Item lockItem = inn.newItem("Method_Lock", "get");
    lockItem.setAttribute("select", "id, is_locked, start_time");
    lockItem.setProperty("method_name", methodName);
    lockItem = lockItem.apply();
    
    if (!lockItem.isError() && lockItem.getProperty("is_locked") == "1")
    {
       // Optionally check timeout (e.g., stuck lock)
       DateTime start = DateTime.Parse(lockItem.getProperty("start_time"));
       if ((DateTime.Now - start).TotalMinutes < 5)
       {
           // Another execution is running; exit safely
           return inn.newResult("Previous execution still in progress");
       }
    }
    
    // Set lock
    Item updateLock = inn.newItem("Method_Lock", "update");
    updateLock.setProperty("method_name", methodName);
    updateLock.setProperty("is_locked", "1");
    updateLock.setProperty("start_time", DateTime.Now.ToString("s"));
    updateLock.apply();
    
    try
    {
       // ---------- Your Method Logic Here ----------
    
    }
    catch (Exception ex)
    {
       throw new Exception("Execution failed: " + ex.Message);
    }
    finally
    {
       // Release lock
       Item unlock = inn.newItem("Method_Lock", "update");
       unlock.setProperty("method_name", methodName);
       unlock.setProperty("is_locked", "0");
       unlock.apply();
    }

Reply
  • 0 オフライン in reply to AngelaIp

    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.

    Innovator inn = this.getInnovator();
    string methodName = "YourMethodName";
    
    // Check if lock exists and is active
    Item lockItem = inn.newItem("Method_Lock", "get");
    lockItem.setAttribute("select", "id, is_locked, start_time");
    lockItem.setProperty("method_name", methodName);
    lockItem = lockItem.apply();
    
    if (!lockItem.isError() && lockItem.getProperty("is_locked") == "1")
    {
       // Optionally check timeout (e.g., stuck lock)
       DateTime start = DateTime.Parse(lockItem.getProperty("start_time"));
       if ((DateTime.Now - start).TotalMinutes < 5)
       {
           // Another execution is running; exit safely
           return inn.newResult("Previous execution still in progress");
       }
    }
    
    // Set lock
    Item updateLock = inn.newItem("Method_Lock", "update");
    updateLock.setProperty("method_name", methodName);
    updateLock.setProperty("is_locked", "1");
    updateLock.setProperty("start_time", DateTime.Now.ToString("s"));
    updateLock.apply();
    
    try
    {
       // ---------- Your Method Logic Here ----------
    
    }
    catch (Exception ex)
    {
       throw new Exception("Execution failed: " + ex.Message);
    }
    finally
    {
       // Release lock
       Item unlock = inn.newItem("Method_Lock", "update");
       unlock.setProperty("method_name", methodName);
       unlock.setProperty("is_locked", "0");
       unlock.apply();
    }

Children
No Data