#!/usr/bin/env python
# -*- coding: utf-8 -*-
from gevent import monkey
monkey.patch_all()

from os import sys, path
import schedule
from time import sleep

from bottle.ext.mongo import MongoPlugin

sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
from mining.bin.cube import run
from mining.utils import conf, log_it


log_it("START", "bin-scheduler")
onrun = {}
register = []


def job(slug):
    log_it("START JOB: {}".format(slug), "bin-scheduler")
    run(slug)
    log_it("END JOB: {}".format(slug), "bin-scheduler")


def rules(cube, scheduler_type='minutes', scheduler_interval=59,
          dashboard=None):
    if scheduler_type:
        scheduler_type = cube.get('scheduler_type', 'minutes')
    if scheduler_interval:
        scheduler_interval = cube.get('scheduler_interval', 59)

    log_it("START REGISTER", "bin-scheduler")
    log_it("cube: {}".format(cube.get('slug')), "bin-scheduler")
    log_it("type: {}".format(scheduler_type), "bin-scheduler")
    log_it("interval: {}".format(scheduler_interval), "bin-scheduler")
    log_it("END REGISTER", "bin-scheduler")

    t = {}
    if scheduler_type == 'minutes':
        env = schedule.every(int(scheduler_interval))
        t = env.minutes
    elif scheduler_type == 'hour':
        env = schedule.every()
        t = env.hour
    elif scheduler_type == 'day':
        env = schedule.every()
        t = env.day

    try:
        t.do(job, slug=cube.get('slug'))
        jobn = cube.get("slug")
        if dashboard:
            jobn = u"{}-{}".format(cube.get("slug"), dashboard)
        onrun[jobn] = env
        register.append(jobn)
    except Exception, e:
        log_it("ERROR {}: {}".format(cube.get('slug'), e))

    return True


mongo = MongoPlugin(
    uri=conf("mongodb")["uri"],
    db=conf("mongodb")["db"],
    json_mongo=True).get_mongo()

for cube in mongo['cube'].find({'scheduler_status': True}):
    rules(cube)
    run(cube['slug'])

for dashboard in mongo['dashboard'].find({'scheduler_status': True}):
    elements = [e['id'] for e in dashboard['element']]
    for e in elements:
        element = mongo['element'].find_one({'slug': e})
        cube = mongo['cube'].find_one({'slug': element['cube']})
        rules(cube, dashboard['scheduler_type'],
              dashboard['scheduler_interval'])
        run(cube['slug'])

while True:
    for cube in mongo['cube'].find({'scheduler_status': True}):
        if cube['slug'] not in register:
            rules(cube)

    for dashboard in mongo['dashboard'].find({'scheduler_status': True}):
        elements = [e['id'] for e in dashboard['element']]
        for e in elements:
            element = mongo['element'].find_one({'slug': e})
            cube = mongo['cube'].find_one({'slug': element['cube']})
            if cube['slug'] not in register:
                rules(cube, dashboard['scheduler_type'],
                      dashboard['scheduler_interval'],
                      dashboard['slug'])

    for cube in mongo['cube'].find({'scheduler_status': False}):
        if cube['slug'] in register:
            schedule.cancel_job(onrun[cube['slug']])
            del onrun[cube['slug']]
            register.remove(cube['slug'])

    for dashboard in mongo['dashboard'].find({'scheduler_status': False}):
        elements = [e['id'] for e in dashboard['element']]
        for e in elements:
            element = mongo['element'].find_one({'slug': e})
            cube = mongo['cube'].find_one({'slug': element['cube']})
            jobn = u"{}-{}".format(cube['slug'], dashboard['slug'])
            if jobn in register:
                schedule.cancel_job(onrun[jobn])
                del onrun[jobn]
                register.remove(jobn)

    schedule.run_pending()
    sleep(1)

@@ -3479,32 +3479,53 @@
 ement%5B'cube'%5D%7D)%0A
+            try:%0A    
             jobn
@@ -3572,24 +3572,28 @@
 rd%5B'slug'%5D)%0A
+    
             
@@ -3621,32 +3621,36 @@
                 
+    
 schedule.cancel_
@@ -3674,32 +3674,36 @@
                 
+ 
+   
 del onrun%5Bjobn%5D%0A
@@ -3702,16 +3702,20 @@
 n%5Bjobn%5D%0A
+    
         
@@ -3743,16 +3743,57 @@
 ve(jobn)
+%0A            except:%0A                pass
 %0A%0A    sc

fix try on process scheduler load dashboard time