         @prepareNextCheck()
 
     prepareNextCheck: ->
-        # day after between 00:00am and 02:00am
+
+        if @timeout?
+            clearTimeout @timeout
+            @timeout = null
+
+        # day after between 02:00am and 04:00am
         delta = Math.random() * 120 | 0 # opa asm.js style
         now = moment()
         nextUpdate = now.clone().add(1, 'days')
-                            .hours(0)
+                            .hours(2)
                             .minutes(delta)
                             .seconds(0)
 

                return

            process = (access, callback) ->
                weboob.retrieveAccountsByBankAccess access, callback

            async.each accesses, process, (err) =>
                if err?
                    console.log "Error when fetching accounts: #{err}"
                    return

                process = (access, callback) ->
                    weboob.retrieveOperationsByBankAccess access, callback

                async.each accesses, process, (err) =>
                    if err?
                        console.log "Error when fetching operations: #{err}"
                        return
                    console.log "All accounts have been polled."
                    @prepareNextCheck()



module.exports = new AccountsPoller

async = require 'async'

BankAccess  = require "../models/bankaccess"
Config      = require "../models/kresusconfig"

weboob = require './weboob-manager'

