|
本帖最后由 追影 于 2018-7-13 13:21 编辑
(1/)etc/crontab.xml
注:时间的五个※ 分别是 分 时 日 月 周
- <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
- <group id="index">
- <job name="silks_scheduled_importexport" instance="Silk\Catalog\Model\ScheduledImportExport" method="execute">
- <schedule>*/5 * * * *</schedule>
- </job>
- </group>
- </config>
复制代码
(2/app/code/Silk/Catalog/Model/ScheduledImportExport.php
- <?php
- /**
- * Created by PhpStorm.
- * User: toby
- * Date: 18-7-11
- * Time: 上午11:50
- */
- namespace Silk\Catalog\Model;
- use Magento\Framework\DataObject;
- class ScheduledImportExport {
- protected $_objectManager;
- protected $_dateTime;
- protected $_import_export_collection;
- protected $_localtime;
- public function __construct(
- \Magento\Framework\Stdlib\DateTime $dateTime,
- \Magento\ScheduledImportExport\Model\ResourceModel\Scheduled\Operation\Collection $collection,
- \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone,
- \Magento\Store\Model\StoreManagerInterface $storeManager,
- array $data = []
- )
- {
- $this->_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
- $this->_import_export_collection = $collection;
- $this->_dateTime = $dateTime;
- $this->_localtime = $timezone;
- }
- public function execute(){
- try{
- $list = $this->_import_export_collection->getItems();
- foreach($list as $item){
- $operationId = (int)$item->getId();
- $freq = $item->getFreq();
- $cur_time = $this->_localtime->date()->format('Y-m-d').' '.$item->getStartTime();
- $now_time = $this->_localtime->date()->format('Y-m-d H:i:s');
- $compare_result = $this->compare_time($now_time,$cur_time);
- $last_time = (string)$item->getLastRunDate();
- $last_unix = (int)strtotime($last_time);
- //$this->log($now_time.' '.$cur_time . $compare_result);
- switch($freq){
- case 'D':
- if(( !$last_unix && $compare_result) || ($last_unix && time() - $last_unix >= 24*3600)){
- // $this->log('执行D:'. $operationId." $now_time $cur_time");
- $this->scheduled_operation($operationId);
- }
- break;
- case 'M':
- if((!$last_unix && $compare_result) || ($last_unix && time() - $last_unix >= 7*24*3600)){
- // $this->log('执行M:'. $operationId." $now_time $cur_time");
- $this->scheduled_operation($operationId);
- }
- break;
- case 'Y':
- if((!$last_unix && $compare_result) || ($last_unix && time() - $last_unix >= 365*24*3600)){
- // $this->log('执行Y:'. $operationId." $now_time $cur_time");
- $this->scheduled_operation($operationId);
- }
- break;
- }
- }
- return true;
- }catch(\Exception $e){
- return false;
- }
- }
- private function scheduled_operation($operationId){
- $schedule = new DataObject();
- $schedule->setJobCode(
- \Magento\ScheduledImportExport\Model\Scheduled\Operation::CRON_JOB_NAME_PREFIX . $operationId
- );
- /** @var \Magento\ScheduledImportExport\Model\Observer $result */
- $result = $this->_objectManager->get(\Magento\ScheduledImportExport\Model\Observer::class)
- ->processScheduledOperation($schedule, true);
- return $result;
- }
- function compare_time($a,$b){
- if(date('H',strtotime($a)) == date('H',strtotime($b)) && date('i',strtotime($a)) >= date('i',strtotime($b))){
- return true;
- }
- return false;
- }
- protected function log($msg) {
- if (is_array($msg) || is_object($msg)) {
- $msg = print_r($msg, true);
- }
- $_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
- $msg = '['.date('Y-m-d H:i:s').']: '.$msg."\n";
- $directory = $_objectManager->get('\Magento\Framework\Filesystem\DirectoryList');
- file_put_contents($directory->getPath('log').'/silk_import.log', $msg, FILE_APPEND);
- }
- }
复制代码
|
|