2017-06-06 4 views
0

コンテキスト:NodeJS動的サービスローダー&ランナー

私はその 安いスケート 金融に精通した人であること、私は上のアクティブなよチャネルのためのいくつかのたるみフック/通知サービスを構築しようとしています私は、無料のサービスを利用したいと思っています(Herokuアカウントやそれに類似する製品を追跡する)ので、複数のサービスを1つのインスタンスで実行できるようにしたいと考えています。

一般的なランナーを作成しました。これは設定に基づいており、いくつかのノードモジュールを選択して構成設定を提供する必要があります。

"オートローダ": "^ 0.2.0"、 "ノードのcron":私は、次のnode_modules使用してい

"^ 1.2.0" を、 「読み取りYAML 「: "^ 1.1.0"

config.yml

foo: 
    cron: 1 * * * * 
    url: http://www.foo.com 
    team: 
    - 
     slackId: bar 
     bnetId: 1 
     eloId: 1 
    - 
     slackId: baz 
     bnetId: 2 
     eloId: 2 

app.js

const autoLoader = require('auto-loader'); 
const readYaml = require('read-yaml'); 
const cron = require('node-cron'); 

const services = autoLoader.load(__dirname +'/services') 

readYaml('config.yml', function(err, conf) { 

    if (err) throw err; 

    Object.keys(conf).forEach(function (key) { 

     console.log('Creating CRON for ' + key); 

     if(cron.validate(conf[key].cron)) {    

      console.log(conf[key].cron, '-> is valid cron'); 

      // the cron task below does not seem to fire ever 
      cron.schedule(conf[key].cron, function(){ 
       services[key](conf[key]); 
      }); 

     } else { 
      console.log('Cron invalid for::' + key) 
     } 

    }); 
}); 

サービス/ foo.jsは

module.exports = function (config) { 
    console.log('foo entered!!'); 
    console.log(config) 
} 

質問:

私は何をしないのですか?

NodeJSで動作するプロセスの長さについて、概念的に何か不足しています(これは私の最初のものです) 、または私はバグにスーパー(分かりません)が不足しています。

NodeJSで長時間実行されるタスク/プロセスを、別々のスケジュールされたコードセクション/タスクでどのように作成しますか?

答えて

1

コード自体は期待通りに機能します。この問題は、cronの設定にあるようです。

cron: 1 * * * *は1時間後に実行されます。

cron: "*/1 * * * *"  # would run it every minute 
cron: "*/5 * * * *"  # would run it every 5 minutes 
cron: "*/10 * * * * *" # would run every 10 seconds 
関連する問題