【问题标题】:Laravel Tasks - Storing when last ranLaravel 任务 - 存储上次运行的时间
【发布时间】:2019-06-07 09:17:54
【问题描述】:

我使用 Laravel 的任务调度制作了 CRON Job。但我需要的是在上次运行该任务时存储在某个地方,

有没有人有任何方法来存储它们,如果 Laravel 输出任何可以告诉你上次运行时间的东西?

谢谢

【问题讨论】:

    标签: php laravel scheduled-tasks


    【解决方案1】:

    不可能直接,但是如果您在每次运行时cache 一个日期时间字符串(在脚本的开头或结尾),则可以。

    Cache::rememberForever('name_of_artisan_task', function () {
        return now()->toDateTimeString();
    });
    

    该示例显示使用Cache 外观的::rememberForever 方法来创建最后一次运行任务的键/值。顾名思义,这是永久保存的。

    您可以使用 cache() 助手轻松检索此日期和时间:

    cache('name_of_artisan_task');
    

    这种方法的缺点是,如果您的缓存被清除,您将不再存储它。

    【讨论】:

      【解决方案2】:

      每次运行任务时只写日志,也可以将其推送到数据库中。

      <?php
      
      namespace App\Console\Commands\Tasks;
      
      use Illuminate\Console\Command;
      
      class ScheduledTask extends Command
      {
          public function handle()
          {
              //
              // ...handle you task
              //
              $file = 'logs/jobs/' . __CLASS__ . '.log';
              $message = 'Executed at: ' . date('Y-m-d H:i:s', time());
              file_put_contents(storage_path($file), $message, FILE_APPEND);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-10-13
        • 2020-11-11
        • 1970-01-01
        • 2012-12-03
        • 1970-01-01
        • 2010-09-12
        • 2013-11-19
        • 2015-07-27
        • 2018-10-22
        相关资源
        最近更新 更多