Heroku scheduler with Node Js

浪子不回头ぞ 提交于 2019-12-24 05:05:17

问题


I've followed this answer here and it works but when I try to call my app nothing happens. I have my main function in app.js called

function start() { ... }

This is my task stored in bin which I know it works when I call:

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

After: 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                                     

回答1:


You need to export start from where it is defined and your script needs to require the file that has start() defined. Otherwise how would it know where to find it?

So you probably need to modify your app.js and say module.exports = start. Then in the mytask file you do this:

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


来源:https://stackoverflow.com/questions/37951785/heroku-scheduler-with-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!