你怎么能每天自动运行咕噜声?

时间:2014-02-11 12:28:39

标签: cron gruntjs protractor

我想每天自动运行量角器端到端测试。

目前我可以做到

$ grunt

在测试目录中运行。

有没有办法让它们在特定时间每天自动运行?

我尝试过使用cron,但是根据this,我必须将所有需求路径都设置为绝对路径,我不想这样做。

2 个答案:

答案 0 :(得分:2)

使用节点脚本和setInterval

var exec = require('child_process').exec
var path = require('path')
var running = false

var run = function(what, where) {
  if (running === true) return
  running = true

  // by default, just run grunt
  what = what || 'grunt'
  // by default, run on grunt in the current working directory
  where = path.resolve(where || path.join(process.cwd(), 'Gruntfile.coffee'))

  what += ' --gruntfile=' + where
  exec(what, { cwd: path.dirname(where) }, function(err, stdout, stderr) {
    if (err || stderr) { /* log the error somewhere */ }
    /* log the stdout if needed*/
    console.log(stdout)
    running = false
  })
}

setInterval(function() {
  run(/* set what to run, where to run */)
  /* or even multiple gruntfiles and node projects */
}, 24 * 60 * 60 * 1000) // once a day

这是平台无关的,易于启动和停止,易于维护和定制。

查看像https://www.npmjs.org/package/forever这样的库,以便永远运行节点脚本。或许多其他方式:nohup,monit,upstart等。

答案 1 :(得分:1)

使用Kyle Robinson Young的答案以及节点模块cron,我想出了以下每天下午5:00运行gruntfile的以下内容:

var cronJob = require('cron').CronJob
  , exec = require('child_process').exec
  , path = require('path')
  , running = false
  ;

var run = function(what, where) {
  if (running === true) {
    return;
  }
  running = true;

  // by default, just run grunt
  what = what || 'grunt';
  // by default, run on grunt in the current working directory
  where = path.resolve(where || path.join(process.cwd(), 'Gruntfile.js'));

  what += ' --gruntfile=' + where;

  exec(what, { cwd: path.dirname(where) }, function(err, stdout, stderr) {
    if (err || stderr) {
      console.log(err);
    }
    /* log the stdout if needed*/
    console.log(stdout);
    running = false;
  });
};

new cronJob('00 00 17 * * *', function(){
    console.log('Running Gruntfile ' + new Date());
    var what = 'grunt'
      , where
      ;

    run(what, where);
}, null, true);