id = $id; $this->fileResultType = $fileResultType; $this->path = $path; $this->type = $type; $this->fromRequest = $fromRequest; $this->storage = Storage::disk('local'); } /** * Execute the job. * * @return void */ public function handle() { try { $this->execute(); } catch (\Exception $exception) { \Illuminate\Support\Facades\Log::info('=============== IngestDocuments@handle'); \Illuminate\Support\Facades\Log::info($exception->getMessage()); \Illuminate\Support\Facades\Log::info($exception->getTraceAsString()); \Illuminate\Support\Facades\Log::info('=============== '); $this->failed(); return; } $directoryPath = pathinfo($this->path, PATHINFO_DIRNAME); if ($this->fromRequest) { SendToCore::dispatch($this->id, $this->fileResultType, $directoryPath); return; } $this->storage->deleteDirectory($directoryPath); $this->updateAnalyzer(); } protected function execute() { if ($this->fileResultType === 'md') { $this->convertToMD(); return; } $this->convertToJsonData(); } /** * Convert document to plain MD file which is easy to work with. * * @throws \Exception */ protected function convertToMD() { $convertor = new Convertor($this->path, $this->type); $convertor->execute(); } /** * Convert document to JSON data file. * * @throws \Exception */ protected function convertToJsonData() { $convertor = new DataJsonConvertor($this->path, $this->type); $convertor->execute(); } public function failed() { if ( ! $this->storage) { $this->storage = Storage::disk('local'); } Log::error('Ingest documents failed. ' . $this->path); $directoryPath = pathinfo($this->path, PATHINFO_DIRNAME); if ($this->fromRequest) { SendToCore::dispatch($this->id, $this->fileResultType, $directoryPath, true); return; } $this->storage->deleteDirectory($directoryPath); $this->updateAnalyzer(true); } protected function updateAnalyzer($failed = false) { $redis = Redis::connection(); if ($failed) { $redis->set('analyze_performance_error', '1'); } $remainingFiles = $redis->get('analyze_performance_remaining_files'); $remainingFiles -= 1; if ($remainingFiles === 0) { $startedAt = $redis->get('analyze_performance_time'); $endedAt = Carbon::now()->format('U'); $directoryPath = $redis->get('analyze_performance_path'); $data = 'Time elapsed in seconds: ' . ($endedAt - $startedAt) . "\n"; if ($failed) { $data = $data . 'Something went wrong while processing the files.'; } file_put_contents($directoryPath . '/ingest_analyze_performance.txt', $data); return; } $redis->set('analyze_performance_remaining_files', $remainingFiles); } }