hossein142001/yii2-cronjob 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

hossein142001/yii2-cronjob

Composer 安装命令:

composer require hossein142001/yii2-cronjob

包简介

Yii2 extension to help in the creation of automated console scripts

README 文档

README

Latest Stable Version Total Downloads Latest Unstable Version License

Yii2 extension to help in the creation of automated console scripts. It helps to manage the execution of console scripts, for example avoiding the execution if the previous cron is already running. It generates a history of cron executed, with the time spent and helps to batch processing the script.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist fedemotta/yii2-cronjob "*"

or add

"fedemotta/yii2-cronjob": "*"

to the require section of your composer.json file.

This extension needs a database to manage the cron job execution. Run the following migration to create the table structure:

yii migrate --migrationPath=@fedemotta/cronjob/migrations

Usage

Once the extension is installed, you can use it as a helper in your console controller.

See the following example:

<?php
namespace somenamespace\controllers;

use fedemotta\cronjob\models\CronJob;
use somenamespace\SomeModel;
use yii\console\Controller;

/**
 * SomeContrController controller
 */
class SomeContrController extends Controller {
 
    /**
     * Run SomeModel::some_method for a period of time
     * @param string $from
     * @param string $to
     * @return int exit code
     */
    public function actionInit($from, $to){
        $dates  = CronJob::getDateRange($from, $to);
        $command = CronJob::run($this->id, $this->action->id, 0, CronJob::countDateRange($dates));
        if ($command === false){
            return Controller::EXIT_CODE_ERROR;
        }else{
            foreach ($dates as $date) {
                //this is the function to execute for each day
                SomeModel::some_method((string) $date);
            }
            $command->finish();
            return Controller::EXIT_CODE_NORMAL;
        }
    }
    /**
     * Run SomeModel::some_method for today only as the default action
     * @return int exit code
     */
    public function actionIndex(){
        return $this->actionInit(date("Y-m-d"), date("Y-m-d"));
    }
    /**
     * Run SomeModel::some_method for yesterday
     * @return int exit code
     */
    public function actionYesterday(){
        return $this->actionInit(date("Y-m-d", strtotime("-1 days")), date("Y-m-d", strtotime("-1 days")));
    }
}

Run the SomeModel::some_method for today:

./yii some-contr

For yesterday:

./yii some-contr/yesterday

For some custom dates like this:

./yii some-contr/init 2010-10-10 2012-10-10

And you can add a cron job to run every 10 minutes with some controller action like this:

*/10 * * * * /path/to/yii/application/yii some-contr/yesterday >> /var/log/console-app.log 2>&1

统计信息

  • 总下载量: 227
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 9
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-05-18

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固