Closed lotestudio closed 4 years ago
Handle method for my custom command:
$host = 'host';
$port = 6001;
$connection = @fsockopen($host, $port);
if (is_resource($connection))
{
$this->info('Websocket running!');
fclose($connection);
}
else
{
$this->info('Port is not responding. Starting server...');
\Illuminate\Support\Facades\Artisan::call('websockets:serve');
}
You should use Supervisor to make sure the process is always running. See Laravel docs for guidance on configuration: https://laravel.com/docs/5.8/queues#supervisor-configuration
Using a cron for this is not the right approach. You should definitely be using a tool like supervisord or another kind of init tool. Cron/schedulers are meant for executing tasks like purging old records, sending out daily/whatever emails, triggering some scheduled backups, etc. Keeping a process running is a more specialized task and there are tools that are built specifically for that job.
on \App\Console\Kernel.php
/**
* Define the application's command schedule.
*/
protected function schedule(Schedule $schedule): void
{
$schedule->command('websockets:serve')->when(function (){
$connection = @fsockopen('*** YOUR WEBSOCKET ADDRESS ***', '6001');
if (is_resource($connection))
{
//WEBSOCKET IS RUNNING
fclose($connection);
return false;
}
else
{
//RUN WEBSOCKET
return true;
}
});
}
AND it is important to know that schedule can not run automaticcaly there is two way to run them
cmd
or terminal
run php artisan schedule:run
(* * * *)
command /usr/bin/php /ADDRESS TO YOUR PROJECT/artisan schedule:run
Is it a good idea to use laravel task scheduler to check server is running? Something like:
And if yes, how can I check server running? Thanks in advance!