简体   繁体   English

设置 node-cron 与 Hubot 一起运行

[英]Setting up node-cron to run with Hubot

I'm trying to setup Hubot to run a cronjob but for some reason, the cronjob using node-cron is not firing.我正在尝试设置 Hubot 来运行 cronjob,但由于某种原因,使用 node-cron 的 cronjob 没有触发。 I've read and implemented things a few different ways (one example: https://leanpub.com/automation-and-monitoring-with-hubot/read ) but nothing seems to work.我已经通过几种不同的方式阅读和实现了一些东西(一个例子: https : //leanpub.com/automation-and-monitoring-with-hubot/read )但似乎没有任何效果。 Currently the code I'm using is目前我使用的代码是

module.exports = (robot) ->
  cronJob = require('cron').CronJob
  tz = 'America/Los_Angeles'
  pattern = '*/1 * * * *'
  cronjob = new cronJob(pattern, everyMinute, null, true, tz)

  console.log "reading cron"
  room = "#testing"
  robot.messageRoom room, 'startup message'

  everyMinute = ->
    robot.messageRoom '#testing', 'I will nag you every minute'
    console.log "every minute should be executed"

I see the startup messages but the messages in everyMinute don't come up in the room or the log.我看到了启动消息,但 everyMinute 中的消息没有出现在房间或日志中。 I've tried different formats for the pattern but haven't had any luck.我为模式尝试了不同的格式,但没有任何运气。

What's odd is hubot-cron ( https://github.com/miyagawa/hubot-cron ) works fine.奇怪的是 hubot-cron ( https://github.com/miyagawa/hubot-cron ) 工作正常。 I can setup a job and see the output message from a cronjob so I know it works.我可以设置一个作业并查看来自 cronjob 的输出消息,所以我知道它有效。 If I look through the hubot-cron source, I see如果我查看 hubot-cron 源,我会看到

start: (robot) ->
    @cronjob = new cronJob(@pattern, =>
      @sendMessage robot
    , null, false, @timezone)
    @cronjob.start()

This looks like what I'm doing but hubot-cron works and my code doesn't.这看起来像我在做什么,但是 hubot-cron 可以工作,而我的代码却没有。 Any ideas?有任何想法吗?

It turns out that node-cron didn't like the way I was passing that function.事实证明,node-cron 不喜欢我传递该函数的方式。 Looks like the proper way to pass a function to node-cron is看起来将函数传递给 node-cron 的正确方法是

module.exports = (robot) ->
  cronJob = require('cron').CronJob
  tz = 'America/Los_Angeles'
  pattern = '* * * * *'
  new cronJob(pattern, (->
    do everyMinute
  ), null, true, tz)

  everyMinute = ->
    console.log "every minute should be executed"

This works for me.这对我有用。 Note that have the 'do everyMinute' on a separate line is necessary to avoid coffeescript complaining about a trailing comma.请注意,将“do everyMinute”放在单独的行上是必要的,以避免 coffeescript 抱怨尾随逗号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM