var cron = require('cron')
var app = require('../server')
var statusConfig = require('../../config/status.json')
var utility = require('../../public/utility')

var rankingHelper = require('../../common/helpers/rankingHelper')

var startCampaign = cron.job("0 */1 * * * *", function () {
  campaign = app.models.campaign
  campaign.find({
    where: {
      'status': statusConfig.approved,
      'startStyle': "Automatic"
    }
  }, function (err, campaignList) {
    if (err)
      console.error(err)
    for (var i = 0; i < campaignList.length; i++) {
      if (campaignList[i].beginningTime <= utility.getUnixTimeStamp() && campaignList[i].endingTime >= utility.getUnixTimeStamp()) {
        campaignList[i].updateAttribute('status', status.started, function (err, campaignInst) {
					if (err)
						console.error(err)
          rankingHelper.setRankingAndWeight(campaignList[i], function(err, result) {
            if (err)
              console.error(err)            
          })
        })
      }
    }
  })
})

var finishCampaign = cron.job("0 */1 * * * *", function () {
  campaign = app.models.campaign
  campaign.find({
    where: {
      'status': statusConfig.started
    }
  }, function (err, campaignList) {
    if (err)
      console.error(err)
    for (var i = 0; i < campaignList.length; i++) {
      if (campaignList[i].endingTime < utility.getUnixTimeStamp()) {
        campaignList[i].updateAttribute('status', status.finished, function (err, campaignInst) {
					if (err)
						console.error(err)
          rankingHelper.recalculateRankingAndWeight(function(err, result) {
            if (err)
              console.error(err)
          })
        })
      }
    }
  })
})

var recalculateRanking = cron.job("0 */1 * * * *", function () {
  rankingHelper.recalculateRankingAndWeight(function(err, result) {
    if (err)
      console.error(err)
  })
})

startCampaign.start()
finishCampaign.start()
recalculateRanking.start()
@@ -736,16 +736,22 @@
 , status
+Config
 .started
@@ -874,23 +874,20 @@
 campaign
-List%5Bi%5D
+Inst
 , functi
@@ -957,16 +957,17 @@
 ror(err)
+%0A
         
@@ -970,16 +970,35 @@
         
+console.log(result)
 %0A       
@@ -1456,16 +1456,22 @@
 , status
+Config
 .finishe
@@ -1665,32 +1665,64 @@
 sole.error(err)%0A
+            console.log(result)%0A
           %7D)%0A   
@@ -1923,16 +1923,40 @@
 or(err)%0A
+    console.log(result)%0A
   %7D)%0A%7D)%0A

Add Successful Logs for Scheduled Blocks of Codes