【magento2配置专题9】配置自定义定时任务(cron )和计划组(cron group)

1: 获取示例模块

  1. 登录到magento服务器,切换到 Magento 2文件系统所有者.
  2. 克隆 magento2 样本库.

    示例

    cd ~
    git clone [email protected]:magento/magento2-samples.git
    
  3. 创建一个目录来拷贝示例代码:

    mkdir -p /var/www/html/magento2/app/code/Magento/SampleMinimal
    
  4. 复制示例模块代码:

    cp -r ~/magento2-samples/sample-module-minimal/* /var/www/html/magento2/app/code/Magento/SampleMinimal
    
  5. 验证文件是否正确复制:

    ls -al /var/www/html/magento2/app/code/Magento/SampleMinimal
    

    You should see the following result:

    drwxrwsr-x.   4 magento_user apache  4096 Oct 30 13:19 .
    drwxrwsr-x. 121 magento_user apache  4096 Oct 30 13:19 ..
    -rw-rw-r--.   1 magento_user apache   372 Oct 30 13:19 composer.json
    drwxrwsr-x.   2 magento_user apache  4096 Oct 30 13:19 etc
    -rw-rw-r--.   1 magento_user apache 10376 Oct 30 13:19 LICENSE_AFL.txt
    -rw-rw-r--.   1 magento_user apache 10364 Oct 30 13:19 LICENSE.txt
    -rw-rw-r--.   1 magento_user apache  1157 Oct 30 13:19 README.md
    -rw-rw-r--.   1 magento_user apache   270 Oct 30 13:19 registration.php
    drwxrwsr-x.   3 magento_user apache  4096 Oct 30 13:19 Test
    
  6. 更新Magento 2 数据库和schema:

    php /var/www/html/magento2/bin/magento setup:upgrade
    

2: 验证样本模块

  1. 用管理员身份登录magento 2后台
  2. 点击 Stores > Configuration > ADVANCED > Advanced.
  3. 如下图:

3: 创建一个类来运行cron

  1. 为类创建目录并更改目录:

    mkdir /var/www/html/magento2/app/code/Magento/SampleMinimal/Cron && cd /var/www/html/magento2/app/code/Magento/SampleMinimal/Cron
    
  2. 在目录中创建了一个名为Test.php的文件:

logger = $logger;
    }

/**
   * Write to system.log
   *
   * @return void
   */

    public function execute() {
        $this->logger->info('Cron Works');
    }

}

4: 创建 crontab.xml

在 /var/www/html/magento2/app/code/Magento/SampleMinimal/etc 目录创建 crontab.xml :



    
        
            * * * * *
        
    

5: 验证cron job

  1. 运行 Magento cron jobs:

    php /var/www/html/magento2/bin/magento cron:run
    
  2. 输入 magento cron:run 命令

  3. 运行查询sql SELECT * from cron_schedule WHERE job_code like '%custom%' 如下:

    1. 输入 mysql -u magento -p
    2. 输入 use magento;
    3. 输入 SELECT * from cron_schedule WHERE job_code like '%custom%';

    查询结果:

    +-------------+----------------+---------+----------+---------------------+---------------------+---------------------+---------------------+
    | schedule_id | job_code       | status  | messages | created_at          | scheduled_at        | executed_at         | finished_at         |
    +-------------+----------------+---------+----------+---------------------+---------------------+---------------------+---------------------+
    |        3670 | custom_cronjob | success | NULL     | 2016-11-02 09:38:03 | 2016-11-02 09:38:00 | 2016-11-02 09:39:03 | 2016-11-02 09:39:03 |
    |        3715 | custom_cronjob | success | NULL     | 2016-11-02 09:53:03 | 2016-11-02 09:53:00 | 2016-11-02 09:54:04 | 2016-11-02 09:54:04 |
    |        3758 | custom_cronjob | success | NULL     | 2016-11-02 10:09:03 | 2016-11-02 10:09:00 | 2016-11-02 10:10:03 | 2016-11-02 10:10:03 |
    |        3797 | custom_cronjob | success | NULL     | 2016-11-02 10:24:03 | 2016-11-02 10:24:00 | 2016-11-02 10:25:03 | 2016-11-02 10:25:03 |
    +-------------+----------------+---------+----------+---------------------+---------------------+---------------------+---------------------+
    

6: 设置自定义 cron group

  1. 打开 crontab.xml 在文本编辑器.
  2. 更改  为 
  3. 保存.
  4. 创建 /var/www/html/magento2/app/code/Magento/SampleMinimal/etc/cron_groups.xml 具有以下内容:


    
        1
        4
        2
        10
        60
        600
    

7: 验证 cron group

  1. 运行cron job 在Magento自定义组:

    php /var/www/html/magento2/bin/magento cron:run --group="custom_crongroup"
    

    至少运行两次命令。

  2. 清除magento 2缓存:

    php /var/www/html/magento2/bin/magento cache:clean
    
  3. 用管理员身份登录到后台.
  4. 点击 Stores > Configuration > Advanced > System.
  5. 在右窗格中展开 Cron.

    如下图:

你可能感兴趣的:(magento2)