Laravel queue config stop working after some time - laravel

Currently I have a cron running that calls a command and adds this job to my queue.
This works normally up to a point, then the job runs but doesn't add anything to the queue, so I have to log into the server and give an artisan config:clear to get everything running again.
Does anyone have an idea what it could be? I'm using forge to perform server deployments and management, my queue is using redis driver, laravel 9, horizon and octane, php 8.1, mysql
Just to be clear: my problem is not happening while running the jobs, when the job arrives in the queue the horizon is processing perfect. the biggest problem is when adding item to the queue, that when cron goes to run, all of a sudden it doesn't find the settings of which queue it should use anymore and doesn't add anything to the queue :(
Example of command running using crontab:
namespace App\Console\Commands;
use App\Jobs\MyJob;
use Illuminate\Console\Command;
class MyCommand extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'cron:myCommand';
/**
* The console command description.
*
* #var string
*/
protected $description = '';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return int
*/
public function handle()
{
MyJob::dispatch()->onQueue('my_queue');
return Command::SUCCESS;
}
}

Related

why cron job stoped in new year 2021 in laravel

every thing works fine till 31 decemeber 2020 and in new year my cron job stoped working i dont know what happend i am using spatie laravel package to take db backup an i also have one other cron job in console and command
kernal.php
`<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
'App\Console\Commands\UpdateUserNotNew',
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
$schedule->command('cron:update-user-not-new')->everyMinute();
$schedule->command('backup:run')->everyMinute();
$schedule->command('backup:clean')->everyMinute();
}
/**
* Register the commands for the application.
*
* #return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}`
my issue is resolved byhttps://stackoverflow.com/a/65890784/14913109
only my php version was not integrated with php multimanager
The problem is most likely not coming from spatie/laravel-backup package. To run Laravels cronjob you need to a have a central call in your crontab to Laravels scheduler
* * * * * cd /path-to-your-project && php artisan schedule:run
You can also check all the scheduled tasks locally by running
$ php artisan schedule:work
and if they run fine locally then there is an issue with your server, but not with Laravel.
Also always have a look at storage/logs/laravel.log if there are any errors showing up.

Laravel: Set/mock application time globally for development

On my application, users have lists of emails they can send to. Their accounts have settings for the time of day they want emails automatically sent and the timezone they're in.
I would like to test certain scenarios on when my queues are triggered since each user's send time may differ drastically.
I'd like to globally set a fake time with carbon.
In public/index.php, I tried to set:
$time = Carbon::create('2020-09-16 00:00:00');
Carbon::setTestNow($time);
but pieces of my application are not affected.
Is there a global way to set a fake time?
Original question below:
On my application, users have lists of emails they can send to. Their accounts have settings for the time of day they want emails automatically sent and the timezone they're in.
I have a command that will trigger an event that sends email.
Inside the listener, the handle method looks like:
public function handle(ReviewRequested $event)
{
$time = Carbon::create(2020, 9, 15, 0);
Carbon::setTestNow($time);
$reviewRequest = $event->reviewRequest;
Log::info('email sending at ' . $reviewRequest->sent_at . ' and current time is ' . Carbon::now());
Mail::to($reviewRequest->customer->email)
->later($reviewRequest->sent_at, new ReviewRequestMailer($reviewRequest));
}
Note that I'm faking the time with Carbon and setting it to midnight. In this example, The emails should be sent at 9am. The logged info is as follows:
local.INFO: email sending at 2020-09-15 09:00:00 and current time is 2020-09-15 00:00:00
So the current time is 12AM and I'm queuing these up to get sent at 9AM.
As soon as I run php artisan queue:work, the pending jobs (emails) are immediately run and sent. Why is this happening? They should remain queued until 9AM.
Perhaps queuing is using system time and doesn't care about what I set in Carbon? How can I resolve this?
Edit: I forgot to mention that I'm using Redis
Check what queue driver you're using in your .env file. QUEUE_CONNECTION=sync does not allow for any delaying (sync stands for synchronous).
The quickest way to fix this would be doing the following:
change the driver to database QUEUE_CONNECTION=database
clear the cached configuration php artisan config:clear
publish the migration for the jobs table php artisan queue:table
migrate this new table php artisan migrate
After following these steps, you can now have delayed execution in your queues when you run it with php artisan queue:work
I think you should use Laravel Cron Job for this purpose. you should make a file in App/Console/Commands/YourCronJobFile.php
<?php
namespace App\Console\Commands;
use App\TestingCron;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class TestingCronJob extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'send:Mail';
/**
* The console command description.
*
* #var string
*/
protected $description = 'This command is use for test cron jobs.';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
DB::table('testing_cron')->insert(['created_at' => now(),'updated_at' => now()]);
}
}
Then go to directory App/Console/Kernel.php
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
Commands\TestingCronJob::class
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('send:Mail')->dailyAt('09:00');
}
/**
* RegisterController the commands for the application.
*
* #return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
https://laravel.com/docs/7.x/scheduling

Laravel 5.4 - schedule:run works only once

I made a custom artisan command called db:dump which dumps my database into a sql file.
I tried applying it into a scheduler so it'll backup every time depending on the time range I want to apply. In this case, I tested it with everyMinute()
Here's what my Kernel look's like.
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
Commands\MySqlDump::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('db:dump')
->everyMinute();
}
/**
* Register the Closure based commands for the application.
*
* #return void
*/
protected function commands()
{
require base_path('routes/console.php');
}
}
It only works once when I execute php artisan schedule:run in my command prompt. Tried waiting for 5 minutes to see if it will backup 5 times but it didn't. I tried running schedule:run repeatedly and that's when it only worked each time. Basically speaking, it dumps an sql file every time I run schedule:run.
I'm totally lost now, advanced thanks to those who can help me. :)
You need to run cronjob on control panel plan servers like Plesk or directadmin or Cpanel.
egg:visit
If you are running locally, then you should use the following.
php artisan schedule:work
When you are on a live server, you have to set the cron configuration like the following
https://laravel.com/docs/8.x/scheduling#running-the-scheduler-locally

Laravel 5.1: Run Custom Artisan Command in Background

I'm working on a chat application using the Ratchet package. With the help of tutorials I've written a custom artisan command to start the Websocket server. I need to run this Artisan command in the background and it should be running all the time. How do I do it?
I tried using Artisan::queue and Artisan::call from Artisan Facade. But since my custom command runs indefinitely(for a long time) it isn't working.
Edit:
My hosting provider is not allowing me to run Artisan commands through ssh.
Here is the code for the Custom Artisan Command:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Ratchet\Http\HttpServer;
use Ratchet\Server\IoServer;
use Ratchet\WebSocket\WsServer;
use App\Classes\Socket\ChatSocket;
use App\Classes\Socket\Base\BaseSocket;
class ChatServer extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'chat_server:serve';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$this->info("Start server");
$server = IoServer::factory(
new HttpServer(
new WsServer(
new ChatSocket()
)
),
8080
);
$server->run();
}
}
You simply should run it in console with command:
php artisan chat_server:serve
And probably you should make sure it will work all the time. One of the ways is using Supervisor to make sure this command will run (almost) all the time

Laravel 5 - Queued Commands throwing spl_autoload_call() error

UPDATE - This has been narrowed down to beanstalkd, sync works
I am receiving the following error when attempting to run queued commands in my production environment:
exception 'ErrorException' with message 'unserialize(): Function spl_autoload_call() hasn't defined the class it was called for'
in /home/forge/default/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php:74
I have tried both the beanstalkd and database drivers, no change. For simplicity, I am using the following command:
<?php namespace App\Commands;
use App\Commands\Command;
use App\User;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldBeQueued;
class TestQueueCommand extends Command implements SelfHandling, ShouldBeQueued {
use InteractsWithQueue, SerializesModels;
/**
* #var User
*/
private $user;
/**
* Create a new command instance.
*
* #param User $user
*/
public function __construct(User $user)
{
//
$this->user = $user;
}
/**
* Execute the command.
*
* #return void
*/
public function handle()
{
\Log::info("You gave me " . $this->user->fullName());
}
}
Dispatch code:
get('queue-test', function()
{
Bus::dispatch(new TestQueueCommand(User::first()));
});
This works in my Homestead environment, fails in production (Digital Ocean, Forge). I have several beanstalkd workers and I have tried restarting them. I have also run php artisan queue:flush.
Here is the code where the error is occurring (from source):
/**
* Handle the queued job.
*
* #param \Illuminate\Contracts\Queue\Job $job
* #param array $data
* #return void
*/
public function call(Job $job, array $data)
{
$command = $this->setJobInstanceIfNecessary(
$job, unserialize($data['command'])
);
$this->dispatcher->dispatchNow($command, function($handler) use ($job)
{
$this->setJobInstanceIfNecessary($job, $handler);
});
if ( ! $job->isDeletedOrReleased())
{
$job->delete();
}
}
In the past, I also ran into a similar issue while unserializing. The problem was the default Beanstalk job size (65,535 bytes), which might not be big enough if the class being serialized contains lots of properties that need to be kept (increasing the size of the serialized string and using more than 65K for storage).
In order to solve this, try setting the size to 131,072 or even 262,144 bytes using the -z option, on the configuration file (/etc/default/beanstalkd):
BEANSTALKD_EXTRA="-z 262144"
After that, you should restart the service.
Also note that the configuration file path might be other, depending on the distribution you're using.
And since you're using Digital Ocean, you might find their documentation useful.

Resources