Pony.fm/app/Jobs/EncodeTrackFile.php

130 lines
3.9 KiB
PHP
Raw Normal View History

2015-10-27 18:20:43 +01:00
<?php
/**
* Pony.fm - A community for pony fan music.
* Copyright (C) 2015 Kelvin Zhang
2015-10-27 18:20:43 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Poniverse\Ponyfm\Jobs;
use Carbon\Carbon;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
use OAuth2\Exception;
use Poniverse\Ponyfm\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;
use Poniverse\Ponyfm\Track;
use Poniverse\Ponyfm\TrackFile;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
class EncodeTrackFile extends Job implements SelfHandling, ShouldQueue
{
use InteractsWithQueue, SerializesModels;
/**
* @var TrackFile
*/
private $trackFile;
/**
* @var
*/
private $isExpirable;
/**
* Create a new job instance.
* @param TrackFile $trackFile
* @param $isExpirable
*/
public function __construct(TrackFile $trackFile, $isExpirable)
{
$this->trackFile = $trackFile;
$this->isExpirable = $isExpirable;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
// Start the job
2015-11-01 17:49:28 +01:00
$this->trackFile->is_in_progress = true;
2015-10-27 18:20:43 +01:00
$this->trackFile->update();
2015-11-01 17:49:28 +01:00
// Use the track's master file as the source
2015-10-27 18:20:43 +01:00
$source = TrackFile::where('track_id', $this->trackFile->track_id)
->where('is_master', true)
->first()
->getFile();
// Assign the target
2015-10-27 18:35:38 +01:00
$this->trackFile->track->ensureDirectoryExists();
2015-11-01 17:49:28 +01:00
$target = $this->trackFile->getFile();
2015-10-27 18:20:43 +01:00
// Prepare the command
$format = Track::$Formats[$this->trackFile->format];
$command = $format['command'];
$command = str_replace('{$source}', '"' . $source . '"', $command);
$command = str_replace('{$target}', '"' . $target . '"', $command);
Log::info('Encoding track file ' . $this->trackFile->id . ' into ' . $target);
// Start a synchronous process to encode the file
$process = new Process($command);
try {
$process->mustRun();
} catch (ProcessFailedException $e) {
Log::error('An exception occured in the encoding process for track file ' . $this->trackFile->id . ' - ' . $e->getMessage());
// Ensure queue fails
throw $e;
} finally {
Log::info($process->getOutput());
}
// Update the tags of the track
2015-11-01 17:49:28 +01:00
$this->trackFile->track->updateTags($this->trackFile->format);
2015-10-27 18:20:43 +01:00
// Insert the expiration time for cached tracks
if ($this->isExpirable) {
2015-11-01 17:49:28 +01:00
$this->trackFile->expires_at = Carbon::now()->addMinutes(Config::get('ponyfm.track_file_cache_duration'));
2015-10-27 18:20:43 +01:00
$this->trackFile->update();
}
// Update file size
$this->trackFile->updateFilesize();
2015-11-01 17:49:28 +01:00
// Complete the job
$this->trackFile->is_in_progress = false;
$this->trackFile->update();
2015-10-27 18:20:43 +01:00
}
/**
* Handle a job failure.
*
* @return void
*/
public function failed()
{
2015-11-01 17:49:28 +01:00
$this->trackFile->is_in_progress = false;
$this->trackFile->expires_at = null;
2015-10-27 18:20:43 +01:00
$this->trackFile->update();
}
}