PHP code example of hbalkhi / yii2-scheduler

1. Go to this page and download the library: Download hbalkhi/yii2-scheduler 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/ */

    

hbalkhi / yii2-scheduler example snippets


'bootstrap' => ['log', 'scheduler'],
'modules' => [
    'scheduler' => ['class' => 'hbalkhi\scheduler\Module'],
],
'components' => [
    'errorHandler' => [
        'class' => 'hbalkhi\scheduler\ErrorHandler'
    ],
    'log' => [
        'traceLevel' => YII_DEBUG ? 3 : 0,
        'targets' => [
            [
                'class' => 'yii\log\EmailTarget',
                'mailer' =>'mailer',
                'levels' => ['error', 'warning'],
                'message' => [
                    'to' => ['[email protected]'],
                    'from' => [$params['adminEmail']],
                    'subject' => 'Scheduler Error - ####SERVERNAME####'
                ],
                'except' => [
                ],
            ],
        ],
    ],
]

\yii\base\Event::on(
    \hbalkhi\scheduler\console\SchedulerController::className(),
    \hbalkhi\scheduler\events\SchedulerEvent::EVENT_AFTER_RUN,
    function ($event) {
        if (!$event->success) {
            foreach($event->exceptions as $exception) {
                throw $exception;
            }
        }
    }
);

'bootstrap' => ['log', 'scheduler'],
'modules' => [
    'scheduler' => ['class' => 'hbalkhi\scheduler\Module'],
],



namespace app\modules\admin\controllers;

use yii\web\Controller;

/**
 * Class SchedulerController
 * @package app\modules\admin\controllers
 */
class SchedulerController extends Controller
{
    public function actions()
    {
        return [
            'index' => [
                'class' => 'hbalkhi\scheduler\actions\IndexAction',
                'view' => '@scheduler/views/index',
            ],
            'update' => [
                'class' => 'hbalkhi\scheduler\actions\UpdateAction',
                'view' => '@scheduler/views/update',
            ],
            'view-log' => [
                'class' => 'hbalkhi\scheduler\actions\ViewLogAction',
                'view' => '@scheduler/views/view-log',
            ],
        ];
    }
}


namespace app\tasks;

/**
 * Class AlphabetTask
 * @package app\tasks
 */
class AlphabetTask extends \hbalkhi\scheduler\Task
{
    public $description = 'Prints the alphabet';
    public $schedule = '0 * * * *';
    
    public function run()
    {
        foreach (range('A', 'Z') as $letter) {
            echo $letter;
        }
    }
}

Event::on(AlphabetTask::className(), AlphabetTask::EVENT_BEFORE_RUN, function ($event) {
    Yii::trace($event->task->className . ' is about to run');
});
Event::on(AlphabetTask::className(), AlphabetTask::EVENT_AFTER_RUN, function ($event) {
    Yii::trace($event->task->className . ' just ran '.($event->success ? 'successfully' : 'and failed'));
});

$application->on(\hbalkhi\scheduler\events\SchedulerEvent::EVENT_AFTER_RUN, function ($event) {
    if (!$event->success) {
        foreach($event->exceptions as $exception) {
            throw $exception;
        }
    }
});
bash
php yii migrate up --migrationPath=vendor/hbalkhi/yii2-scheduler/src/migrations