<article><h1>Laravel 的任务调度(计划任务)功能 Task Scheduling</h1><ul><li><a href="#introduction">简介</a></li><li><a href="#defining-schedules">定义调度</a><ul><li><a href="#schedule-frequency-options">调度频率设置</a></li><li><a href="#preventing-task-overlaps">避免任务重复</a></li><li><a href="#maintenance-mode">维护模式</a></li></ul></li><li><a href="#task-output">任务输出</a></li><li><a href="#task-hooks">任务钩子</a></li></ul><p><a name="introduction"></a></p><h2><a href="#introduction">简介</a></h2><p>在过去,开发者必须为每个需要调度的任务生成单独的 Cron 项目。然而令人头疼的是任务调度不受版本控制,并且需要 SSH 到服务器上来增加 Cron 条目。</p><p>Laravel 命令调度器允许你在 Laravel 中对命令调度进行清晰流畅的定义,并且仅需要在服务器上增加一条 Cron 项目即可。你的调度已经定义在 <code class=" language-php">app<span class="token operator">/</span>Console<span class="token operator">/</span>Kernel<span class="token punctuation">.</span>php</code> 文件的 <code class=" language-php">schedule</code> 方法中。为了方便你开始,在该方法内包含了一个简单的例子。你可以随意增加调度到 <code class=" language-php">Schedule</code> 对象中。</p><h3>启动调度器</h3><p>使用调度器时,你只需要把 Cron 添加到你的服务器,如果你不知道如何添加到服务器,你可以使用 <a href="https://forge.laravel.com">Laravel Forge</a> 服务来管理你的 Cron 。</p><pre class=" language-php"><code class=" language-php"><span class="token operator">*</span> <span class="token operator">*</span> <span class="token operator">*</span> <span class="token operator">*</span> <span class="token operator">*</span> php <span class="token operator">/</span>path<span class="token operator">/</span>to<span class="token operator">/</span>artisan schedule<span class="token punctuation">:</span>run <span class="token operator">></span><span class="token operator">></span> <span class="token operator">/</span>dev<span class="token operator">/</span><span class="token keyword">null</span> <span class="token number">2</span><span class="token operator">></span><span class="token operator">&</span><span class="token number">1</span></code></pre><p>该 Cron 将于每分钟调用一次 Laravel 命令调度器,当 <code class=" language-php">schedule<span class="token punctuation">:</span>run</code> 命令执行时, Laravel 会评估你的计划任务并运行预定任务。</p><p><a name="defining-schedules"></a></p><h2><a href="#defining-schedules">定义调度</a></h2><p>你可以将所有的计划任务定义在 <code class=" language-php">App\<span class="token package">Console<span class="token punctuation">\</span>Kernel</span></code> 类的 <code class=" language-php">schedule</code> 方法中。在开始之前,先让我们来看看一个任务的调度示例。在该例子中,我们计划了一个会在每天午夜被调用的 <code class=" language-php">闭包</code>。该 <code class=" language-php">闭包</code> 将运行数据库查询语句来清除某个数据表:</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter"><?php</span>
<span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Console</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">DB</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Console<span class="token punctuation">\</span>Scheduling<span class="token punctuation">\</span>Schedule</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Console<span class="token punctuation">\</span>Kernel</span> <span class="token keyword">as</span> ConsoleKernel<span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">Kernel</span> <span class="token keyword">extends</span> <span class="token class-name">ConsoleKernel</span>
<span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">/**
* 应用提供的 Artisan 命令
*
* @var array
*/</span>
<span class="token keyword">protected</span> <span class="token variable">$commands</span> <span class="token operator">=</span> <span class="token punctuation">[</span>
\<span class="token scope">App<span class="token punctuation">\</span>Console<span class="token punctuation">\</span>Commands<span class="token punctuation">\</span>Inspire<span class="token punctuation">::</span></span><span class="token keyword">class</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/**
* 定义应用的命令调度
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/</span>
<span class="token keyword">protected</span> <span class="token keyword">function</span> <span class="token function">schedule<span class="token punctuation">(</span></span>Schedule <span class="token variable">$schedule</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token variable">$schedule</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">call<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table<span class="token punctuation">(</span></span><span class="token string">'recent_users'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">delete<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">daily<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre><p>除了计划 <code class=" language-php">闭包</code> 调用,你还能计划 <a href="/docs/5.4/artisan">Artisan 命令</a>以及系统命令操作。举个例子,你可以使用 <code class=" language-php">command</code> 方法传参命令名称或者命令类名称来计划一个 <code class=" language-php">Artisan</code> 命令:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$schedule</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">command<span class="token punctuation">(</span></span><span class="token string">'emails:send --force'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">daily<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$schedule</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">command<span class="token punctuation">(</span></span><span class="token scope">EmailsCommand<span class="token punctuation">::</span></span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'--force'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">daily<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><code class=" language-php">exec</code> 命令可发送命令到操作系统上:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$schedule</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">exec<span class="token punctuation">(</span></span><span class="token string">'node /home/forge/script.js'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">daily<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="schedule-frequency-options"></a></p><h3>调度频率设置</h3><p>当然,你可以针对你的任务来分配多种调度计划:</p><table><thead><tr><th>方法</th><th>描述</th></tr></thead><tbody><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">cron<span class="token punctuation">(</span></span><span class="token string">'* * * * * *'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>自定义调度任务</td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">everyMinute<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td> 每分钟执行一次任务</td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">everyFiveMinutes<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>每五分钟执行一次任务</td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">everyTenMinutes<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>每十分钟执行一次任务</td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">everyThirtyMinutes<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>每半小时执行一次任务</td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">hourly<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>每小时执行一次任务</td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">hourlyAt<span class="token punctuation">(</span></span><span class="token number">17</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>每一个小时的第 17 分钟运行一次</td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">daily<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>每到午夜执行一次任务</td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">dailyAt<span class="token punctuation">(</span></span><span class="token string">'13:00'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>每天的 13:00 执行一次任务</td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">twiceDaily<span class="token punctuation">(</span></span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>每天的 1:00 和 13:00 分别执行一次任务</td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">weekly<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>每周执行一次任务</td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">monthly<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>每月执行一次任务</td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">monthlyOn<span class="token punctuation">(</span></span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token string">'15:00'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>在每个月的第四天的 15:00 执行一次任务</td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">quarterly<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>每季度执行一次任务</td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">yearly<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>每年执行一次任务</td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">timezone<span class="token punctuation">(</span></span><span class="token string">'America/New_York'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>设置时区</td></tr></tbody></table><p>这些方法可以合并其它限制条件以生成更精确的调度。例如在某周的某几天运行调度。举个例子,计划一个每周周一的调度:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">// 每周一的下午一点钟运行
</span><span class="token variable">$schedule</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">call<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> //
</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">weekly<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">mondays<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">at<span class="token punctuation">(</span></span><span class="token string">'13:00'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// 工作日中从早上 8 点钟运行到下午 5 点
</span><span class="token variable">$schedule</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">command<span class="token punctuation">(</span></span><span class="token string">'foo'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">weekdays<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">hourly<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">timezone<span class="token punctuation">(</span></span><span class="token string">'America/Chicago'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">between<span class="token punctuation">(</span></span><span class="token string">'8:00'</span><span class="token punctuation">,</span> <span class="token string">'17:00'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>下方列出其它额外限制条件:</p><table><thead><tr><th>方法</th><th>描述</th></tr></thead><tbody><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">weekdays<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>限制任务在工作日</td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">sundays<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>限制任务在星期日</td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">mondays<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>限制任务在星期一</td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">tuesdays<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>限制任务在星期二</td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">wednesdays<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>限制任务在星期三</td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">thursdays<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>限制任务在星期四</td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">fridays<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>限制任务在星期五</td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">saturdays<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>限制任务在星期六</td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">between<span class="token punctuation">(</span></span><span class="token variable">$start</span><span class="token punctuation">,</span> <span class="token variable">$end</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>限制任务运行在开始到结束时间范围内</td></tr><tr><td><code class=" language-php"><span class="token operator">-</span><span class="token operator">></span><span class="token function">when<span class="token punctuation">(</span></span>Closure<span class="token punctuation">)</span><span class="token punctuation">;</span></code></td><td>限制任务基于一个为真的验证</td></tr></tbody></table><h4>时间范围限制</h4><p><code class=" language-php">between</code> 方法可以用来限制一天中某个时间范围内:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$schedule</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">command<span class="token punctuation">(</span></span><span class="token string">'reminders:send'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">hourly<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">between<span class="token punctuation">(</span></span><span class="token string">'7:00'</span><span class="token punctuation">,</span> <span class="token string">'22:00'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>类似的,<code class=" language-php">unlessBetween</code> 方法可以用来排除时间段:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$schedule</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">command<span class="token punctuation">(</span></span><span class="token string">'reminders:send'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">hourly<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">unlessBetween<span class="token punctuation">(</span></span><span class="token string">'23:00'</span><span class="token punctuation">,</span> <span class="token string">'4:00'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>为真验证限制条件</h4><p><code class=" language-php">when</code> 方法可以用来判断是否要运行任务,主要基于一个指定的为真验证的运行结果。如果指定的 <code class=" language-php">闭包</code> 返回 <code class=" language-php"><span class="token boolean">true</span></code>,且没有其它限制条件存在,那么这个任务将会被继续运行。</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$schedule</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">command<span class="token punctuation">(</span></span><span class="token string">'emails:send'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">daily<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">when<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><code class=" language-php">skip</code> 是 <code class=" language-php">when</code> 的颠倒意味。<code class=" language-php">skip</code> 方法返回 <code class=" language-php"><span class="token boolean">true</span></code> 的话,计划就不会运行。</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$schedule</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">command<span class="token punctuation">(</span></span><span class="token string">'emails:send'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">daily<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">skip<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>当链式调用了 <code class=" language-php">when</code> 方法时,计划命令只有在所有的 <code class=" language-php">when</code> 条件返回 <code class=" language-php"><span class="token boolean">true</span></code> 时才运行。</p><p><a name="preventing-task-overlaps"></a></p><h3>避免任务重复</h3><p>默认情况,即便之前相同的任务主体仍未结束,现有计划任务依旧会被运行。为了避免这个问题,你可以使用 <code class=" language-php">withoutOverlapping</code> 方法:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$schedule</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">command<span class="token punctuation">(</span></span><span class="token string">'emails:send'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">withoutOverlapping<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>在这个例子中,如果没有其它 <code class=" language-php">emails<span class="token punctuation">:</span>send</code> <a href="/docs/5.4/artisan">Artisan 命令</a> 在运行的话,此任务将于每分钟被运行一次。当你有些任务运行时间过长,且无法预测出具体所需时间时, <code class=" language-php">withoutOverlapping</code> 方法将会特别有帮助。</p><p><a name="maintenance-mode"></a></p><h3>维护模式</h3><p>当应用进入 <a href="/docs/5.4/configuration#maintenance-mode">维护模式</a> 时,默认情况下 Laravel 的调度功能将会停止运行。这是因为我们考虑到你可能在维护模式下做一些破坏性的维护工作,我们不想让任务调度对这些工作造成干扰。然而,如果你想强制某个任务在维护模式下运行的话,你可以使用 <code class=" language-php">evenInMaintenanceMode</code> 方法:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$schedule</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">command<span class="token punctuation">(</span></span><span class="token string">'emails:send'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">evenInMaintenanceMode<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="task-output"></a></p><h2><a href="#task-output">任务输出</a></h2><p>Laravel 调度器为任务调度输出提供多种便捷方法。首先,通过 <code class=" language-php">sendOutputTo</code> 你可以发送输出到单个文件上以便后续检查:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$schedule</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">command<span class="token punctuation">(</span></span><span class="token string">'emails:send'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">daily<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">sendOutputTo<span class="token punctuation">(</span></span><span class="token variable">$filePath</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>如果想将输出附加到指定的文件上,则可以使用 <code class=" language-php">appendOutputTo</code> 方法:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$schedule</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">command<span class="token punctuation">(</span></span><span class="token string">'emails:send'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">daily<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">appendOutputTo<span class="token punctuation">(</span></span><span class="token variable">$filePath</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>通过 <code class=" language-php">emailOutputTo</code> 方法,你可以发送输出到你所指定的电子邮件上。注意,你必须先通过 <code class=" language-php">sendOutputTo</code> 方法将其输出到一个文件。同时,在邮件发出之前,你需要先设置 Laravel 的 <a href="/docs/5.4/mail">电子邮件服务</a>:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$schedule</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">command<span class="token punctuation">(</span></span><span class="token string">'foo'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">daily<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">sendOutputTo<span class="token punctuation">(</span></span><span class="token variable">$filePath</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">emailOutputTo<span class="token punctuation">(</span></span><span class="token string">'foo@example.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><blockquote class="has-icon note"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="90px" height="90px" viewBox="0 0 90 90" enable-background="new 0 0 90 90" xml:space="preserve"><path fill="#FFFFFF" d="M45 0C20.1 0 0 20.1 0 45s20.1 45 45 45 45-20.1 45-45S69.9 0 45 0zM45 74.5c-3.6 0-6.5-2.9-6.5-6.5s2.9-6.5 6.5-6.5 6.5 2.9 6.5 6.5S48.6 74.5 45 74.5zM52.1 23.9l-2.5 29.6c0 2.5-2.1 4.6-4.6 4.6 -2.5 0-4.6-2.1-4.6-4.6l-2.5-29.6c-0.1-0.4-0.1-0.7-0.1-1.1 0-4 3.2-7.2 7.2-7.2 4 0 7.2 3.2 7.2 7.2C52.2 23.1 52.2 23.5 52.1 23.9z"></path></svg></span></div> <code class=" language-php">emailOutputTo</code>,<code class=" language-php">sendOutputTo</code> 和 <code class=" language-php">appendOutputTo</code> 方法只适用于 <code class=" language-php">command</code>方法,不支持 <code class=" language-php">call</code> 方法。</p></blockquote><p><a name="task-hooks"></a></p><h2><a href="#task-hooks">任务钩子</a></h2><p>通过 <code class=" language-php">before</code> 与 <code class=" language-php">after</code> 方法,你能让特定的代码在任务完成之前及之后运行:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$schedule</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">command<span class="token punctuation">(</span></span><span class="token string">'emails:send'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">daily<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">before<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> // Task is about to start...
</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">after<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> // Task is complete...
</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>Ping 网址</h4><p>通过 <code class=" language-php">pingBefore</code> 与 <code class=" language-php">thenPing</code> 方法,调度器能自动的在一个任务完成之前或之后 ping 一个指定的网址。该方法在你计划的任务进行或完成时,可用来有效的通知一个外部服务,例如 <a href="https://envoyer.io">Laravel Envoyer</a>:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$schedule</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">command<span class="token punctuation">(</span></span><span class="token string">'emails:send'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">daily<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">pingBefore<span class="token punctuation">(</span></span><span class="token variable">$url</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">thenPing<span class="token punctuation">(</span></span><span class="token variable">$url</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>使用 <code class=" language-php"><span class="token function">pingBefore<span class="token punctuation">(</span></span><span class="token variable">$url</span><span class="token punctuation">)</span></code> 或 <code class=" language-php"><span class="token function">thenPing<span class="token punctuation">(</span></span><span class="token variable">$url</span><span class="token punctuation">)</span></code> 功能需要 Guzzle HTTP 函数库的支持。可在 <code class=" language-php">composer<span class="token punctuation">.</span>json</code> 文件中加入以下代码来安装 Guzzle:</p><pre class=" language-php"><code class=" language-php">composer <span class="token keyword">require</span> guzzlehttp<span class="token operator">/</span>guzzle</code></pre></article>
- 入门指南
- 安装
- 配置信息
- 文件夹结构
- 请求周期
- 开发环境部署
- Valet
- Homestead
- 核心概念
- 服务提供者
- Facades
- Contracts
- 服务容器
- HTTP 层
- 路由
- 中间件
- CSRF 保护
- 控制器
- 请求
- 响应
- 视图
- Session
- 表单验证
- 前端
- Blade 模板
- 本地化
- 前端指南
- 编辑资源 Mix
- 安全
- API 认证
- 用户认证
- 用户授权
- 加密解密
- 哈希
- 重置密码
- 数据库
- 快速入门
- 查询构造器
- 分页
- 数据库迁移
- Redis
- 数据填充
- Eloquent ORM
- Eloquent ORM快速入门
- 模型关联
- Eloquent 集合
- 修改器
- 序列化
- 综合话题
- Artisan 命令行
- 广播系统
- 缓存系统
- 集合
- 错误与日志
- 事件系统
- 文件存储
- 辅助函数
- 邮件发送
- 消息通知
- 扩展包开发
- 队列
- 任务调度