Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heroku scheduler execute a PHP file

i'm trying to run a php script using the heroku scheduler. What command should i put in after the $ in heroku. I keep getting file not found. I have transferred the file i want to run to heroku but not luck with running it with the scheduler.

like image 759
Luke Zammit Avatar asked Nov 26 '12 10:11

Luke Zammit


People also ask

How do I schedule a heroku script?

On the Scheduler Dashboard, click “Add Job…”, enter a task, select a frequency, dyno size, and next run time. Note that the next run time for daily jobs is in UTC. If you want to schedule the job at a certain local time, add the proper UTC offset.

Can heroku run cron job?

Cron To Go is an add-on created for Heroku users to run scheduled jobs based on one-off dynos using cron expressions. Cron To Go combines the simplicity of using cron to schedule jobs with the scale and elasticity of the cloud.

Is heroku scheduler reliable?

Reliable delivery Heroku Scheduler is a free add-on, but it doesn't guarantee that jobs will be executed at their scheduled time, or at all for that matter. While it is rare, the possibility that a job may be skipped or run twice does exist.


2 Answers

The default PHP buildpack on Heroku does not currently have PHP CLI support, so you can only use it to serve web requests via Apache and not for scripts in worker dynos. However, this is possible if you are using a PHP buildpack that does have CLI support.

To test it out, I forked the PHP buildpack, switched out the PHP binary with one that was compiled with CLI support, and put together small demo of running a scheduled PHP job on Heroku. See the project's readme for step-by-step instructions. To use this fork on an existing app, set the buildpack with:

$ heroku config:add BUILDPACK_URL=https://github.com/ryanbrainard/heroku-buildpack-php.git

Note, the release script in my fork sets up the PATH to resolve the php executable in /app/bin/php with just php, unlike the default buildpack that woud require using the absolute path.

like image 162
ryanbrainard Avatar answered Oct 10 '22 19:10

ryanbrainard


How to run a PHP script with Heroku Scheduler?

Testing and setting up the job

With a directory structure and Procfile looking something like this:

├─ Procfile
├─ web/
├── (your webfiles)
├─ worker/
└── myscript.php

Procfile:

web: vendor/bin/heroku-php-apache2 web/
worker: php worker/myscript.php

Then you can test your worker from the command-line like so:

heroku run worker

To schedule a job, go into Heroku Scheduler and add the job in a similar fashion, but without the heroku run segment (else you'll get bash: heroku: command not found errors), just:

worker

Or, alternatively, directly:

php worker/myscript.php

Checking on the job

You can see the job in the app's logs. e.g.:

2017-09-01T14:19:37.397210+00:00 heroku[scheduler.9540]: Starting process with command `php worker/myscript.php` 

Notes

  • The worker name in the Procfile could be set to something else. e.g.: myworker, mysuperduperscript, etc.
  • I included a web section, but it's optional if all you want is a worker / background service and don't need to serve files on the web.

Alternative: if, for whatever reason, you'd rather perform a GET/POST request on a URL, you can use the Temporize Scheduler add-on.

like image 40
Fabien Snauwaert Avatar answered Oct 10 '22 17:10

Fabien Snauwaert