PHP code example of gutink / schedule

1. Go to this page and download the library: Download gutink/schedule library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gutink / schedule example snippets


protected function schedule(Schedule $schedule): void
{
    $schedule->call(function () {
       Db::table('recent_users')->delete();
    })->daily();
}

$schedule->call(new DeleteRecentUsers)->daily();

php think schedule:list


use app\command\SendEmails;

$schedule->command('emails:send Taylor --force')->daily();

$schedule->command(SendEmails::class, ['Taylor', '--force'])->daily();


use app\jhobs\Heartbeat;

$schedule->job(new Heartbeat)->everyFiveMinutes();

use app\jobs\Heartbeat;
// 分发任务到「heartbeats」队列及「redis」连接...
$schedule->job(new Heartbeat, 'heartbeats', 'redis')->everyFiveMinutes();

$schedule->exec('node /home/forge/script.js')->daily();

// 在每周一 13:00 执行...
$schedule->call(function () {
    // ...
})->weekly()->mondays()->at('13:00');

// 在每个工作日 8:00 到 17:00 之间的每小时周期执行...
$schedule->command('foo')
          ->weekdays()
          ->hourly()
          ->timezone('America/Chicago')
          ->between('8:00', '17:00');

$schedule->command('email:send')
                ->hourly()
                ->days([0, 3]);

use schedule\scheduling\Schedule;

$schedule->command('email:send')
                ->hourly()
                ->days([Schedule::SUNDAY, Schedule::WEDNESDAY]);

$schedule->command('email:send')
                    ->hourly()
                    ->between('7:00', '22:00');

$schedule->command('email:send')
                    ->hourly()
                    ->unlessBetween('23:00', '4:00');

$schedule->command('email:send')->daily()->when(function () {
    return true;
});

$schedule->command('email:send')->daily()->skip(function () {
    return true;
});

$schedule->command('report:generate')
         ->timezone('America/New_York')
         ->at('2:00')

use DateTimeZone;

/**
 * 获取计划事件默认使用的时区
 */
protected function scheduleTimezone(): DateTimeZone|string|null
{
    return 'America/Chicago';
}

$schedule->command('email:send')->withoutOverlapping();

$schedule->command('email:send')->withoutOverlapping(10);

$schedule->command('report:generate')
                ->fridays()
                ->at('17:00')
                ->onOneServer();

$schedule->job(new CheckUptime('https://think.com'))
            ->name('check_uptime:think.com')
            ->everyFiveMinutes()
            ->onOneServer();

$schedule->job(new CheckUptime('https://vapor.think.com'))
            ->name('check_uptime:vapor.think.com')
            ->everyFiveMinutes()
            ->onOneServer();

$schedule->call(fn () => User::resetApiRequestCount())
    ->name('reset-api-request-count')
    ->daily()
    ->onOneServer();

$schedule->command('analytics:report')
         ->daily()
         ->runInBackground();

* * * * * cd /path-to-your-project && php think schedule:run >> /dev/null 2>&1

php think schedule:work

$schedule->command('email:send')
         ->daily()
         ->sendOutputTo($filePath);

$schedule->command('emails:send')
         ->daily()
         ->appendOutputTo($filePath);

$schedule->command('report:generate')
         ->daily()
         ->sendOutputTo($filePath)
         ->emailOutputTo('[email protected]');

$schedule->command('report:generate')
         ->daily()
         ->emailOutputOnFailure('[email protected]');

$schedule->command('email:send')
         ->daily()
         ->before(function () {
             // 任务即将执行。。。
         })
         ->after(function () {
             // 任务已经执行。。。
         });

$schedule->command('email:send')
         ->daily()
         ->onSuccess(function () {
             // 任务执行成功。。。
         })
         ->onFailure(function () {
             // 任务执行失败。。。
         });

$schedule->command('email:send')
         ->daily()
         ->pingBefore($url)
         ->thenPing($url);

$schedule->command('email:send')
         ->daily()
         ->pingBeforeIf($condition, $url)
         ->thenPingIf($condition, $url);

$schedule->command('email:send')
         ->daily()
         ->pingOnSuccess($successUrl)
         ->pingOnFailure($failureUrl);


'listen' => [
        'schedule\events\ScheduledTaskStarting' => [
            \app\listener\LogScheduledTaskStarting::class
        ],
        'schedule\events\ScheduledTaskFinished' => [
            \app\listener\LogScheduledTaskFinished::class,
        ],
        'schedule\events\ScheduledTaskSkipped' => [
            \app\listener\LogScheduledTaskSkipped::class,
        ],
        'schedule\events\ScheduledTaskFailed' => [
            \app\listener\LogScheduledTaskFailed::class,
        ],
];

php think schedule:init

namespace app;

use schedule\scheduling\ScheduleConsole;
use schedule\scheduling\Schedule;

class ConsoleScheduling extends ScheduleConsole
{
    /**
     * 定义任务计划
     * @param Schedule $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command("test")->onOneServer()->everyMinute();
        $schedule->exec('echo 555')->everyMinute();
        $schedule->call(function () {
            file_put_contents(runtime_path()."console-scheduling.log", time() . PHP_EOL, FILE_APPEND);
        })->everyMinute();
    }
}