zoukankan      html  css  js  c++  java
  • 定时

    在laravel 中使用定时是非常简单的,在此只是做笔记,需要研究,仔细google  推荐一个文章http://www.cnblogs.com/chunguang/p/5714389.html

     简单介绍一下原理  

       linux 中有个contab  系统会每分钟去执行一个文件,我们可以把我们需要执行的东西,写进这个文件,让系统每分钟去调用即可,

    按照上面说的,我们只需要把我们的kernel这个文件加载到 corntab 文件中,然后让linux去每分钟执行 kernel中的 schedule ,下面的命令就是完成对接任务

    * * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1   

    具体参数意思google。  
      /path/to/artisan  是你的项目目录

      2>&1 牵扯到linux 的标准输入输出。
      >> 是追加内容
      > 是覆盖原有内容
      null 代表空设备文件
     

    ============laravel 中的运用===========

    在 laravel 我们只需要暴露一个文件即可,就是下面的kernel类


    namespace AppConsole;

    use IlluminateConsoleSchedulingSchedule;
    use IlluminateFoundationConsoleKernel as ConsoleKernel;

    class Kernel extends ConsoleKernel
    {
    /**
    * The Artisan commands provided by your application.
    *
    * @var array
    */
    protected $commands = [
    AppConsoleCommandsInspire::class, //把我们的定时任务类给添加到这个数组中
    ];

    /**
    * Define the application's command schedule.
    *
    * @param IlluminateConsoleSchedulingSchedule $schedule
    * @return void
    */
    protected function schedule(Schedule $schedule)
    {
    $schedule->command('inspire') // 每小时就会执行 inspire 这个command
    ->hourly();
    }
    }

     我们来定义我们的inspire这个命令


    namespace AppConsoleCommands;

    use IlluminateConsoleCommand;
    use IlluminateFoundationInspiring;

    class Inspire extends Command
    {
    /**
    * The name and signature of the console command. 我们定义的命令
    *
    * @var string
    */
    protected $signature = 'inspire';

    /**
    * The console command description. 命令的说明
    *
    * @var string
    */
    protected $description = 'Display an inspiring quote';

    /**
    * Execute the console command.
    *
    * @return mixed
    */
    public function handle()
    {
    $this->comment(PHP_EOL.Inspiring::quote().PHP_EOL); //定时的处理逻辑
    }
    }

    完成上面的之后,我们可以手动测试一下 php artisan inspire 就会调用 inspire 这个命令下的handle方法
    
    
      
      
     
  • 相关阅读:
    [Luogu 2261] CQOI2007 余数求和
    [Luogu 3178] HAOI2013 树上操作
    「模板」 树链剖分 HLD
    「模板」 线段树——区间乘 && 区间加 && 区间求和
    [Luogu 2221] HAOI2012 高速公路
    [Luogu 3973] TJOI2015 线性代数
    「模板」 01 Trie实现平衡树功能
    [Luogu 1640] SCOI2010 连续攻击游戏
    [Luogu 1402] 酒店之王
    [Luogu 1963] NOI2009 变换序列
  • 原文地址:https://www.cnblogs.com/lengthuo/p/7205675.html
Copyright © 2011-2022 走看看