PHP code example of thamtech / yii2-scheduler

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

    

thamtech / yii2-scheduler example snippets



[
    'bootstrap' => ['log', 'scheduler'],
    'modules' => [
        'scheduler' => [
            'class' => 'thamtech\scheduler\Module',
            
            // optional: define a mutex component to acquire a lock
            // while executing tasks so only one execution of schedule tasks
            // can be running at a time.
            'mutex' => [
                'class' => 'yii\mutex\MysqlMutex',
            ], 
            
            // OR optionally reference an existing application mutex component,
            // for example, one named "mutex":
            // 'mutex' => 'mutex',
        ],
    ],
    'components' => [
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\EmailTarget',
                    'mailer' =>'mailer',
                    'levels' => ['error', 'warning'],
                    'message' => [
                        'to' => ['[email protected]'],
                        'from' => ['[email protected]'],
                        'subject' => 'Scheduler Error - ####SERVERNAME####'
                    ],
                    'except' => [
                    ],
                ],
            ],
        ],
    ],
];

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


namespace app\modules\admin\controllers;

use yii\web\Controller;

/**
 * SchedulerController has a set of actions for viewing scheduled tasks and
 * their logs.
 */
class SchedulerController extends Controller
{
    public function actions()
    {
        return [
            'index' => [
                'class' => 'thamtech\scheduler\actions\IndexAction',
                'view' => '@scheduler/views/index',
            ],
            'view' => [
                'class' => 'thamtech\scheduler\actions\ViewAction',
                'view' => '@scheduler/views/view',
            ],
            'view-log' => [
                'class' => 'thamtech\scheduler\actions\ViewLogAction',
                'view' => '@scheduler/views/view-log',
            ],
        ];
    }
}


namespace app\tasks;

/**
 * Task to print a concatenation of the specified strings.
 */
class ConcatStringsTask extends \thamtech\scheduler\Task
{
    /**
     * @var string task description
     */
    public $description = 'Prints a concatenation of the specified strings';
    
    /**
     * @var string[] the strings to be concatenated
     */
    public $strings = [];
    
    /**
     * @inheritdoc
     */
    public function run()
    {
        echo join('', $this->strings);
    }
}


'modules' => [
    'scheduler' => [
        'class' => 'thamtech\scheduler\Module',
        'tasks' => [
            'hello-world' => [
                'class' => 'app\tasks\ConcatStringsTask',
                'displayName' => 'Hello World Task',
                'schedule' => '0 * * * *',
                'strings' => ['Hello', ' ', 'World'],
            ],
        ],
    ],
],


'modules' => [
    'scheduler' => [
        'class' => 'thamtech\scheduler\Module',
        'tasks' => [
            'hello-world' => [
                'class' => 'app\tasks\ConcatStringsTask',
                'displayName' => 'Hello World Task',
                'schedule' => '0 * * * *',
                'strings' => ['Hello', ' ', 'World'],
            ],
            'foo-bar' => [
                'class' => 'app\tasks\ConcatStringsTask',
                'displayName' => 'Foo Bar Task',
                'schedule' => '30 * * * *',
                'strings' => ['Foo', ' ', 'Bar'],
            ],
        ],
    ],
],


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(\thamtech\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/thamtech/yii2-scheduler/src/migrations