Laravel notification channel to Telegram - laravel

I want to send my articles to my Telegram channel automatically by the date time I have scheduled, so I should use queue or cron job.
I have used queue, so in local development to send notifications to telegram I refresh the webpage, I don't want it like this, when deployed to production I want it to auto-send when I have scheduled.

I think the cron job will suits best for the scenario, kindly refer the task scheduling from the laravel documentation for the same : https://laravel.com/docs/9.x/scheduling

You should create a Laravel Command. Take you code from the controller and put it there.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class SendTelegram extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'command:telegram';
protected $description = 'Send a Telegram channel';
public function handle()
{
/*Here your code*/
}
}
Then, add a schedule: app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->command('command:telegram')
->everyMinute();
}
Finally; using terminal
php artisan schedule:run
Also, you can create a schedule cron to run your command:telegram

Related

Laravel queue config stop working after some time

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;
}
}

Laravel Queue not running the actual job

I'm facing an issue in my production environment. One specific job is not working as expected when specifically called via the scheduler php artisan schedule:run. As it seems, the queue worker does not run through the code in the handle function of the job. Instead, the job is marked as completed in Horizon. The strange thing is that if I run Laravel Tinker on my production server, and push the job manually to the queue it works as expected.
See below for my setup and code snippets.
Does anyone have an idea what the issue is? No code around this specific job has been touched in months, and the issue just showed up randomly last Friday. The server, docker, and Horizon have been restarted several times without any change in the behavior.
Server Setup
Laravel Version: v8.78.1
Laravel Horizon Version: v5.7.18
Docker PHP Image: php:8.1.0-fpm-alpine3.15
App\Console\Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->job(new AdExportFile(
fileName: 'adwords.csv',
daysToExport: 10,
header: [
'Google Click ID',
'Conversion Name',
'Conversion Time',
'Conversion Value',
'Conversion Currency'
],
timezone: config('app.timezone'),
origin: Visit::SOURCE_GOOGLE
))->hourly();
}
App\Jobs\AdExportFile.php
class AdExportFile implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
public const QUEUE = 'default';
/**
* Create a new job instance.
*
* #return void
*/
public function __construct(
protected string $fileName,
protected int $daysToExport,
protected array $header,
protected string $timezone,
protected string $origin,
protected string $delimiter = ',',
protected int $minBaseCommission = 5000
) {
$this->onQueue(static::QUEUE);
\Log::info('AdExportFile: Running __construct');
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
\Log::info('AdExportFile: Running the handle function');
}
}
Log output
AdExportFile: Running __construct
Edit: This has now spread to a second job, that is called via the "$schedule->call" method

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

Resources