Enabled photos to be moved via a queue

This commit is contained in:
Deon George 2016-06-23 07:07:44 +10:00
parent e297c5ca7e
commit 4c317a811c
4 changed files with 126 additions and 11 deletions

View File

@ -9,9 +9,13 @@ use App\Model\PhotoPerson;
use App\Model\Photo;
use App\Model\PhotoTag;
use App\Model\Tag;
use Illuminate\Foundation\Bus\DispatchesJobs;
use App\Jobs\PhotoMove;
class Import extends Command
{
use DispatchesJobs;
/**
* The name and signature of the console command.
*
@ -65,22 +69,24 @@ class Import extends Command
// Exclude . & .. from the path.
$files = array_diff(scandir($dir),array('.','..'));
// Determine if our dir is releative to where we store data
$dir = preg_replace('/^\//','',str_replace(config('photo.dir'),'',$dir));
// Add our path
if ($dir)
array_walk($files,function(&$value,$key,$path='') {
if ($path) {
$value = sprintf('%s/%s',$path,$value);
}
},$dir);
}
elseif ($this->option('file'))
{
$files = array($this->option('file'));
$dir = dirname($this->option('file'));
$files = array(basename($this->option('file')));
}
// Determine if our dir is releative to where we store data
$dir = preg_replace('/^\//','',str_replace(config('photo.dir'),'',$dir));
// Add our path
if ($dir)
array_walk($files,function(&$value,$key,$path='') {
if ($path) {
$value = sprintf('%s/%s',$path,$value);
}
},$dir);
// Show a progress bar
$bar = $this->output->createProgressBar(count($files));
$bar->setFormat("%current%/%max% [%bar%] %percent:3s%% (%memory%) (%remaining%) ");
@ -212,6 +218,8 @@ class Import extends Command
$x->photo_id = $po->id;
$x->save();
}
$this->dispatch((new PhotoMove($po))->onQueue('move'));
}
$bar->finish();

37
app/Jobs/PhotoMove.php Normal file
View File

@ -0,0 +1,37 @@
<?php
namespace App\Jobs;
use Log;
use App\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Model\Photo;
class PhotoMove extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
private $photo;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Photo $photo)
{
$this->photo = $photo;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
Log::info('Moving: '.$this->photo->id);
}
}

View File

@ -0,0 +1,37 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateJobsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('jobs', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('queue');
$table->longText('payload');
$table->tinyInteger('attempts')->unsigned();
$table->tinyInteger('reserved')->unsigned();
$table->unsignedInteger('reserved_at')->nullable();
$table->unsignedInteger('available_at');
$table->unsignedInteger('created_at');
$table->index(['queue', 'reserved', 'reserved_at']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('jobs');
}
}

View File

@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateFailedJobsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('failed_jobs', function (Blueprint $table) {
$table->increments('id');
$table->text('connection');
$table->text('queue');
$table->longText('payload');
$table->timestamp('failed_at')->useCurrent();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('failed_jobs');
}
}