I'm using Laravel with the protonemedia/laravel-ffmpeg package https://github.com/protonemedia/laravel-ffmpeg
I create a Job to handle video conversion and concatenation and this error appears:
TypeError: Argument 1 passed to App\Jobs\VideoConversion::App\Jobs{closure}() must be an instance of FFMpeg\Filters\Video\VideoFilters, instance of FFMpeg\Filters\AdvancedMedia\ComplexFilters given in /Users/fab/Sites/add-intro/app/Jobs/VideoConversion.php:43
Here is the code of the Job:
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\Video;
use Storage, Str, ProtoneMedia\LaravelFFMpeg\Support\FFMpeg;
use FFMpeg\Filters\Video\VideoFilters;
class VideoConversion implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $video;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct(Video $video)
{
$this->video = $video;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$filename = Str::uuid().'.mp4';
FFMpeg::fromDisk('public')
->open(['intro.mp4', $this->video->path])
->addFilter(function (VideoFilters $filters) {
$filters->resize(new \FFMpeg\Coordinate\Dimension(640, 480));
})
->export()
->toDisk('public')
->inFormat(new \FFMpeg\Format\Video\X264)
->concatWithTranscoding($hasVideo = true, $hasAudio = true)
->save($filename);
}
public function failed(Throwable $exception)
{
FFMpeg::cleanupTemporaryFiles();
dd($exception);
}
}
What I'm doing wrong ?
Thanks for help
Related
When adding an item to the queue, for some reason the handle method is not being called.
The Log entry in __construct is appearing but when attempting to log in handle(), nothing appears.
The method i'm using to dispatch is ProcessImport::dispatch($path, $task->task_id);
My queue service is configured to use Redis, and redis is storing all the data accordingly.
I am using Laravel 8. What could be wrong?
<?php
namespace App\Jobs;
use App\Models\Tasks;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Http\Controllers\Products\Products;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Queue;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Queue\Events\JobProcessed;
use Throwable;
class ProcessImport implements ShouldQueue, ShouldBeUnique
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $file_path;
protected $response;
protected $task;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($path, $task_id)
{
Log::info("Importing products (construct)");
$this->task = Tasks::where('task_id', $task_id)->first();
$this->file_path = $path;
Log::info('Importing ' . $path);
}
private function getFilePath() {
return $this->file_path;
}
/**
* Handle a job failure.
*
* #param \Throwable $exception
* #return void
*/
public function failed(Throwable $exception)
{
$this->task->failed($exception->getMessage());
}
/**
* Get the cache driver for the unique job lock.
*
* #return \Illuminate\Contracts\Cache\Repository
*/
public function uniqueVia()
{
return Cache::driver('redis');
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
Log::info("Importing products (handle)");
$this->task->start();
$products = new Products();
$products->importProductsFromCSV($this->getFilePath());
$this->task->success();
Log::info("End of importing products..");
}
}
You've just pushed the jobs onto the queue but haven't started a worker to process them. You can run the worker with:
php artisan queue:work
Im trying to test a job in laravel but with no success, when the job is called from a method inside a controller i get the following error:
“Target [Illuminate\Contracts\Bus\Dispatcher] is not instantiable.”
I already tried AltThree Bus command as an alternative.
Controller method where thw job is called
public function onOpen(ConnectionInterface $conn) {
// Store the new connection to send messages to later
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
Updates::dispatch();
}
Job Class
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class Updates implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
echo "this is a update";
}
}
I want to pass a closure function to job dispatch method of Laravel queue. But i am getting "Serialization of 'Closure' is not allowed" exception. Can anyone please tell me how i can be able to pass closure function to the dispatch method?
Here are the controller codes:
public function syncProductByQueue($id){
$syncCallback=function() use($id){
$this->syncProduct($id);
};
ProcessSyncImport::dispatch($syncCallback);
}
And here is the job class:
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class ProcessSyncImport implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* #return void
*/
private $syncCallBack;
public function __construct($syncCallBack)
{
$this->syncCallBack= $syncCallBack;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$syncCallBack();
}
}
I'm trying to set up Events, Listeners and Jobs in one of my project. The code is able to run if I remove "implements ShouldQueue". But once I include implements ShouldQueue into the Job class, it stops working
My EventServiceProvider
class EventServiceProvider extends ServiceProvider
{
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
'App\Events\IncorpPaid' => [
'App\Listeners\ProcessIncorpReceived',
],
];
}
My Event Class
Namespace App\Events;
use App\Company;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class IncorpPaid
{
use Dispatchable, SerializesModels;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct(Company $company)
{
$this->company = $company;
}
}
My listener
namespace App\Listeners;
use App\Company;
use App\Events\IncorpPaid;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Http\Request;
use Illuminate\Queue\SerializesModels;
class ProcessIncorpReceived
{
//use SerializesModels;
/**
* Create the event listener.
*
* #return void
*/
public function __construct()
{
}
/**
* Handle the event.
*
* #param IncorpPaid $event
* #return void
*/
public function handle(IncorpPaid $event)
{
\App\Jobs\SendKYC::dispatch($event->company);
}
}
And finally my problem code.
The following works. But the problem is using this doesn't queue the job.
namespace App\Jobs;
use App\Company;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class SendKYC
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct(Company $company)
{
$this->company = $company;
}
/**
* Execute the job.
*
* #return void
*/
public function handle(Company $company)
{
//error_log(print_r($this->company->puc,true));
// error_log(print_r($company,true));
\App\Services\SendDocsForSignature::processKYC($this->company);
}
}
But this queues the job but it doesn't work. $this->company returns a blank company model.
namespace App\Jobs;
use App\Company;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class SendKYC implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct(Company $company)
{
$this->company = $company;
}
/**
* Execute the job.
*
* #return void
*/
public function handle(Company $company)
{
\App\Services\SendDocsForSignature::processKYC($this->company);
}
}
Your last code snippet looks almost correct. You just need to declare the instance variable in which you want to store the $company value. In other words, you need to add protected $company; to your SendKYC class to be able to assign the $company value to $this->company.
You SendKYC class should then start out like this:
class SendKYC implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $company;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct(Company $company)
{
$this->company = $company;
}
Error: Missing argument 1 for App\Jobs\ReorderDatabase::handle() is showing,
I need to pass the variable from controller and i need not to use the model,
so How I should proceed.
My controller function code is here
public function postData(Request $request)
{
$updateRecordsArray = Input::get('order');
$this->dispatch(new ReorderDatabase($updateRecordsArray));
return Response::json('Okay');
}
My job RecorderDatabase job code is
<?php namespace App\Jobs;
use App\Http\Requests\Request;
use App\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\DragDropController;
/**
* Class ReorderDatabase
* #package App\Jobs
*/
class ReorderDatabase extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct()
{
}
/**
* Execute the job.
*
* #return void
*/
public function handle($updateRecordsArray)
{
$i = 1;
foreach ($updateRecordsArray as $recordID) {
DB::table('venues')->where('id', '=', $recordID)->update(array('priority' => $i));
$i++;
}
}
}
As #lagbox mentioned, you need to pass this argument into constructor and not handle method.
Your job class should look like this:
<?php namespace App\Jobs;
use App\Http\Requests\Request;
use App\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\DragDropController;
/**
* Class ReorderDatabase
* #package App\Jobs
*/
class ReorderDatabase extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
protected $updateRecordsArray;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($updateRecordsArray)
{
$this->updateRecordsArray = $updateRecordsArray;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$i = 1;
foreach ($this->updateRecordsArray as $recordID) {
DB::table('venues')->where('id', '=', $recordID)->update(array('priority' => $i));
$i++;
}
}
}