【问题标题】:Run sitecore scheduled task at the same time every day每天同一时间运行sitecore计划任务
【发布时间】:2011-06-23 14:57:55
【问题描述】:

我需要在每天的准确时间运行 sitecore 计划任务。 目前任务安排如下:

Schedule: 20100201T235900|20200201T235900|127|23:59:59
Last run: 2011 06 22 01:32:25

任务执行大约需要 5 分钟,所以 'Last run' 会逐渐滑落,并且会越来越晚地运行。

我的主要想法是创建一个 Windows 计划任务,该任务调用网络服务并重置相关任务的上次运行时间。

还有其他方法吗?我是否缺少一些可以更轻松地实现这一点的配置属性?

【问题讨论】:

    标签: sitecore sitecore6


    【解决方案1】:

    我为此开发了自己的解决方案。

    首先,将您的任务修改为每 1 分钟左右运行一次。它必须经常运行。 您无需强制任务运行一次,而是让任务在您喜欢的时间执行其功能,然后等到第二天再次执行该功能:

    在这个例子中,我强制我的任务在凌晨 03:00 到凌晨 04:00 之间运行一次:

    public void Execute(Item[] itemArray, CommandItem commandItem, ScheduleItem scheduleItem)
    {
      if (!IsDue(scheduleItem))
        return;
    
      // DO MY STUFF!!
    }
    
    /// <summary>
    /// Determines whether the specified schedule item is due to run.
    /// </summary>
    /// <remarks>
    /// The scheduled item will only run between defined hours (usually at night) to ensure that the
    /// email sending will not interfere with daily operations, and to ensure that the task is only run
    /// once a day. 
    /// Make sure you configure the task to run at least double so often than the time span between 
    /// SendNotifyMailsAfter and SendNotifyMailsBefore
    /// </remarks>
    /// <param name="scheduleItem">The schedule item.</param>
    /// <returns>
    ///     <c>true</c> if the specified schedule item is due; otherwise, <c>false</c>.
    /// </returns>
    private bool IsDue(ScheduleItem scheduleItem)
    {
      DateTime timeBegin;
      DateTime timeEnd;
    
      DateTime.TryParse("03:00:00", out timeBegin);
      DateTime.TryParse("04:00:00", out timeEnd);
    
    
    
      return (CheckTime(DateTime.Now, timeBegin, timeEnd) && !CheckTime(scheduleItem.LastRun, timeBegin, timeEnd));
    }
    
    private bool CheckTime(DateTime time, DateTime after, DateTime before)
    {
      return ((time >= after) && (time <= before));
    }
    

    查看文章了解更多详细信息: http://briancaos.wordpress.com/2011/06/28/run-sitecore-scheduled-task-at-the-same-time-every-day/

    【讨论】:

    • 为什么不在 03:00:00 和 04:00:00 之间每分钟运行一次任务?
    • @KristianNissen IsDue 方法的 return 语句中的第二个 CheckTime 调用查看 scheduleItem.LastRun 日期,因此如果任务已经在时间窗口内执行一次,它将不会t 再次运行。
    • 使用 Sitecore 8.1,每次运行命令时都会更新 Last Run,即使您的逻辑没有执行。这可以通过映射到“上次执行”等新字段来解决。
    【解决方案2】:

    尝试开启异步执行,这应该会导致“上次运行”时间立即更新,即使任务仍在运行时也是如此。此外,对于您的最后一个参数,您可以使用“1.00:00:00”而不是“23:59:59”让它每 1 天执行一次。

    编辑:另一种可能性是 DatabaseAgent 的频率。它多久运行一次?如果它仅每 10 分钟运行一次(默认情况下),则检查不一定会在您希望的那一刻发生。只有当它醒来时。尝试将时间间隔更改为 1 分钟或更短。

      <!-- Agent to process schedules embedded as items in a database -->
      <agent type="Sitecore.Tasks.DatabaseAgent" method="Run" interval="00:10:00">
        <param desc="database">master</param>
        <param desc="schedule root">/sitecore/system/tasks/schedules</param>
        <LogActivity>true</LogActivity>
      </agent>
    

    【讨论】:

    • 我所有的计划任务都设置为异步执行,但它们仍然会向前推进。不幸的是,这个问题似乎没有很好的解决方案。
    • 应该能够将滑动保持在最低限度,请参阅上面的编辑以获得更多想法。
    • 我刚刚检查过,数据库代理设置为每 10 分钟运行一次,但我想即使我将其降低到 1 分钟。日程安排可能每天都在溜走。
    【解决方案3】:

    很难让某些东西每天在同一时间运行。大多数情况下,这是因为 Sitecore 以滴答声而不是一天中的时间来考虑时间。所以应用程序回收之间的时间是滴答声。

    我们之前通过实现 Windows 计划任务也做过同样的事情。我们的任务调用wget(Windows 版本),wget 向我们网站上的特殊页面发出请求。我们对该页面有 IP 限制,因此只有服务器可以通过 wget 请求访问它。我们从来没有遇到过任何问题,如果我们需要手动运行代码,我们要么从服务器点击页面,要么只登录服务器并在那时运行计划任务。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-19
      • 1970-01-01
      • 2018-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-16
      相关资源
      最近更新 更多