Repo for the search and displace ingest module that takes odf, docx and pdf and transforms it into .md to be used with search and displace operations
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
4.2 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. <?php
  2. namespace App\Jobs;
  3. use App\Ingest\Convertor;
  4. use App\Ingest\DataJsonConvertor;
  5. use Illuminate\Bus\Queueable;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Support\Carbon;
  10. use Illuminate\Support\Facades\Log;
  11. use Illuminate\Support\Facades\Redis;
  12. use Illuminate\Support\Facades\Storage;
  13. class IngestDocuments implements ShouldQueue
  14. {
  15. use Dispatchable, InteractsWithQueue, Queueable;
  16. protected $id;
  17. protected $fileResultType;
  18. protected $path;
  19. protected $type;
  20. protected $fromRequest;
  21. /**
  22. * @var \Illuminate\Contracts\Filesystem\Filesystem
  23. */
  24. private $storage;
  25. /**
  26. * Create a new job instance.
  27. *
  28. * @param $id
  29. * @param $fileResultType
  30. * @param string $path
  31. * @param $type
  32. * @param $fromRequest
  33. */
  34. public function __construct($id, $fileResultType, string $path, $type, $fromRequest)
  35. {
  36. $this->id = $id;
  37. $this->fileResultType = $fileResultType;
  38. $this->path = $path;
  39. $this->type = $type;
  40. $this->fromRequest = $fromRequest;
  41. $this->storage = Storage::disk('local');
  42. }
  43. /**
  44. * Execute the job.
  45. *
  46. * @return void
  47. */
  48. public function handle()
  49. {
  50. try {
  51. $this->execute();
  52. } catch (\Exception $exception) {
  53. \Illuminate\Support\Facades\Log::info('=============== IngestDocuments@handle');
  54. \Illuminate\Support\Facades\Log::info($exception->getMessage());
  55. \Illuminate\Support\Facades\Log::info($exception->getTraceAsString());
  56. \Illuminate\Support\Facades\Log::info('=============== ');
  57. $this->failed();
  58. return;
  59. }
  60. $directoryPath = pathinfo($this->path, PATHINFO_DIRNAME);
  61. if ($this->fromRequest) {
  62. SendToCore::dispatch($this->id, $this->fileResultType, $this->type, $directoryPath);
  63. return;
  64. }
  65. $this->storage->deleteDirectory($directoryPath);
  66. $this->updateAnalyzer();
  67. }
  68. protected function execute()
  69. {
  70. if ($this->fileResultType === 'md') {
  71. $this->convertToMD();
  72. return;
  73. }
  74. $this->convertToJsonData();
  75. }
  76. /**
  77. * Convert document to plain MD file which is easy to work with.
  78. *
  79. * @throws \Exception
  80. */
  81. protected function convertToMD()
  82. {
  83. $convertor = new Convertor($this->path, $this->type);
  84. $convertor->execute();
  85. }
  86. /**
  87. * Convert document to JSON data file.
  88. *
  89. * @throws \Exception
  90. */
  91. protected function convertToJsonData()
  92. {
  93. $convertor = new DataJsonConvertor($this->path, $this->type);
  94. $convertor->execute();
  95. }
  96. public function failed()
  97. {
  98. if ( ! $this->storage) {
  99. $this->storage = Storage::disk('local');
  100. }
  101. Log::error('Ingest documents failed. ' . $this->path);
  102. $directoryPath = pathinfo($this->path, PATHINFO_DIRNAME);
  103. if ($this->fromRequest) {
  104. SendToCore::dispatch($this->id, $this->fileResultType, $this->type, $directoryPath, true);
  105. return;
  106. }
  107. $this->storage->deleteDirectory($directoryPath);
  108. $this->updateAnalyzer(true);
  109. }
  110. protected function updateAnalyzer($failed = false)
  111. {
  112. $redis = Redis::connection();
  113. if ($failed) {
  114. $redis->set('analyze_performance_error', '1');
  115. }
  116. $remainingFiles = $redis->get('analyze_performance_remaining_files');
  117. $remainingFiles -= 1;
  118. if ($remainingFiles === 0) {
  119. $startedAt = $redis->get('analyze_performance_time');
  120. $endedAt = Carbon::now()->format('U');
  121. $directoryPath = $redis->get('analyze_performance_path');
  122. $data = 'Time elapsed in seconds: ' . ($endedAt - $startedAt) . "\n";
  123. if ($failed) {
  124. $data = $data . 'Something went wrong while processing the files.';
  125. }
  126. file_put_contents($directoryPath . '/ingest_analyze_performance.txt', $data);
  127. return;
  128. }
  129. $redis->set('analyze_performance_remaining_files', $remainingFiles);
  130. }
  131. }