Reputation: 5476
There is literally no tutorial about using Heroku Scheduler with Node.js. Assume that I have a function called sayHello() and I would like to run it every 10 mins. How can I use it in controller. In ruby you write rake function_name() however no explanation made for Node. Can I write '/sayHello' or I should do extra configuration?
Upvotes: 57
Views: 19943
Reputation: 41
Some updates from 2024. I've tried all the methods provided in the previous answer, but they are not working. So here is the solution that combined the previous answer and my success.
<PROJECT_ROOT>/bin
folder.#!/usr/bin/env node
at the top of your script..js
or .ts
, so your script should looks like this in the folder <PROJECT_ROOT>/bin/myScript
heroku run bash -a your_app_name
in the command line.node bin/myScript
(note: DO NOT include .ts
or .js
)Assume you already have Heroku Scheduler add-on installed, click the Heroku Scheduler and choose Add Job
.
Type the command, please note that although Heroku might give you an example to let you enter something like this node myScript.js
, this won't work.
You have to enter what you entered in the command line, in this case, node bin/myScript
Click Save Job and that's it.
Upvotes: 0
Reputation: 954
Create the file <project_root>/bin/say_hello
:
#! /app/.heroku/node/bin/node
function sayHello() {
console.log('Hello');
}
sayHello();
process.exit();
Deploy to Heroku and test it with $ heroku run say_hello
then add it to the scheduler with task name say_hello
.
Take say_hello.js
as an example of a Node.js script that you would normally run using $ node say_hello.js
.
Turn it into a script by
.js
ending#! /app/bin/node
[1][2]bin
directory [3][1] Read about the shebang on Wikipedia.
[2] The node
executable is installed in app/bin/node
on Heroku. You can check it out by logging into bash on Heroku with $ heroku run bash
then asking $ which node
.
[3] Heroku requires scripts to be placed in the bin
directory. See Defining Tasks in the Heroku Dev Center.
I agree that the Heroku documentation for scheduling tasks is not very clear for anything other than Ruby scripts. I managed to work it out after some trial and error.
Upvotes: 93
Reputation: 621
Thnks so much for the previous answers here.
I found the following worked for me where feed.js
is the script to run as a job on Heroku.:
<PROJECT_ROOT>/bin/feed.js
The contents of feed.js
start with:
#!/usr/bin/env node
async function mediumFeed() {
await fetch('https://medium.com/feed/stokedinfluence')
And end with:
}
mediumFeed();
And on Heroku the job is defined as node bin/medium_feed.js
:
To run the node js script locally feed.js
you can use from the root of your project directory node bin/feed.js
and to run via heroku you can use heroku run feed.js --app <APP_NAME_NOT_PIPELINE_NAME>.
When using heroku command, this will run the job from the server where as running node bin/feed.js
will run locally. Run locally to test and verify the code works, once deployed verify it works with the heroku run...
command
Upvotes: 1
Reputation: 2727
I am confused that nobody tried:
$ heroku run node yourScript.js
So put this in Heroku Scheduler
node yourScript.js
Worked for me.
PS: be sure to import
everything your script needs.
Upvotes: 7
Reputation: 51
Following steps work in my situation.
worker.js
file.
function sayHello() {
console.log('Hello');
}
sayHello();
Here are something should notice
heroku run node worker.js
to check if it work. It should be show 'Hello' in your terminal.Upvotes: 5
Reputation: 15400
Christophe's answer worked for me until I needed to pass a parameter to the script, at which point it failed. The issue is that node
should not be specified in the task. Here is how exactly to get it working:
In your Procfile, define a process type for your script. See below for a typical Procfile with a web process and, for running "scheduled_job.js", a second process type imaginatively named "worker".
web: node app.js worker: node scheduled_job.js
In the Heroku scheduler's Task column, just enter the name of the process type ("worker" in this example) with or without parameters. Don't enter 'node' before it. Heroku shows a dollar sign in front of it, so examples of a valid setup would be $ worker
(run without arguments) or $ worker 123 abc
(to execute scheduled_job.js with arguments "123" and "abc")
Upvotes: 28
Reputation: 1942
A better approach is to define your schedule file called for example worker.js
with following content:
function sayHello() {
console.log('Hello');
}
sayHello();
and then in the heroku schedule, you just write node worker
like you define it in the Procfile
and that's all!
Upvotes: 44