我正在尝试为hubot编写一个使用node-cron的coffeescript脚本。我找到了一个这样的例子,它工作得很好:
module.exports = (robot) ->
cronJob = require('cron').CronJob
new cronJob('0 */1 * * * *', everyMinute(robot), null, true)
everyMinute = (robot) ->
-> robot.messageRoom '#billing', 'hey brah!'然而,这是我当前的脚本。它似乎从未触发过节点事件。我基本上是每分钟检查一次远程API的状态,并在hubot的大脑中设置结果。我在这里做错了什么?这基本上是我写的第一个coffeescript,所以仍然可以找到我的脚。
module.exports = (robot) ->
cronJob = require('cron').CronJob
new cronJob('0 */1 * * * *', getOnPoint, null, true)
getOnPoint = ->
robot.http("https://#{pagerDutyDomain}.pagerduty.com/api/v1/schedules/#{pagerDutyService}")
.headers(Authorization: "Token token=\"#{pagerDutyToken}\"", Accept: 'application/json')
.query(since: '2015-11-23T21:15:49Z', until: '2015-11-23T22:15:49Z')
.get() (err,res,body) ->
if res.statusCode isnt 200
msg.send "Request didn't come back HTTP 200 :( got back #{body}"
return
json = JSON.parse(body)
onpoint_person = json.schedule.final_schedule.rendered_schedule_entries[0].user.email
robot.logger.debug "Setting onpoint to #{onpoint_person}"
robot.brain.set 'onpoint_person', onpoint_person.split("@")[0]我查看了coffeescript编译成的JS,很明显我声明函数的方式很重要,但我不知道为什么。
任何帮助都非常感谢。
发布于 2016-04-27 17:58:56
我也有同样的问题,如果你注意到他的方法中有两个->,我通过在cronJob和你想要使用的方法之间创建另一个方法来解决这个问题。
new cronJob('0 */1 * * * *', getOnPointMethod(), null, true)
getOnPointMethod = ->
-> getOnPoint()这将会起作用不知道这是一个多么优雅或好的解决方案,但它确实起到了作用。
发布于 2019-01-31 16:03:11
您需要将函数getOnPoint放在初始化cron之上
https://stackoverflow.com/questions/33882939
复制相似问题