I have a use case where i need to do certain database manipulations automatically every month on a specific date.Currently using Symfony 2.7 framework is it possible to call a controller every month on a specific date??.Any feedback's/ideas would be helpful
Instead of calling a controller, you could probably create a Symfony Console Command. Have a look here at how to do it: https://symfony.com/doc/2.7/console.html
With this option, the only way to execute it would be via the cli.
Then you can call the command from a cron job that runs at the time you want. Some examples of how to schedule jobs using crontab: https://tecadmin.net/crontab-in-linux-with-20-examples-of-cron-schedule/
You can make a Console Command in Symfony and call this command every month with a cron task.
Console command
Related
I've a code base written in Laravel 9 which targets to execute multiple requests to one API with 1000 different API keys + API secrets. Is there a way to actually do this simmultaniously and not one after another ?
I've come up to this thread Laravel Commands, Pthreads and Closure but the answer there doesn't look like it is wokring, in my opinion it shows that everything is kinda executing at the same time because there is sleep(rand(1,3)) inside the OperatorThreaded class.
Any idea ?
A Laravelly way is to do it like so:
Create a RequestYourApi Job (php artisan make:job RequestYourApi or make a class yourself inside App\Jobs).
Dispatch any amount of jobs using RequestYourApi::dispatch([/* args */]) (the arguments will be passed to the constructor of this class, and the job will be put into the queue, see https://laravel.com/docs/9.x/queues)
Run multiple workers using for instance Supervisor (or Laravel Horizon): php artisan queue:work (use --help to see additional options)
Scale up/down your workers depending on how fast you need to process things.
I’m working on a symfony project and I need to execute a task only once at a specific time(for example at 23/06/2022 18:30)
What’s the best way to do that
Is using the crontab a good idea in this case ?
if I understand correctly you want to run an action only once, it's best to do a command and run it manually, if you want to schedule it to run automatically, you can use cron for this purpose, e.g. 30 18 23 06 * php path_of_your_project/bin/console your:command
I am super confused about Laravel's commands vs. console commands and which one I'm supposed to use with the task scheduler.
I am attempting to use a console since that's the one that seems to work with the task scheduler per Laravel's docs but for some reason it doesn't allow me to have any other methods in the command file other than the construct, fire, getArguments, and getOptions. I can't put everything in the fire method as it would be one big mess.
Basically I am trying to have a crawler run every 5 minutes. So other methods deal with various parts of the crawling process.
And then in the fire method, I'm trying to call my main method via $this->run(); but it returns error:
Declaration of App\Console\Commands\Crawler::run() should be compatible with Illuminate\Console\Command::run(Symfony\Component\Console\Input\InputInterface $input, Symfony\Component\Console\Output\OutputInterface $output)
Any ideas how to get around this?
Can anyone tell me, how to access an action URL of symfony using terminal in ubuntu?
For Example:
http://mysymfonyproject.com/index.php/users/sendnewsletter
Same way, I want to set up an action class URL as cron job. Is it possible to set up cron jobs in symfony?
For Example:
http://mysymfonyproject.com/index.php/crons/sendnewsletter
I think a Symfony Task is what you should look into. There's a lot of built-in tasks, but you probably should write your own task and then call it from cli using a command:
php symfony yourTaskNamespace:yourTaskName
Then you can add such command as a regular cronjob task.
You can check detailed instruction how to create custom Symfony tasks at Documentation Page
Using tasks is the best solution (as Tomasz Madeyski suggested). Just be aware that the tasks work a bit differently than the web app (e.g. read about using sfContext in tasks)
You can also use curl to access your sf pages directly from CLI:
curl -sS http://mysymfonyproject.com/index.php/crons/sendnewsletter >> mylog.log
The -sS option will make curl silent unless there's an error. You can skip that if you don't need it.
The last part (>> mylog.log) will append the result of the request to the log file (it's best if you specify whole path to the log file)
As for setting the cron job it's fairly easy no matter if you use curl or sf tasks. Have a look around the web for a tutorial on setting the crontab, there's plenty of them out there.
I am using MVC framework. Now I want to set up cron such that the URL "http://www.xyz.com/controllera/functiona" should be executed. what should i write in the path section for it.
I got something about "GET" command but it wasnt clear.
Can someone please help me out with it?
As you didn't specify any framework the only way to run this cron is this command
wget --spider 'http://www.xyz.com/controllera/functiona'
I assume you are using an MVC framework as controllera is in the url. If it was Kohana (2.3) framework I would have run it by
/usr/bin/php /path/to/index.php controller/method
Most framework has cli interface to run a controller method. Search for your framework.
See these links for different frameworks.
Zend Framework
Kohana 2 & 3
Codeignier
Yii
I don't understand the "module called cron" part of your question. I believe you are confused, cron is a service on Linux and other Unix systems configured thru crontab.
A crontab(5) entry is defined by a time and date and a command to run.
On Linux and Posix systems, you cannot execute or run an URL. Running something involves the execve(2) system call, which requires an executable file path (and arguments).
Perhaps you want to retrieve some URL using the HTTP protocol. You might use a command-line HTTP client, like wget or curl.
So perhaps the command you want to run in your crontab might be
wget http://www.xyz.com/controllera/functiona
but you could use curl
My guess is that you are confused, and don't understand well enough your question. Consider reading some material.
For example, to retrieve that URL once a day at 3 pm, you would have the following crontab entry:
# run everyday at 3 pm a GET HTTP request
0 15 * * * /usr/bin/wget http://www.xyz.com/controllera/functiona
Use the crontab(1) command to configure your crontab (which may contain several entries, and several variable definitions, so you may have to edit it).