在 Node.js 中使用 Heroku 调度程序 [英] Using Heroku Scheduler with Node.js
问题描述
实际上没有关于在 Node.js 中使用 Heroku Scheduler 的教程.假设我有一个名为 sayHello() 的函数,我想每 10 分钟运行一次.我如何在控制器中使用它.在 ruby 中你编写 rake function_name() 但是没有对 Node.js 做任何解释.我可以写 '/sayHello' 还是我应该做额外的配置?
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?
推荐答案
创建文件
:
#! /app/.heroku/node/bin/node
function sayHello() {
console.log('Hello');
}
sayHello();
process.exit();
部署到 Heroku 并使用 $ heroku run say_hello
对其进行测试,然后将其添加到任务名称为 say_hello
的调度程序中.
Deploy to Heroku and test it with $ heroku run say_hello
then add it to the scheduler with task name say_hello
.
以 say_hello.js
为例,您通常会使用 $ node say_hello.js
运行 Node.js 脚本.
Take say_hello.js
as an example of a Node.js script that you would normally run using $ node say_hello.js
.
把它变成一个脚本
- 删除
.js
结尾 - 在顶部插入'shebang':
#!/app/bin/node
[1][2] - 将其移动到
bin
目录 [3]
- removing the
.js
ending - inserting the 'shebang' at the top:
#! /app/bin/node
[1][2] - moving it into the
bin
directory [3]
[1] 阅读 维基百科上的shebang.
[2] node
可执行文件安装在 Heroku 上的 app/bin/node
中.您可以通过使用 $ heroku run bash
在 Heroku 上登录到 bash 来检查它,然后询问 $ 哪个节点
.
[3] Heroku 需要将脚本放在 bin
目录中.请参阅 Heroku 开发中心中的定义任务.
[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.
我同意用于调度任务的 Heroku 文档对于 Ruby 脚本以外的任何内容都不是很清楚.经过反复试验,我设法解决了这个问题.我希望这会有所帮助.
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. I hope this helps.
这篇关于在 Node.js 中使用 Heroku 调度程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!