大米CMS官网论坛,大米站长联盟,大米站长之家,大米开发者社区

 找回密码
 注册大米会员

QQ登录

只需一步,快速开始

查看: 7843|回复: 0

Magento2自己动手写scheduled import export 定时任务

[复制链接]

496

主题

773

帖子

7627

积分

超级版主

Rank: 8Rank: 8

积分
7627

商城钻石VIP授权用户

发表于 2018-7-12 09:39:09 | 显示全部楼层 |阅读模式
本帖最后由 追影 于 2018-7-13 13:21 编辑

(1/)etc/crontab.xml
注:时间的五个※ 分别是 分  时  日  月  周 
  1. <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
  2.     <group id="index">
  3.         <job name="silks_scheduled_importexport" instance="Silk\Catalog\Model\ScheduledImportExport" method="execute">
  4.             <schedule>*/5 * * * *</schedule>
  5.         </job>
  6.     </group>
  7. </config>
复制代码

(2/app/code/Silk/Catalog/Model/ScheduledImportExport.php
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: toby
  5. * Date: 18-7-11
  6. * Time: 上午11:50
  7. */
  8. namespace Silk\Catalog\Model;

  9. use Magento\Framework\DataObject;

  10. class ScheduledImportExport {

  11.     protected $_objectManager;
  12.     protected $_dateTime;
  13.     protected $_import_export_collection;
  14.     protected $_localtime;

  15.     public function __construct(
  16.         \Magento\Framework\Stdlib\DateTime $dateTime,
  17.         \Magento\ScheduledImportExport\Model\ResourceModel\Scheduled\Operation\Collection $collection,
  18.         \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone,
  19.         \Magento\Store\Model\StoreManagerInterface $storeManager,
  20.         array $data = []
  21.     )
  22.     {
  23.         $this->_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  24.         $this->_import_export_collection = $collection;
  25.         $this->_dateTime = $dateTime;
  26.         $this->_localtime = $timezone;

  27.     }

  28.     public function execute(){
  29.         try{
  30.             $list = $this->_import_export_collection->getItems();
  31.             foreach($list as $item){
  32.                 $operationId = (int)$item->getId();
  33.                 $freq = $item->getFreq();
  34.                 $cur_time = $this->_localtime->date()->format('Y-m-d').' '.$item->getStartTime();
  35.                 $now_time = $this->_localtime->date()->format('Y-m-d H:i:s');
  36.                 $compare_result = $this->compare_time($now_time,$cur_time);
  37.                 $last_time = (string)$item->getLastRunDate();
  38.                 $last_unix = (int)strtotime($last_time);
  39.                 //$this->log($now_time.' '.$cur_time . $compare_result);
  40.                 switch($freq){
  41.                     case 'D':
  42.                         if(( !$last_unix && $compare_result) || ($last_unix && time() - $last_unix >= 24*3600)){
  43.                            // $this->log('执行D:'. $operationId." $now_time $cur_time");
  44.                             $this->scheduled_operation($operationId);
  45.                         }
  46.                         break;
  47.                     case 'M':
  48.                         if((!$last_unix && $compare_result) || ($last_unix && time() - $last_unix >= 7*24*3600)){
  49.                            // $this->log('执行M:'. $operationId." $now_time $cur_time");
  50.                             $this->scheduled_operation($operationId);
  51.                         }
  52.                         break;
  53.                     case 'Y':
  54.                         if((!$last_unix && $compare_result) || ($last_unix && time() - $last_unix >= 365*24*3600)){
  55.                            // $this->log('执行Y:'. $operationId." $now_time $cur_time");
  56.                             $this->scheduled_operation($operationId);
  57.                         }
  58.                         break;
  59.                 }

  60.             }
  61.             return true;
  62.         }catch(\Exception $e){
  63.             return false;
  64.         }

  65.     }

  66.     private function scheduled_operation($operationId){
  67.         $schedule = new DataObject();
  68.         $schedule->setJobCode(
  69.             \Magento\ScheduledImportExport\Model\Scheduled\Operation::CRON_JOB_NAME_PREFIX . $operationId
  70.         );
  71.         /** @var \Magento\ScheduledImportExport\Model\Observer $result */
  72.         $result = $this->_objectManager->get(\Magento\ScheduledImportExport\Model\Observer::class)
  73.             ->processScheduledOperation($schedule, true);
  74.         return $result;
  75.     }

  76.     function compare_time($a,$b){
  77.         if(date('H',strtotime($a)) == date('H',strtotime($b)) && date('i',strtotime($a)) >= date('i',strtotime($b))){
  78.             return true;
  79.         }
  80.         return false;
  81.     }

  82.     protected function log($msg) {
  83.         if (is_array($msg) || is_object($msg)) {
  84.             $msg = print_r($msg, true);
  85.         }
  86.         $_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  87.         $msg = '['.date('Y-m-d H:i:s').']: '.$msg."\n";
  88.         $directory = $_objectManager->get('\Magento\Framework\Filesystem\DirectoryList');
  89.         file_put_contents($directory->getPath('log').'/silk_import.log', $msg, FILE_APPEND);
  90.     }

  91. }
复制代码



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册大米会员

本版积分规则

QQ|小黑屋|大米CMS社区 ( 蜀ICP备11002200号-2广告联系:广告联系 

Powered by 大米CMS

© 2010-2020 大米CMS Inc.

快速回复 返回顶部 返回列表