问题描述
实际上没有关于使用Heroku Scheduler和Node.js的教程。假设我有一个名为 sayHello()的函数,我希望每10分钟运行一次。我如何在控制器中使用它。在ruby中,你写了 rake function_name(),但是没有对Node做任何解释。我可以写'/ sayHello'或者我应该做额外的配置吗? 创建文件 code>< project_root> / bin / say_hello :
#! / app / bin / node
function sayHello(){
console.log('Hello');
}
sayHello();
process.exit();
部署到Heroku并使用来测试$ heroku run say_hello 然后将它添加到任务名称为 say_hello 的调度程序中。
解释
say_hello.js 作为您通常使用 $ node say_hello运行的Node.js脚本的示例。 js 。
通过
- 删除 .js 结尾
- 在顶部插入'shebang':#! / app / bin / node [1] [2]
将
- 移动到 bin目录下[ 3]
阅读关于。
我同意,除了Ruby脚本以外,其他任何计划任务的Heroku文档都不是很清楚。经过一些试验和错误之后,我设法解决了这个问题。我希望这有助于。
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?
Create the file <project_root>/bin/say_hello:
#! /app/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.
Explanation
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
- removing the .js ending
- inserting the 'shebang' at the top: #! /app/bin/node [1][2]
- moving it into the 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. I hope this helps.
这篇关于使用带有Node.js的Heroku Scheduler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!