0

We have two scheduled tasks and the requirement is to make them run at the same time everyday. One of them is Async (Task1) and the other Non-Async (Task2).

Sitecore by default, auto updates its Last Run field after a task is completed, and for this reason, tasks do not run at the same time everyday. For this to happen, an alternative is to update the Last Run field's value to our desired DateTime.

I was able to achieve this for the Async task, by following this article. The trick is to have a try-catch statement and keep the update code inside the finally block.

private void MethodForAsyncTask()
{
  try
  {
     //code to get some data
  }
  catch
  {
  }
  finally
  {
    //code to update the last run field value
  }
}

This works only if the task is Async. Because, code in the finally block gets executed after Sitecore updates the Last run field. All good.

But now, I need to know, how can I do this for a synchronous task, i.e updating the Last Run field value after the task is completed.

One idea I had, was to create another scheduled task to run for every 1 minute and update the Last Run field value of Task2. But how do I know if the task2 was successfully completely today.

using Sitecore v8.0 Update-5

4
  • You can do this using Sitecore Powershell Task. It will allows you to configure the job either in async or non-async. More info is found here: sitecorepowershell.gitbooks.io/sitecore-powershell-extensions/… Jul 3, 2017 at 15:14
  • @HishaamNamooya The task is already configured. I just want to update the time in the LastRun field after the task is executed. I know how to do that for an async task. But now I have to do that for a non-async task
    – sukesh
    Jul 3, 2017 at 15:28
  • Show your existing code, and point out what breaks it for a "sync" task, please. Jul 3, 2017 at 15:42
  • @MarkCassidy I haven't tried anything yet, because I do not where to start. Should I use a pipeline or is there any existing function that I can override or some where else I can know that the task is completed and I can update its field now...
    – sukesh
    Jul 3, 2017 at 15:45

1 Answer 1

3

Since you did not provide any context of the task, let's say that you have a task which needs to update the fields of Sitecore Items. So, when the time comes, the task will trigger the code and the method you provided and it will be something as below:

public void Run()
{
    var items = Factory.GetDatabase("master").GetItem("Path or Id here").Children.ToList();

    foreach(var item in items)
    {
        // Perform the update of the field here
    }

    // Once all items have been processed, the task is completed.
    // Perform the update the the Last Run field here.

    using (new SecurityDisabler())
    {
        // Update the item that contains the Last Run field here
    }
}

Normally, you can use the listed modules below to perform your requirement.

  1. Sitecron
  2. Sitecore Powershell

I would recommend to use the Sitecore Powershell since it already has an in-built task functionality and also you can set if the task is async or not.

Moreover, it will automatically update the Last Run field on the task item. So, all you will need to do is to create the Powershell script. You can use the powershell script to call your C# code.

Update

For you to be able to update the field Last Run based on your requirement, you need to add a handler to the item:saved event. Then, in the code

  • First check if the item being saved is the task item.
  • If yes, proceed with the Last Run field update else skip.

References

1
  • I created the handler to update the field on saved event. This approach worked. Thank you.
    – sukesh
    Jul 4, 2017 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.