sintonia_webapp/app/Console/Commands/CheckPodcastDownload.php

89 lines
3.2 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\Podcast\CeleryTask;
use App\Models\Podcast\PodcastEpisode;
use App\Models\Podcast\ThirdPartyTrackReference;
use Celery\Celery;
use DateTime;
use DateTimeZone;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class CheckPodcastDownload extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:check-podcast-download';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Check for messages in celeryresults queue to update celery_tasks table for finished tasks';
/**
* Execute the console command.
*/
public function handle()
{
$tasks = CeleryTask::where('status','=','PENDING')->with('aThirdPartyTrackReferences')->get();
if ($tasks->count() > 0) {
foreach ($tasks as $task) {
Log::info($task->task_id);
$queue = 'celeryresults.'.$task['task_id'];
$c = new Celery(
host: config('rabbitmq.host'),
login: config('rabbitmq.user'),
password: config('rabbitmq.password'),
vhost: config('rabbitmq.vhost'),
exchange: 'celeryresults',
binding: $queue,
result_expire: 900000
);
try {
$message = $c->getAsyncResultMessage($task->name, $task->task_id);
if ($message) {
try {
DB::beginTransaction();
$body = json_decode($message['body'], true);
$result = json_decode($body['result'], true);
$podcastEpisode = PodcastEpisode::where('id', '=',$result['episodeid'])->first();
$podcastEpisode->fill([
'file_id' => $result['fileid'],
])->save();
$timezone = new DateTimeZone(getenv('APP_TIMEZONE'));
$thirdPartyTrackReference = ThirdPartyTrackReference::where('foreign_id','=',$podcastEpisode->id)->first();
$thirdPartyTrackReference->fill([
'file_id' => $result['fileid'],
'upload_time' => new DateTime('now', $timezone)
])->save();
$celeryTask = CeleryTask::where('task_id','=',$task->task_id)->where('track_reference','=',$thirdPartyTrackReference->id)->first();
$celeryTask->fill([
'status' => 'SUCCESS'
])->save();
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
Log::error($e->getMessage());
}
}
} catch (\Exception $e) {
Log::error('test '.$e);
}
}
}
}
}