带有 Node Js 的 Heroku 调度器

Heroku scheduler with Node Js

我遵循了这个答案 here 并且它有效,但是当我尝试调用我的应用程序时没有任何反应。我在 app.js 中有一个名为

的主要函数
function start() { ... }

这是我存储在 bin 中的任务,我知道它在我调用时有效:

#! /app/.heroku/node/bin/node
function mytask() {
  start();
}
initScrape();
process.exit();

之后:heroku run init mytask

λ heroku run init_scrape                                 
Running mytask on dyno1... up, run.5157   
/app/bin/mytask:3                                   
  start();                                               
  ^                                                      

ReferenceError: start is not defined                     
    at mytask (/app/bin/mytask:3:3)             
    at Object.<anonymous> (/app/bin/mytask:5:1)     
    at Module._compile (module.js:413:34)                
    at Object.Module._extensions..js (module.js:422:10)  
    at Module.load (module.js:357:32)                    
    at Function.Module._load (module.js:314:12)          
    at Function.Module.runMain (module.js:447:10)        
    at startup (node.js:148:18)                          
    at node.js:405:3                                     

您需要从定义它的地方开始导出,并且您的脚本需要需要定义了 start() 的文件。否则它怎么知道在哪里可以找到它?

所以你可能需要修改你的 app.js 并说 module.exports = start。然后在 mytask 文件中执行此操作:

#! /app/.heroku/node/bin/node
var start = require('./app.js');
function mytask() {
  start();
}
initScrape();
process.exit();