Dispatch queue from another queue in laravel 5.1 - php

enter code hereI'm trying to dispatch queue from another queue in laravel rightnow. So for example I have created a job like this :
class CandidateQueue extends Job implements SelfHandling
{
protected $request;
public function __construct($request)
{
$this->request = $request;
}
public function handle()
{
....
$this->dispatch($queueEmail);
}
}
The problem is when executing $this->dispatch(), laravel said "Call to undefined method ....::dispatch()". So How do I trigger this queue from current queue ?
Thanks in advance

Old question, but how I did was instead of
$this->dipatch(new queueEmail);
I did
dispatch(new queueEmail);

Related

Laravel service container - registering an object shared to all services

I have tried to register to the container an Uuid and i have tried to retrive it from a route controller more than once, but the uuid value is not the first registered.
Can anyone help me to understand?
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
if(App::bound('conf')==NULL)
App::instance('conf', Uuid::generate()->string);
}
}
class InstanceController extends Controller
{
public function getUuid()
{
return App::make('conf');
}
}
I need to register an unique value or object that will be accessible to all.
I have also tried to put this code:
config(['uuid' => Uuid::generate()->string]);
in Laravel command handle method:
class RegisteredInstances extends Command
{
public function handle()
{
config(['uuid' => Uuid::generate()->string]);
}
}
and execute it, but when i try to retrive the uuid from a service, the response is null.
Now i have registered a laravel command that do this:
class RegisteredInstances extends Command
{
.
.
.
public function handle()
{
if(App::bound('conf')==NULL)
App::instance('conf', Uuid::generate()->string);
if(config('uuid2')==NULL)
config(['uuid2' => Uuid::generate()->string]);
}
}
A task every minute execute this command and i try to retrive the uuid from a service controller like this:
class InstanceController extends Controller
{
public function getUuid()
{
return App::make('conf');
}
public function getUuid()
{
return config('uuid2');
}
}
The problem, in this case, is that the controller return NULL:
You need to use laravel Configuration (accessing-configuration-values) with AppServiceProvider
Example:
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
config([ 'theconfig.uuid' => $class_here->UUID ]);
}
}
Then to use it, call
config('theconfig.uuid');
anywhere in the program
Is this in Laravel 4? I haven't seen the App::instance markup before, but I found it in Laravel 4.2 docs for the IoC Container.
This looks like a case for using a singleton. You can use this to ensure that conf is only resolved once. Looking at 4.2 docs, you could define your singleton as follows.
App::singleton('conf', function()
{
return Uuid::generate()->string;
});

Is it possible to pass data from controller to kernel schedule?

I'm working on task scheduling to send sms notification to users at a specific date or time. Is it possible to pass data from controller to kernel schedule? Let's say:
protected function schedule(Schedule $schedule)
{
$schedule->command('sms:send')->dailyAt($time);
}
If it is possible, any tips on how to do it? Thank you very much.
You can call controller to Console/Commands/Yourclass
like this in handle() function-
protected $signature = 'showing:rating'; //your command
//you can write and call logic here in handle function
public function handle()
{
$rating = (new CronController())->showingRating();
}
And then you can call your command in Kernel#schedule() function like this -
protected function schedule(Schedule $schedule)
{
$schedule->command('showing:rating')
->hourly();
}
Hope this will help you.

Schedule jobs outside Kernel

I would like to schedule jobs from other parts of the code. So I created my own scheduler class which gets Illuminate\Console\Scheduling\Schedule::class injected as constructor parameter. The scheduler class is resolved with app(..::class).
Everything works fine however jobs scheduled on this instance are never actually scheduled.
One idea was maybe registering the Illuminate\Console\Scheduling\Schedule as singleton.
Ex.:
class JobA extends Job
{
private $taskList;
public function __construct(TaskList $taskList)
{
$this->taskList = $taskList;
}
public function handle()
{
$this->taskList->run();
}
}
class TaskList
{
private $tasks = [
TaskA::class,
TaskB::class,
...
];
public function run()
{
foreach($this->tasks as $task) {
// resolve $task and call it's own run method..
}
}
public function addToSchedule(Schedule $schedule)
{
$scheduler->job(new JobA($this))->everyFiveMinutes();
}
}
The Illuminate\Console\Scheduling\Schedule class is already defined as a Singleton. What would be interesting to understand is that when you say other parts of your code, do you refer to the request lifecycle code or console code? Both have different Kernels and different applications and scheduling was meant to be part of the Console / CLI part of Laravel

How to share variable in the views after firing event laravel 5.2

I have this listener class
class NotifyMR
{
/**
* Create the event listener.
*/
public function __construct()
{
}
public function handle(LeaveRequestWasReviewed $event)
{
return $event->leaveRequest.'->'.$event->statue;
}
}
so I need to share some variables in the views after firing event , so how to do it ?
Via View facade
View::share('key', 'value');

Laravel integration testing jobs

I am trying to run an integration tests for my app. I have those jobs:
StartJob
PrepareJob
PeformJob
StartJob dispatches one or more PrepareJob, every PrepareJob dispatches one PerformJob.
Adding this
$this->expectsJobs(
[
StartJobs::class,
PrepareJob::class,
PerformJob::class
]
);
makes my test fail with error saying
1) JobsTest::testJobs
BadMethodCallException: Method Mockery_0_Illuminate_Contracts_Bus_Dispatcher::dispatchNow() does not exist on this mock object
Removing $this->expectsJobs makes all my tests pass, but I can't assert a given job was run, only whether it modified the DB to a given state.
StartJobs.php
class StartJobs extends Job implements ShouldQueue
{
use InteractsWithQueue;
use DispatchesJobs;
public function handle(Writer $writer)
{
$writer->info("[StartJob] Started");
for($i=0; $i < 5; $i++)
{
$this->dispatch(new PrepareJob());
}
$this->delete();
}
}
PrepareJob.php
class PrepareJob extends Job implements ShouldQueue
{
use InteractsWithQueue;
use DispatchesJobs;
public function handle(Writer $writer)
{
$writer->info("[PrepareJob] Started");
$this->dispatch(new PerformJob());
$this->delete();
}
}
PerformJob.php
class PerformJob extends Job implements ShouldQueue
{
use InteractsWithQueue;
public function handle(Writer $writer)
{
$writer->info("[PerformJob] Started");
$this->delete();
}
}
JobsTest.php
class JobsTest extends TestCase
{
/**
* #var Dispatcher
*/
protected $dispatcher;
protected function setUp()
{
parent::setUp();
$this->dispatcher = $this->app->make(Dispatcher::class);
}
public function testJobs()
{
$this->expectsJobs(
[
StartJobs::class,
PrepareJob::class,
PerformJob::class
]
);
$this->dispatcher->dispatch(new StartJobs());
}
}
I think it has to do something with how I am using a concrete dispatcher, while $this->expectsJob mocks the dispatcher. Might be related to this - https://github.com/laravel/lumen-framework/issues/207. What's the way to solve this?
To me it sounds like there is no dispatchNow()-method. In the Jobs your run dispatch() but the error says dispatchNow() does not exist.
Laravel didn't have the dispatchNow()-method before a certain version (i think Laravel 5.2 ... not sure) but just the dispatch(). Could be that the expectsJobs didn't think about that and fails.
You could try not passing it in one array but use 3 commands:
$this->expectsJobs(StartJobs::class);
$this->expectsJobs(PrepareJob::class);
$this->expectsJobs(PerformJob::class);
Maybe that helps.

Categories