getTask(); if(!$this->cp_id){ return ; } $this->start_time = date('Y-m-d H:i:s'); $this->runSpider(); if($this->commad_status){ $this->record->spider_status = 3; }else{ $this->record->spider_status = 2; } $spider_result = $this->getResult(); $this->record->spider_result = $spider_result->pluck('id')->implode(','); $this->record->save(); $this->notice($spider_result); } /** * 获取同步任务,一次只获取一个 * @return void */ private function getTask(){ $record = $this->CpCollectionModel->where('created_at','>',date('Y-m-d H:i:s',time()-86400))->where('spider_status',0)->first(); if($record){ $record->spider_status = 1; $record->save(); $this->cp_name = $record->cp_name; $this->cp_id = $record->cp_id; $this->spider_name = str_replace('zy_','',$this->cp_name); $this->record = $record; } } /** * 执行脚本,并获取状态 * @return void */ private function runSpider(){ $command = ' bash /project/www/zhiyu_content_spider/content_spider/bash/command.sh '.$this->spider_name; Log::info('cpCollection',['command'=>$command]); $result = Process::forever()->run($command); if($result->successful()){ $this->commad_status = 0; }else{ $this->commad_status = 1; } } /** * 获取同步结果 */ private function getResult(){ return $this->book->where('cp_id',$this->cp_id) ->where('created_at','>=',$this->start_time) ->where('created_at','<=',date('Y-m-d H:i:s')) ->select('id','name')->get(); } /** * 通知同步结果 * @param [type] $collection_result * @return void */ private function notice($collection_result){ $role = Roles::where('identify','contentadmin')->select('id','role_name')->first(); $notice_obj = []; if($role){ $notice_obj[] = ['id'=>$role->id,'name'=>$role->role_name]; } $admin_role = Roles::where('identify','admin')->select('id','role_name')->first(); if($admin_role){ $notice_obj[] = ['id'=>$admin_role->id,'name'=>$admin_role->role_name]; } if(!$notice_obj){ $notice_obj = [['id'=>6,'name'=>'内容管理员']]; } $end_time = date('Y-m-d H:i:s'); $count = $collection_result->count(); $books = $collection_result->pluck('name')->implode(','); $content = $this->start_time.' 同步'.$this->cp_name."任务执行完成\r\n此次执行任务在 {$end_time}执行完成,增量同步{$count}本书如下:\r\n".$books; NoticesService::addNotice([ 'title' => $this->cp_name.'书籍同步完成', 'notice_type_id' => 2, 'type' => 3, 'notice_obj'=>$notice_obj, 'is_popup' => 1, 'content' => $content ]); } }