Laravel/Lumen Using delay and onQueue at the same time - laravel

i want to use delay and onQueue methods at same time but i'll get error:
dispatch(new MyJob())->delay(Carbon::now()->addHours(2))->onQueue('high');
Error : Call to undefined method Laravel\Lumen\Bus\PendingDispatch::delay()
But in normal laravel app i can do this.

Try to use the Queueable trait in jobs

Related

Laravel - OutputStyle from jobs

I'm running into issues with allowing a Laravel job to interact with the console output.
At the moment I am passing in the OutputStyle from a Command to the Job constructor and assigning it.
I have seen the InteractsWithIO trait but if I use that by itself without assigning the OutputStyle from the command then it says it is null.
Call to a member function title() on null
I have also tried setting $this->output from the container using
$this->output = resolve(OutputStyle::class);
This fails with a
Target [Symfony\Component\Console\Input\InputInterface] is not instantiable while building [Illuminate\Console\OutputStyle].
I've also ran into issues with PHPUnit tests that run through this job. The output from the class is displayed in the test output.
.......................Processing element 1 for "Section"
.......
What's the best way to handle outputting to the console within Laravel that also works with PHPUnit?
Putting the following code in a Service Provider works:
$this->app->bind('console.output', function () {
return new OutputStyle(
new StringInput(''),
new StreamOutput(fopen('php://stdout', 'w'))
);
});
I am then able to say, in my Job,
$this->output = resolve('console.output');
Which gives access to all the methods such as title, section, and table.

Laravel 5.8 Call to undefined method Illuminate\Events\Dispatcher::fire()

I get this error on user registration. I have searched for this problem a lot and still couldn't solve the problem on my side. In laravel 5.8 upgrade, it's written that function fire() is changed to dispatch(), but I can't find any fire() function in any file of my app, so I can see what's happening.
Would appreciate any help. Thanks.
From: https://laravel.com/docs/5.8/events#dispatching-events
Instead of:
Event::fire(new \App\Events\NewUserSignup($new_user));
Do instead:
event(new \App\Events\NewUserSignup($new_user));
The fire method (which was deprecated in Laravel 5.4) of the Illuminate\Events\Dispatcher class has been removed(https://github.com/laravel/framework/pull/26392). You should use the dispatch method instead.
Instead of:
Event::dispatch('customer.created', $customer);
Do this:
Event::dispatch('customer.created', $customer);
OR:
event('customer.created', $customer);

Laravel: How to run a Library in background (Process component)

I'm trying to create a program which generates event dates for the whole year. The program works if the user select few events but if the user select 100+ events and assigned them every week it reaches the Maximum execution time error.
In order to fix this, my idea is to run the program in the background.
I'm using Laravel 5.4, running the script below will call the function
$process = new Process('php -f '.$path.' generate_evnt_prepared ' . $content['evntid']);
$process->run();
Function Script
namespace App\Library\shellexec;
use App\Model\V1\EvntPlanDtl;
use App\Model\V1\EvntPlanDtlPrePo;
use Carbon\Carbon;
class RoutePlan {
//put your code here
public function process($method = null, $param = 0)
{
$this->$method($param);
}
I'm pretty sure that the function is called as I'm getting an error in error_logs that the Models are not found
PHP Fatal error: Class 'App\Model\V1\EvntPlanDtl' not found in C:\xampp\htdocs\rdmsoffice\app\Library\shellexec\EventPlan.php on line 30
PHP Stack trace:
PHP 1. {main}() C:\xampp\htdocs\rdmsoffice\app\Library\shellexec\EventPlan.php:0
PHP 2. App\Library\shellexec\EventPlan->process() C:\xampp\htdocs\rdmsoffice\app\Library\shellexec\EventPlan.php:88
PHP 3. App\Library\shellexec\EventPlan->generate_evnt_prepared ()
Anyone knows how to fix this? I'm open to suggestions if this is the wrong way.
Big thanks!
Sounds like a more scalable way is to create a "chunked" version and fire them into a queue for processing. For example, based off some assumptions about the example you provided, I might suggest one firing one queued message for each Event the user selects.
That adds some overhead to manage, but honestly I've always found it's more effective to take some of that cost up front and do it right, rather than finding workarounds like increasing settings or (shudder) shelling out to sub processes.

How do I mock find method in laravel?

I am trying to write a unit test, and I need to be able to mock an internal call to App::make('ClassName')->find($x). However, when I try this:
$mock = $this->getMockBuilder('ClassName')->
setMethods(['find'])->
getMock();
$mock->method('find')->willReturn('test');
echo $mock->find(1);
I get a PHPUnit_Framework_MockObject_BadMethodCallException with no message describing the error. Looking ad xdebug trace, I see that it calls the mock::find method, then immediately calls spl_autoload_call to get the BadMethodCallException class.
Why is the find method failing? If I use a different method (e.g. findx), then it works perfectly. What is magic about find, and how can I fix it?

Queued Events in Laravel 4: Event::flusher() method not found

According to the Laravel 4 Documentation on queued Events, I tried to register an event flusher this way:
Event::flusher('foo.bar', function($data)
{
Mail::send(array('emails.notification', 'emails.notification_text'), array('content' => $data), function($message)
{
$message
->to('email#example.com', 'My Name')
->bcc('test#example.com')
->subject('Message from Listener');
});
});
But I am getting the following error upon loading of the script:
Call to undefined method Illuminate\Events\Dispatcher::flusher()
I also couldn't find this method in the source codes of L4. But when I change this from Event::flusher() to Event::listen(), everything works as expected.
So my guess is, that the documentation isn't up to date and the Event::flusher() method has been dropped, since Event::listen() does the same work. Or are there any differences between those two methods and I have an error in my code?
You may need to update your libraries using:
$ composer update
If that doesn't work, let us know what your composer.json file looks like - you might be using a beta version if the framework. It was updated very often before the first stable release.

Resources