R
Railway•2y ago
luag2124

Set a Cron Job in Laravel

Hello, I just read this post: https://blog.railway.app/p/cron-jobs and I am trying to create and run a cron job in my laravel application. Does anyone has any idea of how this can be done in a laravel project? I created a file named "crone.ts" in the root directory with the next code: const cron = require('node-cron'); const shell = require('shelljs'); // Schedule tasks to be run on the server. cron.schedule('* * * * *', function() { console.log('Running cronjobs'); shell.exec('php artisan schedule:run >> /dev/null 2>&1'); }); I created a task in the Laravel scheduler (.\app\Console\Kernel.php) with the next code: $schedule->call(function () { info("called every minute"); })->everyMinute(); And I modified my npm start script, so it now is: "start": "php artisan serve && node /app/cron.ts&" But I still can't make it work, any help would be really appreciated.
Railway Blog
Cron Jobs on Railway
A brief guide on deploying and running cron jobs on Railway using JavaScript.
23 Replies
Percy
Percy•2y ago
Project ID: 919d7c14-3b5b-4e9d-b55f-8be1a2de52d3
luag2124
luag2124OP•2y ago
919d7c14-3b5b-4e9d-b55f-8be1a2de52d3
Brody
Brody•2y ago
don't know why you need to involve node into this https://laravel.com/docs/10.x/scheduling
luag2124
luag2124OP•2y ago
Thank you for your answer @Brody So if you read the documentation you just sent: https://laravel.com/docs/10.x/scheduling#running-the-scheduler It says: "So, when using Laravel's scheduler, we only need to add a single cron configuration entry to our server that runs the schedule:run command every minute." The problem with Railway is that there isn't any first-party support for cron jobs, or any other simple way to add this configuration entry. That's why I am trying to configure it by using the ts file.
Brody
Brody•2y ago
whats wrong with php artisan schedule:work
luag2124
luag2124OP•2y ago
That's only used to run and test the schedule locally... or what are you suggesting here? As I said, to be able to run the cron jobs on the server it's necessary to to add a cron configuration entry to the server (crontab).
Brody
Brody•2y ago
yes it does say its used locally, but it also doesnt say you cant use it in production
luag2124
luag2124OP•2y ago
Are you sure about this? I've make many tests and I am still not able to make my scheduled tasks work, how am I supposed to run php artisan schedule:work in prod?
Brody
Brody•2y ago
what are you even trying to schedule to begin with?
luag2124
luag2124OP•2y ago
The goal is to make a database backup, but I first tried to do a simple: info("Hello World!"); to begin with, but it didn''t work either
Brody
Brody•2y ago
then make a seprate service to do database backups https://blog.railway.app/p/automated-postgresql-backups
luag2124
luag2124OP•2y ago
Yeah... I saw this blog post earlier, but I am backing up my database data into Google Drive, not Amazon S3, and it's already working locally though, I know this is a good alternative, but I should be able to do it from the same service, right?
Brody
Brody•2y ago
doing it from the same service is not advisable
luag2124
luag2124OP•2y ago
Why? 😮 That's the purpose of the Laravel Scheduler
Brody
Brody•2y ago
you are just making things needlessly complicated for yourself, write a simple node cron app to backup the database, job done
luag2124
luag2124OP•2y ago
With server access or first-party support this is'nt complicated at all, that's the whole point of the Laravel Scheduler, avoiding to write unecessary code or add unecessary services, but it's ok if ther's no other alternative I guess I don't have any other option
Brody
Brody•2y ago
so undo all that node stuff you did, and just write a simple node app to dump and upload the dump to google drive, simple
luag2124
luag2124OP•2y ago
Don't get me wrong, I like Railway and I've loved it so far, but adding support for something like this would be really great. https://fly.io/docs/laravel/the-basics/cron-and-queues/
renatoalvarez
renatoalvarez•2y ago
Im not able to use php artisan queue:work in production
luag2124
luag2124OP•2y ago
It's just my point of view @Brody as a developer that has been using Railway for a small period of time, my intention is not to compare or criticize what we have in a bad way
Brody
Brody•2y ago
^
njoguamos
njoguamos•2y ago
To set up a cron scheduler, create a new instance of the app using the same env variable used in the main instance. Then change the start command to php artisan schedule:work. As Brody said there is nowhere it is written that you cannot use this in production. I have been using it without any issues. Regarding backing up your Laravel database, consider using spatie/laravel-backup. This will allow you to schedule a command like this $schedule->command(command: 'backup:run')->everySixHours(); You can backup to any file system e.g S3 at any interval you want.
renatoalvarez
renatoalvarez•2y ago
I will try it
Want results from more Discord servers?
Add your server