'use strict';

const path = require('path');
const Promise = require('bluebird');
const debug = require('../src/util/debug')('bin:schedule');
const connection = require('../src/mongo/connection');

// Config
const config = require('../src/config/config').globalConfig;
const sources = Object.keys(config.source);
const aggregations = Object.keys(config.aggregation);

const syncHistory = require('../src/mongo/models/syncHistory');
const schedulerLog = require('../src/mongo/models/schedulerLog');

const ProcessScheduler = require('process-scheduler');

Promise.coroutine(function* () {
    yield connection();
    const schedulerConfig = {
        source: 4,
        aggregation: 4
    };

    const schedule = [];
    // Create configuration
        for (const collection of sources) {
            schedule.push({
                id: 'source_copy_' + collection,
                worker: path.join(__dirname, '../src/source/copyWorker.js'),
                immediate: false,
                cronRule: config.source[collection].copyCronRule,
                deps: [],
                noConcurrency: ['source_remove_' + collection],
                arg: config.source[collection],
                type: 'source'
            });
            schedule.push({
                id: 'source_remove_' + collection,
                worker: path.join(__dirname, '../src/source/removeWorker.js'),
                immediate: false,
                cronRule: config.source[collection].removeCronRule,
                deps: [],
                noConcurrency: [],
                arg: config.source[collection],
                type: 'source'
            });
        }

        for (const collection of aggregations) {
            let aggId = 'aggregation_' + collection;
            schedule.push({
                id: aggId,
                worker: path.join(__dirname, '../src/aggregation/worker.js'),
                immediate: false,
                arg: collection,
                type: 'aggregation'
            });

            let sources = Object.keys(config.aggregation[collection].sources);
            for(const source of sources) {
                let s = schedule.find(s => s.id === 'source_copy_' + source);
                if(s) {
                    s.deps.push(aggId);
                    s.noConcurrency.push(aggId);
                }
                s = schedule.find(s => s.id === 'source_remove_' + source);
                if(s) {
                    s.deps.push(aggId);
                    s.noConcurrency.push(aggId);
                }
            }
        }

    var scheduler = new ProcessScheduler(schedulerConfig);
    scheduler.on('change', function(data) {
        console.log(new Date(), data.id, data.status);
        if(data.status === 'error') console.log(data.stderr);
        schedulerLog.save(data);
    });
    scheduler.schedule(schedule);

})();
@@ -635,16 +635,39 @@
 fig = %7B%0A
+        threads: %7B%0A    
         
@@ -677,16 +677,20 @@
 rce: 4,%0A
+    
         
@@ -704,16 +704,26 @@
 tion: 4%0A
+        %7D%0A
     %7D;%0A%0A

fix scheduler thread config