mostafaznv /
larupload
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Mostafaznv\Larupload\Jobs; |
||
| 4 | |||
| 5 | use Illuminate\Bus\Queueable; |
||
| 6 | use Illuminate\Contracts\Filesystem\FileNotFoundException; |
||
| 7 | use Exception; |
||
| 8 | use Illuminate\Database\Eloquent\Model; |
||
| 9 | use Illuminate\Database\Eloquent\Relations\Relation; |
||
| 10 | use Illuminate\Queue\SerializesModels; |
||
| 11 | use Illuminate\Queue\InteractsWithQueue; |
||
| 12 | use Illuminate\Contracts\Queue\ShouldQueue; |
||
| 13 | use Illuminate\Foundation\Bus\Dispatchable; |
||
| 14 | use Illuminate\Support\Facades\DB; |
||
| 15 | use Mostafaznv\Larupload\Events\LaruploadFFMpegQueueFinished; |
||
| 16 | use Mostafaznv\Larupload\Larupload; |
||
| 17 | |||
| 18 | |||
| 19 | class ProcessFFMpeg implements ShouldQueue |
||
| 20 | { |
||
| 21 | use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 22 | |||
| 23 | protected int $queueId; |
||
| 24 | protected int $id; |
||
| 25 | protected string $name; |
||
| 26 | protected string $model; |
||
| 27 | protected ?Larupload $standalone = null; |
||
| 28 | |||
| 29 | |||
| 30 | public function __construct(int $queueId, int $id, string $name, string $model, ?string $standalone = null) |
||
| 31 | { |
||
| 32 | $this->queueId = $queueId; |
||
| 33 | $this->id = $id; |
||
| 34 | $this->name = $name; |
||
| 35 | $this->model = $model; |
||
| 36 | |||
| 37 | if ($standalone) { |
||
| 38 | $this->standalone = unserialize(base64_decode($standalone)); |
||
| 39 | } |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @throws Exception |
||
| 44 | */ |
||
| 45 | public function handle() |
||
| 46 | { |
||
| 47 | $this->updateStatus(false, true); |
||
| 48 | |||
| 49 | // we need to handle ffmpeg queue after model saved event |
||
| 50 | sleep(1); |
||
| 51 | |||
| 52 | try { |
||
| 53 | if ($this->standalone) { |
||
| 54 | $this->standalone->handleFFMpegQueue( |
||
| 55 | isLastOne: $this->availableQueues() === 1, |
||
| 56 | standalone: true |
||
| 57 | ); |
||
| 58 | } |
||
| 59 | else { |
||
| 60 | /** @var Model $class */ |
||
| 61 | $class = class_exists($this->model) ? $this->model : Relation::getMorphedModel($this->model); |
||
| 62 | $modelNotSaved = true; |
||
| 63 | |||
| 64 | while ($modelNotSaved) { |
||
| 65 | $model = $class::query()->where('id', $this->id)->first(); |
||
| 66 | |||
| 67 | if ($model->{$this->name}->meta('name')) { |
||
| 68 | $modelNotSaved = false; |
||
| 69 | |||
| 70 | $model->{$this->name}->handleFFMpegQueue($this->availableQueues() === 1); |
||
| 71 | } |
||
| 72 | |||
| 73 | sleep(1); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | $this->updateStatus(true, false); |
||
| 78 | } |
||
| 79 | catch (FileNotFoundException | Exception $e) { |
||
| 80 | $this->updateStatus(false, false, $e->getMessage()); |
||
| 81 | |||
| 82 | throw new Exception($e->getMessage()); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Update LaruploadFFMpegQueue table |
||
| 88 | * |
||
| 89 | * @param bool $status |
||
| 90 | * @param bool $isStarted |
||
| 91 | * @param string|null $message |
||
| 92 | * @return int |
||
| 93 | */ |
||
| 94 | protected function updateStatus(bool $status, bool $isStarted, ?string $message = null): int |
||
| 95 | { |
||
| 96 | $dateColumn = $isStarted ? 'started_at' : 'finished_at'; |
||
| 97 | |||
| 98 | $result = DB::table(Larupload::FFMPEG_QUEUE_TABLE)->where('id', $this->queueId)->update([ |
||
| 99 | 'status' => $status, |
||
| 100 | 'message' => $message, |
||
| 101 | $dateColumn => now(), |
||
| 102 | ]); |
||
| 103 | |||
| 104 | if ($result and $status) { |
||
| 105 | event(new LaruploadFFMpegQueueFinished($this->id, $this->model, $this->queueId)); |
||
| 106 | } |
||
| 107 | |||
| 108 | return $result; |
||
| 109 | } |
||
| 110 | |||
| 111 | protected function availableQueues(): int |
||
| 112 | { |
||
| 113 | return DB::table(Larupload::FFMPEG_QUEUE_TABLE) |
||
| 114 | ->where('record_id', $this->id) |
||
| 115 | ->where('record_class', $this->model) |
||
| 116 | ->where('status', false) |
||
| 117 | ->count(); |
||
| 118 | } |
||
| 119 | } |
||
| 120 |