Exemple #1
0
def run_cron_cleanup(settings):
    '''
    Read cron scheduling entries and schedule
    '''
    cron_time = {}
    year = settings.get("extract.cleanup.schedule.cron.year")
    month = settings.get("extract.cleanup.schedule.cron.month")
    day = settings.get("extract.cleanup.schedule.cron.day")
    week = settings.get("extract.cleanup.schedule.cron.week")
    day_of_week = settings.get("extract.cleanup.schedule.cron.day_of_week")
    hour = settings.get("extract.cleanup.schedule.cron.hour")
    minute = settings.get("extract.cleanup.schedule.cron.minute")
    second = settings.get("extract.cleanup.schedule.cron.second")

    if year is not None:
        cron_time['year'] = year
    if month is not None:
        cron_time['month'] = month
    if day is not None:
        cron_time['day'] = day
    if week is not None:
        cron_time['week'] = week
    if day_of_week is not None:
        cron_time['day_of_week'] = day_of_week
    if hour is not None:
        cron_time['hour'] = hour
    if minute is not None:
        cron_time['minute'] = minute
    if second is not None:
        cron_time['second'] = second

    if len(cron_time) > 0:
        sched = Scheduler()
        sched.start()
        sched.add_cron_job(delete_stats, **cron_time)
Exemple #2
0
 def pre_eva_start(self, conf):
     # Load all jobs
     self.invoke('pre_scheduler_load_jobs')
     sched = APScheduler()
     conf['scheduler']['scheduler'] = sched
     for job_name in conf['scheduler']['jobs']:
         job = conf['scheduler']['jobs'][job_name]
         if job.get('type') == 'date':
             # datetime is a datetime string in this case
             # ie: '2012-11-06 14:25:10.8880'
             sched.add_date_job(job['func'], job['datetime'], args=[conf])
         elif job.get('type') == 'interval':
             sched.add_interval_job(job['func'],
                                    seconds=job['interval'].get('seconds', 0),
                                    minutes=job['interval'].get('minutes', 0),
                                    hours=job['interval'].get('hours', 0),
                                    days=job['interval'].get('days', 0),
                                    weeks=job['interval'].get('weeks', 0),
                                    start_date=job['interval'].get('start_date'),
                                    args=[conf])
         elif job.get('type') == 'cron':
             sched.add_cron_job(job['func'],
                                second=job['interval'].get('second'),
                                minute=job['interval'].get('minute'),
                                hour=job['interval'].get('hour'),
                                day=job['interval'].get('day'),
                                week=job['interval'].get('week'),
                                month=job['interval'].get('month'),
                                year=job['interval'].get('year'),
                                day_of_week=job['interval'].get('day_of_week'),
                                args=[conf])
     sched.start()
     self.invoke('post_scheduler_load_jobs')
Exemple #3
0
def callback():
	# Start the scheduler
	sched = Scheduler()
	sched.start()
	alarmMonth = userentermonth.get()
	print len(alarmMonth) #DEBUG
	print alarmMonth #DEBUG
	alarmDay = userenterday.get()
	print len(alarmDay) #DEBUG
	print alarmDay #DEBUG
	alarmYear = userenteryear.get()
	print len(alarmYear) #DEBUG
	print alarmYear #DEBUG
	alarmHour = userenterhour.get()
	print len(alarmHour) #DEBUG
	print alarmHour #DEBUG
	alarmMinute = userenterminute.get()
	print len(alarmMinute) #DEBUG
	print alarmMinute #DEBUG
# 	alarmMonth = 5
# 	alarmDay = 1
# 	alarmHour = 5
# 	alarmMinute = 30
	# add values in here to set lighting variables so that they remain static
	# Schedules job_function for specific date and time
	sched.add_cron_job(AlarmOn, year=alarmYear, month=alarmMonth, day=alarmDay, hour=alarmHour, minute=alarmMinute)
	lbl.configure(text="Alarm Set!")
Exemple #4
0
    def handle(self, *args, **options):
        sched = Scheduler()
        sched.start()

        from twilio.rest import TwilioRestClient
        account_sid = "AC6fe90756ae4096c5bf790984038a3f32"
        auth_token  = "97e8833ee3553bc4d9d16e86f1865d32"
        client = TwilioRestClient(account_sid, auth_token)

        for user in user_list:
            user_schedule = Schedule.objects.all().filter(user=user)
            for schedule in user_schedule:
                day_of_week = schedule.day_of_week
                hour = schedule.hour
                minute = schedule.minute
                user_message = schedule.message
                print 'BEFORE:' + str(user_message)
                
                def timed_job(msg):
                    print 'AFTER' + str(msg)
                sched.add_cron_job(lambda: timed_job(user_message), second='0-60')

        #sched.start()
        print 'test'
        while True:
            pass
class Wikipedia(Plugin):
    def __init__(self, skype):
        super(Wikipedia, self).__init__(skype)
        self.daily_channels = ["#stigrk85/$jvlomax;b43a0c90a2592b9b"]
        self.sched = Scheduler()
        self.sched.start()
        self.command = "wikipedia"
        self.sched.add_cron_job(self.dailyWikipedia, hour=18, minute=0, day_of_week="mon-sun")

    def message_received(self, args, status, msg):
        if (len(args) == 1 and args[0] == "random") or not args:
            url = self.fetch_randWiki()
            msg.Chat.SendMessage(url)
        else:
            try:
                page = wiki.wikipedia.page(" ".join(args))
                if page.url:
                    msg.Chat.SendMessage(urllib.unquote(page.url))
                else:
                    msg.Chat.SendMessage("Could not find any results for {}".format(" ".join(args)))
            except wiki.exceptions.DisambiguationError:
                msg.Chat.SendMessage("Your search is disambiguous")
            except wiki.exceptions.PageError:
                 msg.Chat.SendMessage("Could not find any results for {}".format(" ".join(args)))

    def fetch_randWiki(self):
        r = requests.get("http://en.wikipedia.org/wiki/Special:Random")
        return r.url

    def dailyWikipedia(self):
        for channel in self.daily_channels:
            chat = self.skype.Chat(channel)
            chat.SendMessage("Dagens random wikipedia: " + self.fetch_randWiki())
Exemple #6
0
def main(config_file):
    info = load_config(config_file)

    day_of_week = info['days']
    hour = info['hour']
    minute = info['minute']

    config = {
        'apscheduler.jobstores.file.class': info['scheduler-config']['class'],
        'apscheduler.jobstores.file.path': info['scheduler-config']['path']
    }
    sched = Scheduler(config)

    sched.add_cron_job(run_scraper,
                       day_of_week=day_of_week,
                       hour=hour,
                       minute=minute)
    sched.add_cron_job(check_archive, day='first')

    sched.start()

    print('Press Ctrl+{0} to exit'.format('C'))

    try:
        # This is here to simulate application activity (which keeps the main thread alive).
        while True:
            time.sleep(2)
    except (KeyboardInterrupt, SystemExit):
        sched.shutdown(
        )  # Not strictly necessary if daemonic mode is enabled but should be done if possibleisched.start()
Exemple #7
0
class SimpleScheduler:
    def __init__(self):
        self._sched = Scheduler()
        self._sched.start()
        self._jobs = {}

    def schedule(self, job):
        if job.name in self._jobs:
            logger.warn("Already have job with name: %s" % job.name)
            return False

        try:
            self._sched.add_cron_job(job._execute_and_store, **job.schedule)
        except TypeError:
            logger.error("Invalid schedule for job with name: %s" % job.name +
                         " schedule: %s" % job.schedule)
        self._jobs[job.name] = job
        return True

    def schedules(self):
        return {job.name: job.schedule for job in self._jobs.values()}

    def execute(self, name):
        return self._sched.add_date_job(self._jobs[name]._execute_and_store,
                                        datetime.now() + timedelta(seconds=1))
 def load(self):
     sched = Scheduler()
     sched.daemonic = False
     # Schedules job_function to be run on the third Friday
 # of June, July, August, November and December at 00:00, 01:00, 02:00 and 03:00
     sched.add_cron_job(self.job_function, second='*/3')
     sched.start()
Exemple #9
0
class Ddate():
    ''' Scheduling class to post ddate each morning. '''

    def __init__(self, connection, target):
        ''' Set irc details, initialise scheduler, set daily task,
        and grab the current date. '''
        self.con = connection
        self.target = target
        self.sched = Scheduler()
        self.sched.start()
        self.fetch_ddate()
        # scheduler for 0915 (server is gmt+1)
        self.sched.add_cron_job(self.change_ddate, hour=10, minute=15)

    def change_ddate(self):
        ''' update ddate and announce to channel. '''
        self.fetch_ddate()
        self.post_ddate()

    def fetch_ddate(self):
        ''' fetch ddate from shell.'''
        date = check_output(['ddate'])
        # trim `today is ` and `\n`
        self.ddate = date[9:-2]

    def post_ddate(self):
        ''' post date to connection/channel supplied on startup. '''
        self.con.send_msg(self.target, self.ddate)

    def __del__(self):
        ''' Kill scheduler on close. '''
        self.sched.shutdown()
Exemple #10
0
def main(standalone=True):
    
    env.configure("conf.ini")
    
    '''
    transfer History Detail for the first time
    when program is executed
    '''
    if env.config.get("transfer_hist_detail_on_loading") and env.config.get("transfer_hist_detail_on_loading") == "1":
        transferHistDetails(env.config.get("DB_FILE"), env.config.get("STOREID"), 
                         env.config.get("FTP_HOME"), env.config.get("FTP_HOST"),
                         env.config.get("FTP_PORT"), env.config.get("FTP_USERNAME"),
                         env.config.get("FTP_PASSWORD"))
     
    '''
    create the scheduler for two job:
    1. check the client's alive
    2. transfer details to server
    '''
    scheduler = Scheduler(standalone=standalone)
      
    scheduler.add_interval_job(sync, 
                               minutes=int(env.config.get("sync_interval")), 
                               args=(env.config.get("DB_FILE"), env.config.get("STOREID"),
                                     env.config.get("PROGNAME"), env.config.get("PROG"),
                                     env.config.get("FTP_HOME"), env.config.get("FTP_HOST"),env.config.get("FTP_PORT"), 
                                     env.config.get("FTP_USERNAME"),env.config.get("FTP_PASSWORD")))
       
    scheduler.add_cron_job(transferFluxDetailsByStatus, 
                           day_of_week=env.config.get("upload_day_of_week"), hour=env.config.get("upload_hour"), minute=env.config.get("upload_minute"), 
                           args=(env.config.get("DB_FILE"), env.config.get("STOREID"),
                                 env.config.get("FTP_HOME"), env.config.get("FTP_HOST"),env.config.get("FTP_PORT"), 
                                 env.config.get("FTP_USERNAME"),env.config.get("FTP_PASSWORD"), 
                                 0))
    scheduler.start()
Exemple #11
0
def main(config_file):
    info = load_config(config_file)

    day_of_week = info['days']
    hour = info['hour']
    minute = info['minute']

    config = {
        'apscheduler.jobstores.file.class': info['scheduler-config']['class'],
        'apscheduler.jobstores.file.path': info['scheduler-config']['path']
    }
    sched = Scheduler(config)

    sched.add_cron_job(run_scraper, day_of_week=day_of_week, hour=hour, minute=minute)
    sched.add_cron_job(check_archive, day='first')

    sched.start()

    print('Press Ctrl+{0} to exit'.format('C'))

    try:
        # This is here to simulate application activity (which keeps the main thread alive).
        while True:
            time.sleep(2)
    except (KeyboardInterrupt, SystemExit):
        sched.shutdown()  # Not strictly necessary if daemonic mode is enabled but should be done if possibleisched.start()
Exemple #12
0
class FreshPots(BotPlugin):

    pots = [
        'http://i.imgur.com/Q2b54vc.jpg',
        'http://i.imgur.com/SYsdsew.jpg',
        'http://i.imgur.com/caIbQMh.png',
        'http://i.imgur.com/MCwiikl.jpg',
        'http://i.imgur.com/g4sFHwz.jpg',
        'http://i.imgur.com/vnuJQ4S.gif',
        'http://i.imgur.com/cm3Y6jX.jpg',
        'http://i.imgur.com/ZcKZTFU.jpg',
        'http://i.imgur.com/4mEaNIp.jpg',
        'http://i.imgur.com/gDukRFu.png',
        'http://i.imgur.com/1MDO9fV.png',
        'http://i.imgur.com/U5cFX3M.jpg'
    ]

    def activate(self):
        super(FreshPots, self).activate()
        self.sched = Scheduler(coalesce=True)
        self.sched.start()
        self.sched.add_cron_job(
            self.fresh_pots,
            kwargs={'message': 'fresh pots time'},
            day_of_week='mon-fri',
            hour=11)
        self.sched.add_cron_job(
            self.fresh_pots,
            kwargs={'message': 'fresh pots time'},
            day_of_week='mon-fri',
            hour=15)
        logging.info(self.sched.get_jobs())

    def callback_message(self, conn, mess):
        body = mess.getBody().lower()
        if body.find('coffee') != -1 or body.find('fresh pots') != -1:
            self.fresh_pots(mess.getFrom())

    def fresh_pots(self, channel='#cloudant-bristol', message=None):
        if message:
            self.send(
                channel,
                message,
                message_type='groupchat'
            )

        self.send(
            channel,
            choice(self.pots),
            message_type='groupchat'
        )
        self.check()

    def check(self):
        for job in self.sched:
            delta = job.next_run_time - datetime.now()
            hour_delta = timedelta(seconds=3600)
            if delta < hour_delta:
                job.compute_next_run_time(datetime.now() + hour_delta)
def daemon():
    worker_minutes = cfg.get("job.switch_cfg_persistence", "run_on_minutes")
    logger.info("Starting persistence-control daemon...")
    logger.info("The persistence-control worker will run on minutes '%s'..." % worker_minutes)
    sched = Scheduler()
    sched.add_cron_job(worker, minute=worker_minutes)
    sched.start()
    signal.pause()
Exemple #14
0
def tasks(hour):
    """run tasks, keep it running for crawler"""
    sched = Scheduler(standalone=True)
    # http://pythonhosted.org/APScheduler/modules/scheduler.html
    sched.add_cron_job(douban_spider_task, hour=int(hour))
    sched.start()
    info = 'start tasks at' + datetime.datetime.now().strftime('%y-%m-%d %H:%M:%S')
    spider_task_log.log_info(info)
Exemple #15
0
class Main(Daemon):
    """
    do some things
    """
    def __init__(self, pidfile, cfgfile):
        Daemon.__init__(self, pidfile)
        self.jobs = {}
        self.immediately = False
        self.scheduler = Scheduler(daemonic=False)
        self.logger = logging.getLogger(self.__class__.__name__)
        if os.path.exists(cfgfile):
            with open(cfgfile, 'rt') as f:
                config = yaml.load(f.read())
            for k1 in config.keys():
                if k1 == 'version':
                    pass
                if k1 == 'immediately':
                    self.immediately = config[k1]
                elif k1 == 'taobao':
                    self.jobs[k1] = config[k1]
                    self.jobs[k1]['id'] = None
                    if 'chktime' in self.jobs[k1].keys():
                        self.jobs[k1]['btime'] = time.strptime(self.jobs[k1]['chktime'].split('-')[0], '%H:%M')
                        self.jobs[k1]['etime'] = time.strptime(self.jobs[k1]['chktime'].split('-')[1], '%H:%M')
                        if self.jobs[k1]['btime'] >= self.jobs[k1]['etime']:
                            raise ValueError('"chktime" is illegal')
                    else:
                        raise ValueError('There is no "chktime" be found in configure.')
                else:
                    pass
        else:
            self.logger.error('{0} not found'.format(cfgfile))

    def job_main(self):
        st_beg = self.jobs['taobao']['btime']
        st_end = self.jobs['taobao']['etime']
        dt_beg = datetime.now().replace(hour=st_beg.tm_hour, minute=st_beg.tm_min)
        dt_end = datetime.now().replace(hour=st_end.tm_hour, minute=st_end.tm_min)
        td_rnd = dt_end - dt_beg
        dt_rnd = dt_beg + timedelta(seconds=randint(1, td_rnd.days * 86400 + td_rnd.seconds - 1))
        if dt_rnd <= datetime.now():
            dt_rnd += timedelta(days=1)
        self.jobs['taobao']['id'] = self.scheduler.add_date_job(lambda: self.job_taobao(), dt_rnd)

    def job_taobao(self):
        for v in self.jobs['taobao']['account']:
            taobao = Taobao(v['username'], v['password'])
            if taobao.login():
                taobao.checkin()

    def run(self):
        if self.immediately:
            self.job_taobao()
            self.immediately = False
        self.scheduler.add_cron_job(lambda: self.job_main(), hour='0', minute='1')
        self.scheduler.start()
        stopevent.wait()
        self.scheduler.shutdown()
Exemple #16
0
	def handle(self, *args, **options):
		sched = Scheduler(daemonic=True)
		sched.add_cron_job(job_function,  minute='*')
		sched.configure()
		try:
		    sched.start()
		except (KeyboardInterrupt, SystemExit):
		    pass
		print sched.print_jobs()
Exemple #17
0
def main():
    fpid = os.fork()
    if fpid != 0:
        # Running as daemon now. PID is fpid
        sys.exit(0)
    sched = Scheduler()
    sched.add_cron_job(report, minute='*/10')
    sched.start()
    signal.pause()
Exemple #18
0
def daemon():
    worker_minutes = cfg.get("job.switch_cfg_persistence", "run_on_minutes")
    logger.info("Starting persistence-control daemon...")
    logger.info("The persistence-control worker will run on minutes '%s'..." %
                worker_minutes)
    sched = Scheduler()
    sched.add_cron_job(worker, minute=worker_minutes)
    sched.start()
    signal.pause()
Exemple #19
0
def start_schedules(config):
    sched = Scheduler()
    config.scheduler = sched
    sched.start()
    for repo in config.repositories:
        for branch in repo.branches:
            if branch.schedule:
                sched.add_cron_job(tester(config, repo, branch),
                                   **branch.schedule)
Exemple #20
0
def start_schedules(config) :
    sched = Scheduler()
    config.scheduler = sched
    sched.start()
    for repo in config.repositories :
        for branch in repo.branches :
            if branch.schedule :
                sched.add_cron_job(tester(config,repo,branch), 
                    **branch.schedule)
class EventStream(object):
    def __init__(self):
        self.clients = {}
        self.next_id = 1

        self.functions = []
        self._scheduler = Scheduler()
        self._scheduler.start()

    def register(self, func, **kwargs):
        self.functions.append(func)
        self._scheduler.add_cron_job(func, **kwargs)

    def post(self, widget_id, client_id=None, **kwargs):
        # Create message
        message = kwargs
        message.update({
            'id': widget_id,
            'updatedAt': int(time.time() + 300)
        })

        # Queue for clients to send
        clients = self.clients.items()
        if client_id:
            clients = [(client_id, self.clients[client_id])]

        for cid, client in clients:
            client['messages'][client['next_mid']] = message
            client['next_mid'] += 1

        Logr.debug("Message posted, queued to be sent to %s clients" % len(self.clients))

    def read(self):
        client_id = self.next_id
        self.next_id += 1
        self.clients[client_id] = {
            'next_mid': 1,
            'messages': {}
        }

        for func in self.functions:
            func(client_id=client_id)

        while client_id in self.clients:
            sent = []

            for mid, message in self.clients[client_id]['messages'].items():
                Logr.debug("Sent message: %s" % message)
                yield "data: %s\n\n" % json.dumps(message)
                sent.append(mid)

            # Remove sent messages
            for mid in sent:
                self.clients[client_id]['messages'].pop(mid)

            time.sleep(5)
Exemple #22
0
class Controller():
    def __init__(self):
        self.configList = []

        self.scheduler = Scheduler()
        self.scheduler.start()

        self.uploadConfigList()

        self.mqttClient = mqtt.Client(mqttClientName)
        self.mqttClient.on_connect = onConnect
        self.mqttClient.on_disconnect = onDisconnect
        self.mqttClient.on_log = onLog
        self.mqttClient.on_publish = onPublish
        self.mqttClient.on_message = onMessage

        self.mqttClient.connect(mqttBrokerHost)
        self.mqttClient.loop_start()

    def stop(self):
        self.scheduler.shutdown()

        self.mqttClient.loop_stop()
        self.mqttClient.disconnect()

    def uploadConfigList(self):
        tableRows = None
        try:
            tableRows = getTableRows(scheduleTable)
        except sqlite3.Error as err:
            logger.error('SQLite: error: ' + str(err))

        self.configList.clear()
        for row in tableRows:
            config = Config(topic=row[0],
                            weekday=row[1],
                            hour=row[2],
                            minute=row[3],
                            second=row[4])
            logger.info('Uploaded config list with: ' + str(config.topic) +
                        ' ' + str(config.weekday) + ' ' + str(config.hour) +
                        ' ' + str(config.minute) + ' ' + str(config.second))
            self.configList.append(config)
        self.updateScheduler()

    def updateScheduler(self):
        for config in self.configList:
            self.scheduler.add_cron_job(func=self.requestUpdate,
                                        args=[config.topic],
                                        day_of_week=config.weekday,
                                        hour=config.hour,
                                        minute=config.minute,
                                        second=config.second)

    def requestUpdate(self, mqttTopic):
        self.mqttClient.publish(mqttTopic, mqttUpdateTrigger, int(mqttQos))
Exemple #23
0
    def run(self):
        print "Start scheduler @ {dt}".format(dt=datetime.utcnow())

        # Start the scheduler
        sched = Scheduler(daemonic=False)
        sched.start()
        
        #sched.add_cron_job(self.job_function, second="*/2", max_instances=6)
        seconds = 5
        sched.add_cron_job(self.job_function, second="*/%d" % seconds, max_instances=6)
Exemple #24
0
def tasks(hour):
    '''run tasks, keep it running for spider'''
    import logging
    logging.basicConfig()
    sched = Scheduler(standalone=True)
    # http://pythonhosted.org/APScheduler/modules/scheduler.html
    sched.add_cron_job(douban_spider_task, hour=int(hour))
    # sched.add_interval_job(douban_spider_task, seconds=10)
    print 'start tasks at', datetime.datetime.now()
    sched.start()
Exemple #25
0
def tasks(hour):
    '''run tasks, keep it running for spider'''
    import logging
    logging.basicConfig()
    sched = Scheduler(standalone=True)
    # http://pythonhosted.org/APScheduler/modules/scheduler.html
    sched.add_cron_job(douban_spider_task, hour=int(hour))
    # sched.add_interval_job(douban_spider_task, seconds=10)
    print 'start tasks at', datetime.datetime.now()
    sched.start()
Exemple #26
0
 def handle(self, *args, **options):
     scheduler = Scheduler(daemonic=False)
     logger.info("started the scheduler")
     scheduler.add_cron_job(send_reminders,
                            hour=settings.SCHEDULER_HOUR,
                            minute=settings.SCHEDULER_MINUTES)
     scheduler.add_cron_job(send_deactivate_email,
                            hour=settings.SCHEDULER_HOUR,
                            minute=settings.SCHEDULER_MINUTES)
     scheduler.start()
Exemple #27
0
class ScheduleThread(threading.Thread):
    def __init__(self):
        super(ScheduleThread, self).__init__()
        self.sched = Scheduler()

    def run(self):
        self.sched.start()

    def addschedule(self, **d):
        self.sched.add_cron_job(**d)
Exemple #28
0
    def checkCron(self):
        def fun():
            pass

        sche = Scheduler()
        try:
            sche.add_cron_job(fun, **self.toDictByDateTime())
            return True
        except:
            return False
        finally:
            del sche
Exemple #29
0
def main():
    fpid = os.fork()
    if fpid != 0:
        # Running as daemon now. PID is fpid
        sys.exit(0)
    parse_snmp()
    parse()
    sched = Scheduler()
    sched.add_cron_job(parse, minute='*/2')
    sched.add_cron_job(parse_snmp, minute='*/1')
    sched.start()
    signal.pause()
Exemple #30
0
class evExecuter:
    def __init__(self):
        self.executer = Scheduler()
        self.executer.start()

    def addEvt(self, func, hr):
        self.executer.add_cron_job(func,
                                   year='*',
                                   month='*',
                                   day='*',
                                   hour='*',
                                   minute='*',
                                   second='0')
Exemple #31
0
def run():
    tasks = get_tasks()
    scheduler = Scheduler(standalone=True)
    print "执行任务:"
    for k, v in tasks.iteritems():
        t = v()
        print "%s: %s" % (t.desc, t.cron)
        scheduler.add_cron_job(t, **t.cron)

    try:
        scheduler.start()
    except (KeyboardInterrupt, SystemExit):
        print "exit ..."
def download_user_info_scheduler():
    """
    Create and prepare scheduler for downloading xml data from url.
    """
    scheduler = Scheduler()
    scheduler.start()

    # backup Schedule to run once every 4 hours
    day_of_week = app.config.get('cron_day_of_week_pattern', '*')
    hour = app.config.get('cron_hour_pattern', '*/4')
    minute = app.config.get('cron_minutes_pattern', '0')
    scheduler.add_cron_job(download_users_information, day_of_week=day_of_week,
                           hour=hour, minute=minute)
def daemon():
    try:
        from setproctitle import setproctitle
    except ImportError:
        pass
    else:
        setproctitle("netl2api [netl2server:http-daemon/job/switch-cfg-persistence]")
    logger.info("Starting persistence-control daemon...")
    logger.info("The persistence-control worker will run on minutes '%s'..." % worker_minutes)
    sched = Scheduler()
    sched.add_cron_job(worker, minute=worker_minutes)
    sched.start()
    signal.pause()
Exemple #34
0
class TimeScheduler:

    instance = None
    
    def __init__(self):
        '''
        '''
    
    @staticmethod
    def getInstance():
        if TimeScheduler.instance is None:
            TimeScheduler.instance = TimeScheduler()
        return TimeScheduler.instance
    
    def init(self,threadpool = None):
        if threadpool is None :
            self.sched = Scheduler({'apscheduler.threadpool.core_threads':1,
                                    'apscheduler.threadpool.max_threads':1,
                                    'apscheduler.threadpool.keepalive':1})  
        else:
            self.sched = Scheduler({'apscheduler.threadpool':threadpool})  
        self.sched.daemonic = False 
    
    def registerCronExp(self,handler,year=None, month=None, day=None, hour=None, minute=None, second=None,
                     start_date=None):
        return self.sched.add_cron_job(handler.execute,year, month, day, None,None, 
                                       hour, minute, second,None)
        
    def registerCron(self, handler ,year=None, month=None, day=None, week=None,
                     day_of_week=None, hour=None, minute=None, second=None,
                     start_date=None):
        return self.sched.add_cron_job(handler.execute,year=None, month=None, day=None, week=None,
                     day_of_week=None, hour=None, minute=None, second=None,
                     start_date=None)

    '''
        register interval task
    '''
    def registerInterval(self, handler,weeks=0, days=0, hours=0, minutes=0,
                         seconds=0, start_date=None):
        
        return self.sched.add_interval_job(handler.execute,weeks,days,hours, minutes,
                        seconds,start_date)  
    def registerDate(self, handler,date):
        return self.sched.add_date_job(handler.execute,date)  

    def unregister(self,job):
        self.sched.unschedule_job(job)
        
    def start(self):
        self.sched.start() 
def init_sched():
    global g_sched
    global g_old_appdata 
    g_old_appdata = []

    config = {'apscheduler.threadpool.core_threads':1,'apscheduler.threadpool.max_threads':1}
    g_sched = Scheduler(gconfig=config)
    g_sched.add_listener(listener_missed_job, apscheduler.events.EVENT_JOB_MISSED)

    g_sched.start()

    update_jobs()
    g_sched.add_cron_job(update_jobs, name='update_jobs', 
            minute='*', hour='*', day='*', month='*', day_of_week='*', year='*')
Exemple #36
0
def createTask( listTask ):
    """
    创建并开始文件提取任务
    """

    for dic in listTask:
        pfID = str(dic["pfid"])
        groupID = str(dic["groupid"])
        configPath = dic["configpath"]
        logSource = dic["logsource"]
        Protocol = dic["protocol"]
        Port = dic["port"]
        userName = dic["username"]
        userPass = dic["userpass"]
        fPath = dic["fpath"]
        Files = dic["files"]
        oneTime = dic["onetime"]
        schedStart = dic["schedstart"]
        schedEnd = dic["schedend"]
        schedTime = dic["schedtime"]
        schedCron = dic["schedcron"]

        argus = [ int(pfID), int(groupID), configPath, logSource, Protocol, Port, userName, userPass, fPath, Files ]
        
        sched = Scheduler()
        if oneTime.upper() == 'Y':  # 只执行一次
            if schedStart == None:
                schedStart = datetime.datetime.now() + datetime.timedelta( seconds = 2 )  # 延时两秒
            sched.add_date_job( taskFunc, schedStart, name='Job'+pfID, args=argus )
        elif schedTime != None:
            ( sWeeks, sDays, sHours, sMinutes, sSeconds ) = scheduletime.fmtSchedTime( schedTime )
            if schedStart == None:    # interval_job 在start_date为None时默认从当前算起,过一个设定的时间间隔第一次执行任务
                schedStart = datetime.datetime.now() + datetime.timedelta( seconds = 2 ) - datetime.timedelta( seconds = sSeconds, \
                        minutes = sMinutes, hours = sHours, days = sDays, weeks = sWeeks )
            sched.add_interval_job( taskFunc, weeks=sWeeks, days=sDays, hours=sHours, minutes=sMinutes, seconds=sSeconds, \
                    start_date=schedStart, name='Job'+pfID, args=argus )
        elif schedCron != None:
            ( cSecond, cMinute, cHour, cDay, cDayofWeek, cWeek, cMonth, cYear ) = scheduletime.frmSchedCron( schedCron )
            sched.add_cron_job( taskFunc, year=cYear, month=cMonth, week=cWeek, day_of_week=cDayofWeek, day=cDay, \
                    hour=cHour, minute=cMinute, second=cSecond, start_date=schedStart, name='Job'+pfID, args=argus )
        sched.add_listener( taskListener, events.EVENT_JOB_EXECUTED | events.EVENT_JOB_ERROR )

        # 保存计划任务的截止时间
        dicJob['T'+pfID] = oneTime.upper()
        dicJob['Job'+pfID] = schedEnd

        dicTask['Task'+pfID] = sched
        dicTask['Task'+pfID].start()
Exemple #37
0
class Scheduler(Plugin):

    crons = {}
    intervals = {}
    started = False

    def __init__(self):

        addEvent("schedule.cron", self.cron)
        addEvent("schedule.interval", self.interval)
        addEvent("schedule.remove", self.remove)

        self.sched = Sched(misfire_grace_time=60)
        self.sched.start()
        self.started = True

    def remove(self, identifier):
        for cron_type in ["intervals", "crons"]:
            try:
                self.sched.unschedule_job(getattr(self, cron_type)[identifier]["job"])
                log.debug("%s unscheduled %s", (cron_type.capitalize(), identifier))
            except:
                pass

    def doShutdown(self):
        self.stop()
        return super(Scheduler, self).doShutdown()

    def stop(self):
        if self.started:
            log.debug("Stopping scheduler")
            self.sched.shutdown()
            log.debug("Scheduler stopped")
        self.started = False

    def cron(self, identifier="", handle=None, day="*", hour="*", minute="*"):
        log.info('Scheduling "%s", cron: day = %s, hour = %s, minute = %s', (identifier, day, hour, minute))

        self.remove(identifier)
        self.crons[identifier] = {
            "handle": handle,
            "day": day,
            "hour": hour,
            "minute": minute,
            "job": self.sched.add_cron_job(handle, day=day, hour=hour, minute=minute),
        }

    def interval(self, identifier="", handle=None, hours=0, minutes=0, seconds=0):
        log.info(
            "Scheduling %s, interval: hours = %s, minutes = %s, seconds = %s", (identifier, hours, minutes, seconds)
        )

        self.remove(identifier)
        self.intervals[identifier] = {
            "handle": handle,
            "hours": hours,
            "minutes": minutes,
            "seconds": seconds,
            "job": self.sched.add_interval_job(handle, hours=hours, minutes=minutes, seconds=seconds),
        }
Exemple #38
0
def start( config={} ):

	# params init
	mongo_host = config.get('host', SETTING['host'])
	mongo_port = config.get('port', SETTING['port'])

	db = pymongo.Connection(mongo_host, mongo_port)  
	store = MongoDBJobStore(connection=db) 

	# create schedudler and run
	scheduler = Scheduler(daemonic=False)
	scheduler.start()
	scheduler.add_jobstore(store, 'mongo') 

	# add cron jobs
	scheduler.add_cron_job(monitor_cron_job, hour='0-23', minute="0", second="0", jobstore='mongo')
def createTask(listTask):
    """
    创建并开始计划任务
    """

    for dic in listTask:
        taskID = str(dic["schedid"])
        searchCond = dic["searchcond"].replace(DELIMITER, " ")
        searchStart = dic["searchstart"]
        searchEnd = dic["searchend"]
        schedStart = dic["schedstart"]
        schedEnd = dic["schedend"]
        schedTime = dic["schedtime"]
        schedCron = dic["schedcron"]
        warnOrNot = dic["warnornot"]
        warnCondOp = dic["warncondop"]
        warnCondVal = dic["warncondval"]
        warnLevel = dic["warnlevel"]
        saveResult = dic["saveresult"]

        argus = [
            int(taskID), searchCond, searchStart, searchEnd, warnOrNot,
            warnCondOp, warnCondVal, warnLevel, saveResult
        ]

        sched = Scheduler()
        if schedTime != None:
            (sWeeks, sDays, sHours, sMinutes,
             sSeconds) = scheduletime.fmtSchedTime(schedTime)
            if schedStart == None:  # interval_job 在start_date为None时默认从当前算起,过一个设定的时间间隔第一次执行任务
                schedStart = datetime.datetime.now() + datetime.timedelta( seconds = 2 ) - datetime.timedelta( seconds = sSeconds, \
                        minutes = sMinutes, hours = sHours, days = sDays, weeks = sWeeks )
            sched.add_interval_job( taskFunc, weeks=sWeeks, days=sDays, hours=sHours, minutes=sMinutes, seconds=sSeconds, \
                    start_date=schedStart, name='Job'+taskID, args=argus )
        elif schedCron != None:
            (cSecond, cMinute, cHour, cDay, cDayofWeek, cWeek, cMonth,
             cYear) = scheduletime.frmSchedCron(schedCron)
            sched.add_cron_job( taskFunc, year=cYear, month=cMonth, week=cWeek, day_of_week=cDayofWeek, day=cDay, \
                    hour=cHour, minute=cMinute, second=cSecond, start_date=schedStart, name='Job'+taskID, args=argus )
        sched.add_listener(taskListener,
                           events.EVENT_JOB_EXECUTED | events.EVENT_JOB_ERROR)

        # 保存计划任务的截止时间
        dicJob['Job' + taskID] = schedEnd

        dicTask['Task' + taskID] = sched
        dicTask['Task' + taskID].start()
Exemple #40
0
class Scheduler(Plugin):

    crons = {}
    intervals = {}
    started = False

    def __init__(self):

        addEvent('schedule.cron', self.cron)
        addEvent('schedule.interval', self.interval)
        addEvent('schedule.start', self.start)
        addEvent('schedule.restart', self.start)

        addEvent('app.load', self.start)
        addEvent('app.shutdown', self.stop)

        self.sched = Sched(misfire_grace_time=60)

    def remove(self, identifier):
        for type in ['interval', 'cron']:
            try:
                self.sched.unschedule_job(
                    getattr(self, type)[identifier]['job'])
                log.debug('%s unscheduled %s' %
                          (type.capitalize(), identifier))
            except:
                pass

    def start(self):

        # Stop all running
        self.stop()

        # Crons
        for identifier in self.crons:
            try:
                self.remove(identifier)
                cron = self.crons[identifier]
                job = self.sched.add_cron_job(cron['handle'],
                                              day=cron['day'],
                                              hour=cron['hour'],
                                              minute=cron['minute'])
                cron['job'] = job
            except ValueError, e:
                log.error("Failed adding cronjob: %s" % e)

        # Intervals
        for identifier in self.intervals:
            try:
                self.remove(identifier)
                interval = self.intervals[identifier]
                job = self.sched.add_interval_job(interval['handle'],
                                                  hours=interval['hours'],
                                                  minutes=interval['minutes'],
                                                  seconds=interval['seconds'],
                                                  repeat=interval['repeat'])
                interval['job'] = job
            except ValueError, e:
                log.error("Failed adding interval cronjob: %s" % e)
Exemple #41
0
class MetaDataGenerationScheduler():
    def __init__(self, updateIntervalSeconds=30):
        self.interval = updateIntervalSeconds
        config = {'apscheduler.daemonic': False}
        self.sched = Scheduler(config)
        # initialize these per instance.
        self.repo_timestamps = {}
        self.jobs = {}


    repo_timestamps = {}  #dictionary with jobName (=reponame) : last scheduler modification timestamp (float)
    jobs = {} #dictionary with jobName (=reponame) : jobHandle

    configService = RepoConfigService()
    static_root_dir = configService.getStaticRepoDir()
    sched = None
    interval = None

    def start(self):
        self.update_program_config() #read configs, schedule jobs

        # schedule an update as a job
        self.sched.add_interval_job(self.update_program_config, seconds=self.interval)
        
        # schedule cleanup cache
        self.sched.add_cron_job(self.cleanupCacheDir, hour = 23, minute = 17, second = 20)

        self.sched.start()
        
    def createrepo_with_optional_cleanup_job(self, *argList):
        monitor = JobMonitorer()
        monitor.job_starts()
        repoDir = argList[0]
        reponame = argList[1]
        rpm_max_keep = argList[2]
        didCleanUp=False
        try:
            if rpm_max_keep != None:
                didCleanUp=True
                self.configService.doCleanup(repoDir, rpm_max_keep)
                logging.info("job RpmCleanup on "+reponame+" took "+str(monitor.get_execution_time_until_now_seconds())+" seconds")
            self.configService.doCreateRepo(repoDir, reponame)
            monitor.job_finishes()
            logging.info(monitor.get_pretty_job_summary("createrepo on "+reponame+" (cleanup included : "+str(didCleanUp)+")"))
        except Exception, ex:
            logging.error(traceback.format_exc())
Exemple #42
0
class MetaDataGenerationScheduler():
    def __init__(self, updateIntervalSeconds=30):
        self.interval = updateIntervalSeconds
        config = {'apscheduler.daemonic': False}
        self.sched = Scheduler(config)
        # initialize these per instance.
        self.repo_timestamps = {}
        self.jobs = {}


    repo_timestamps = {}  #dictionary with jobName (=reponame) : last scheduler modification timestamp (float)
    jobs = {} #dictionary with jobName (=reponame) : jobHandle

    configService = RepoConfigService()
    static_root_dir = configService.getStaticRepoDir()
    sched = None
    interval = None

    def start(self):
        self.update_program_config() #read configs, schedule jobs

        # schedule an update as a job
        self.sched.add_interval_job(self.update_program_config, seconds=self.interval)
        
        # schedule cleanup cache
        self.sched.add_cron_job(self.cleanupCacheDir, hour = 23, minute = 17, second = 20)

        self.sched.start()
        
    def createrepo_with_optional_cleanup_job(self, *argList):
        monitor = JobMonitorer()
        monitor.job_starts()
        repoDir = argList[0]
        reponame = argList[1]
        rpm_max_keep = argList[2]
        didCleanUp=False
        try:
            if rpm_max_keep != None:
                didCleanUp=True
                self.configService.doCleanup(reponame, repoDir, rpm_max_keep)
                logging.info("job RpmCleanup on "+reponame+" took "+str(monitor.get_execution_time_until_now_seconds())+" seconds")
            self.configService.doCreateRepo(repoDir, reponame)
            monitor.job_finishes()
            logging.info(monitor.get_pretty_job_summary("createrepo on "+reponame+" (cleanup included : "+str(didCleanUp)+")"))
        except Exception, ex:
            logging.error(traceback.format_exc())
class Scheduler(Plugin):

    crons = {}
    intervals = {}
    started = False

    def __init__(self):

        addEvent('schedule.cron', self.cron)
        addEvent('schedule.interval', self.interval)
        addEvent('schedule.remove', self.remove)

        self.sched = Sched(misfire_grace_time = 60)
        self.sched.start()
        self.started = True

    def remove(self, identifier):
        for cron_type in ['intervals', 'crons']:
            try:
                self.sched.unschedule_job(getattr(self, cron_type)[identifier]['job'])
                log.debug('%s unscheduled %s', (cron_type.capitalize(), identifier))
            except:
                pass

    def doShutdown(self):
        super(Scheduler, self).doShutdown()
        self.stop()

    def stop(self):
        if self.started:
            log.debug('Stopping scheduler')
            self.sched.shutdown()
            log.debug('Scheduler stopped')
        self.started = False

    def cron(self, identifier = '', handle = None, day = '*', hour = '*', minute = '*'):
        log.info('Scheduling "%s", cron: day = %s, hour = %s, minute = %s', (identifier, day, hour, minute))

        self.remove(identifier)
        self.crons[identifier] = {
            'handle': handle,
            'day': day,
            'hour': hour,
            'minute': minute,
            'job': self.sched.add_cron_job(handle, day = day, hour = hour, minute = minute)
        }

    def interval(self, identifier = '', handle = None, hours = 0, minutes = 0, seconds = 0):
        log.info('Scheduling %s, interval: hours = %s, minutes = %s, seconds = %s', (identifier, hours, minutes, seconds))

        self.remove(identifier)
        self.intervals[identifier] = {
            'handle': handle,
            'hours': hours,
            'minutes': minutes,
            'seconds': seconds,
            'job': self.sched.add_interval_job(handle, hours = hours, minutes = minutes, seconds = seconds)
        }
Exemple #44
0
class LuxLogger():
    def __init__(self, cron_seconds, output, serialport):
        self.sched = Scheduler()
        self.sched.start()
        self.log_queue = Queue()
        self.incoming = SerialPort(serialport, self.log_queue, TIMEOUT)
        self.parser = Parser()
        self.saver = Persistence(output, self.log_queue, self.parser)
        self.saver.start()
        self.cron_seconds = cron_seconds

    def go(self):
        try:
            logging.debug("starting the cron based scheduler")
            self.sched.add_cron_job(self.incoming.getData,
                                    second=self.cron_seconds)
        except Exception, err:
            logging.error(err)
Exemple #45
0
def run_scheduler(jobs):

    # Set up scheduler
    sched = Scheduler()

    for cron_spec, argv in jobs:

        # Convert cron style spec inot apscheduler spec
        spec = parse_cron_spec(cron_spec)
        method = process_wrapper(argv)

        sched.add_cron_job(method, **spec)

    # Run scheduler
    sched.start()

    # Infinatly sleep
    while (True):
        time.sleep(15.0)
Exemple #46
0
 def handle(self, *args, **options):
     scheduler = Scheduler(daemonic=False)
     logger.info("started the scheduler")
     scheduler.add_cron_job(send_reminders,
                            hour=settings.SCHEDULER_HOUR,
                            minute=settings.SCHEDULER_MINUTES)
     scheduler.add_cron_job(send_time_based_reminder_email,
                            hour=settings.SCHEDULER_HOUR,
                            minute=settings.SCHEDULER_MINUTES)
     scheduler.add_cron_job(deactivate_poll_questionnaire,
                            hour=settings.SCHEDULER_HOUR,
                            minute=settings.SCHEDULER_MINUTES)
     scheduler.add_cron_job(deactivate_expired_trial_account,
                            hour=settings.SCHEDULER_HOUR,
                            minute=settings.SCHEDULER_MINUTES + 1)
     scheduler.add_cron_job(update_all_views,
                            hour=settings.SCHEDULER_HOUR,
                            minute=settings.SCHEDULER_MINUTES)
     scheduler.start()
Exemple #47
0
def start(config={}):

    # params init
    mongo_host = config.get('host', SETTING['host'])
    mongo_port = config.get('port', SETTING['port'])

    db = pymongo.Connection(mongo_host, mongo_port)
    store = MongoDBJobStore(connection=db)

    # create schedudler and run
    scheduler = Scheduler(daemonic=False)
    scheduler.start()
    scheduler.add_jobstore(store, 'mongo')

    # add cron jobs
    scheduler.add_cron_job(monitor_cron_job,
                           hour='0-23',
                           minute="0",
                           second="0",
                           jobstore='mongo')
def run_cron_job(settings, prefix, job):
    '''
    Runs a cron job

    :param dict settings:  configuration for the application
    :param string prefix:  the prefix to prepend to properties
    :param job: reference to the function to run as a cron job
    '''
    enabled = to_bool(settings.get(prefix + "enable", 'False'))
    if enabled:
        new_prefix = prefix + 'schedule.cron.'
        cron_time = {}
        year = settings.get(new_prefix + "year")
        month = settings.get(new_prefix + "month")
        day = settings.get(new_prefix + "day")
        week = settings.get(new_prefix + "week")
        day_of_week = settings.get(new_prefix + "day_of_week")
        hour = settings.get(new_prefix + "hour")
        minute = settings.get(new_prefix + "minute")
        second = settings.get(new_prefix + "second")

        if year is not None:
            cron_time['year'] = year
        if month is not None:
            cron_time['month'] = month
        if day is not None:
            cron_time['day'] = day
        if week is not None:
            cron_time['week'] = week
        if day_of_week is not None:
            cron_time['day_of_week'] = day_of_week
        if hour is not None:
            cron_time['hour'] = hour
        if minute is not None:
            cron_time['minute'] = minute
        if second is not None:
            cron_time['second'] = second
        if len(cron_time) > 0:
            sched = Scheduler()
            sched.start()
            sched.add_cron_job(job, args=[settings], **cron_time)
Exemple #49
0
class ActivityScheduler(baseRunner.BaseRunner):
    def __init__(self):
        super(ActivityScheduler, self).__init__()

        # set logging options as defined in config file
        logConf = self.config._sections["logging"]
        # remove default __name__ item
        del logConf["__name__"]
        logConf["level"] = int(logConf["level"])
        logConf["filename"] = join(dirname(__file__), logConf["filename"])
        logging.basicConfig(**logConf)

        # initialize scheduler
        self.scheduler = Scheduler()
        self.scheduler.start()

        # create initial schedule
        if not self.scheduler.get_jobs():
            self.createSchedule()

        # main loop
        while True:
            try:
                time.sleep(10)
            except KeyboardInterrupt:
                logging.info("Shutting down..")
                self.scheduler.shutdown()
                break

    def createSchedule(self):
        logging.info("Schedule requests..")

        schedules = self.config._sections["schedule"]
        # remove default __name__ item
        del schedules["__name__"]
        for methodName, schedule in schedules.items():
            # schedule handler requests (wrapper method gets called with
            # cron-like notation and the method name)
            # name parameter is given for logging/debugging purposes only
            self.scheduler.add_cron_job(self.wrap, *schedule.split(), \
             args=[methodName], misfire_grace_time=120, name=methodName)
Exemple #50
0
def entry():
    parser = argparse.ArgumentParser(prog='simplecoin rpc client scheduler')
    parser.add_argument('-l',
                        '--log-level',
                        choices=['DEBUG', 'INFO', 'WARN', 'ERROR'],
                        default='INFO')
    parser.add_argument('-cl', '--config-location', default='/config.yml')
    args = parser.parse_args()

    # Setup logging
    root = logging.getLogger()
    hdlr = logging.StreamHandler()
    formatter = logging.Formatter(
        '%(asctime)s [%(name)s] [%(levelname)s] %(message)s')
    hdlr.setFormatter(formatter)
    root.addHandler(hdlr)
    root.setLevel(getattr(logging, args.log_level))

    # Setup yaml configs
    # =========================================================================
    cfg = yaml.load(open(os_root + args.config_location))

    # Setup our CoinRPCs + SCRPCClients
    coin_rpc = {}
    sc_rpc = {}
    for curr_cfg in cfg['currencies']:

        if not curr_cfg['enabled']:
            continue

        cc = curr_cfg['currency_code']
        coin_rpc[cc] = CoinRPC(curr_cfg, logger=logger)

        curr_cfg.update(cfg['sc_rpc_client'])
        sc_rpc[cc] = SCRPCClient(curr_cfg, coin_rpc[cc], logger=logger)

    pm = PayoutManager(logger, sc_rpc, coin_rpc)

    sched = Scheduler(standalone=True)
    logger.info("=" * 80)
    logger.info("SimpleCoin cron scheduler starting up...")
    setproctitle.setproctitle("simplecoin_scheduler")

    # All these tasks actually change the database, and shouldn't
    # be run by the staging server
    sched.add_cron_job(pm.pull_payouts, minute='*/1')
    sched.add_cron_job(pm.send_payout, hour='23')
    sched.add_cron_job(pm.associate_all_payouts, hour='0')
    sched.add_cron_job(pm.confirm_payouts, hour='1')

    sched.start()
Exemple #51
0
def run(start_type = None):
    global postdata

    conf = sys_config.SysConfig(configFile)
    # 进程号文件名
    pidFile = conf.getConfig("datastats", "pidFile")

    if start_type == None:
        if os.path.exists(pidFile):
            os.remove(pidFile)
        pFile = open(pidFile, "w")
        pFile.write(str(os.getpid()))
        pFile.close()

    # 生成postdata对象
    postdata = postgrestats.PostgresData(configFile)
    argus = [ postdata ]

    sched = Scheduler(standalone = True)

    sched.add_cron_job(funcNoon, year=cron_noon[0], month=cron_noon[1], \
            week=cron_noon[2], day_of_week=cron_noon[3], day=cron_noon[4], \
            hour=cron_noon[5], minute=cron_noon[6], second=cron_noon[7], args=argus)
    sched.add_cron_job(funcHour, year=cron_hour[0], month=cron_hour[1], \
            week=cron_hour[2], day_of_week=cron_hour[3], day=cron_hour[4], \
            hour=cron_hour[5], minute=cron_hour[6], second=cron_hour[7], args=argus)
    sched.add_cron_job(funcMidnight, year=cron_midnight[0], month=cron_midnight[1], \
            week=cron_midnight[2], day_of_week=cron_midnight[3], day=cron_midnight[4], \
            hour=cron_midnight[5], minute=cron_midnight[6], second=cron_midnight[7], args=argus)
    # 自定义dashboard统计服务
    sched.add_cron_job(stats_dashboard.stats_dashboard, year=cron_dashboard[0], month=cron_dashboard[1], \
            week=cron_dashboard[2], day_of_week=cron_dashboard[3], day=cron_dashboard[4], \
            hour=cron_dashboard[5], minute=cron_dashboard[6], second=cron_dashboard[7], args=argus)

    # 每隔几分钟(默认5分钟)检查是否需要删除原始pcap文件
    interval_chkdevice = conf.getConfig("datastats", "intervalCheckDevice")
    if interval_chkdevice == None:
        interval_chkdevice = 5
    else:
        interval_chkdevice = int(interval_chkdevice)

    sched.add_interval_job(chkdevice.checkDevice, weeks=0, days=0, hours=0, minutes=interval_chkdevice, seconds=0, args=argus)

    try:
        sched.start()
    except (KeyboardInterrupt, SystemExit):
        pass
Exemple #52
0
def init_sched():
    global g_sched
    global g_old_appdata
    g_old_appdata = []

    config = {
        'apscheduler.threadpool.core_threads': 1,
        'apscheduler.threadpool.max_threads': 1
    }
    g_sched = Scheduler(gconfig=config)
    g_sched.add_listener(listener_missed_job,
                         apscheduler.events.EVENT_JOB_MISSED)

    g_sched.start()

    update_jobs()
    g_sched.add_cron_job(update_jobs,
                         name='update_jobs',
                         minute='*',
                         hour='*',
                         day='*',
                         month='*',
                         day_of_week='*',
                         year='*')
def main():
    sched = Scheduler()
    #sched = BlockingScheduler()
    sched.start()
    sched.add_cron_job(bookTKB, hour=3, minute=50)
    sched.add_cron_job(bookTKB, hour=15, minute=50)

    sched.add_cron_job(bookTKB, hour=8, minute=57)
    #sched.add_cron_job(bookTKB, hour=16, minute=50)
    #sched.add_job(bookTKB, 'cron', hour=3, minute=55)
    #sched.add_job(bookTKB, 'cron', hour=15, minute=55)
    #sched.add_job(bookTKB, 'cron', hour=8, minute=12)
    logging.warning('running...')

    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)
Exemple #54
0
class bakCron(object):
    def __init__(self):
        self.sched = Scheduler()
        self.sched.daemonic = False
        self.sched.start()

        self.assign_jobs()
        self.assign_monitor()

    def get_fileconfig(self):
        ''' 
		获取配置文件的路径,此路径在软件安装时指定目录。
		'''
        policyfile = os.path.dirname(nsccdbbak.__file__) + "/conf/Policy.conf"
        serverfile = os.path.dirname(nsccdbbak.__file__) + "/conf/Server.conf"

        policys = []
        PolicyConfig = ConfigParser.ConfigParser(allow_no_value=True)
        PolicyConfig.read(policyfile)

        for section in PolicyConfig.sections():
            dictTmp = {}
            colon = section.find(':')
            key, value = section[:colon], section[colon + 1:]
            dictTmp[key] = value
            for key, value in PolicyConfig.items(section):
                if 'pass' in key:
                    dictTmp[key] = base64.decodestring(value)
                else:
                    dictTmp[key] = value

            policys.append(dictTmp.copy())
            dictTmp.clear()

        servers = []
        ServerConfig = ConfigParser.ConfigParser(allow_no_value=True)
        ServerConfig.read(serverfile)

        for section in ServerConfig.sections():
            dictTmp = {}
            colon = section.find(':')
            key, value = section[:colon], section[colon + 1:]
            dictTmp[key] = value
            for key, value in ServerConfig.items(section):
                if 'pass' in key:
                    dictTmp[key] = base64.decodestring(value)
                else:
                    dictTmp[key] = value

            servers.append(dictTmp.copy())
            dictTmp.clear()

        return policys, servers

    def assign_jobs(self):
        '''
		读取配置文件,获得针对不同数据库的备份策略,设定备份线程。
		'''
        (policys, servers) = self.get_fileconfig()

        for dictTmp in policys:
            if dictTmp['flag'] == '1':
                for dict in servers:
                    if dict['server'] == dictTmp['server']:
                        serverInfo = dict
                for key in dictTmp.keys():
                    if dictTmp[key] == '':
                        dictTmp[key] = None
                glob_bak_name = 'glob_bak_' + dictTmp['server']
                print[serverInfo, dictTmp['bakcon']]
                self.sched.add_cron_job(self.glob_bak,
                                        args=[serverInfo, dictTmp['bakcon']],
                                        month=dictTmp['globmonth'],
                                        day=dictTmp['globday'],
                                        day_of_week=dictTmp['globweekday'],
                                        hour=dictTmp['globhour'],
                                        minute=dictTmp['globminute'],
                                        second='*/3',
                                        name=glob_bak_name)
                incr_bak_name = 'incr_bak_' + dictTmp['server']
                self.sched.add_cron_job(self.incr_bak,
                                        month=dictTmp['incmonth'],
                                        day=dictTmp['incday'],
                                        day_of_week=dictTmp['incweekday'],
                                        hour=dictTmp['inchour'],
                                        minute=dictTmp['incminute'],
                                        name=incr_bak_name)
        print self.sched.get_jobs()
        print 'assign jobs finished!'

    def assign_monitor(self):
        '''
		设定文件监控线程。
		'''
        self.sched.add_interval_job(self.monitorfile, name='monitorDaemon')
        print self.sched.get_jobs()
        print 'assign monitor finished'

    def filechange(self, monitor, file1, file2, evt_type):
        '''
		备份策略文件发生变化时,撤销计划列表中除文件监控以外的所有计划,然后重新设定备份线程。
		'''
        if evt_type == gio.FILE_MONITOR_EVENT_CHANGED:
            print 'file changed'
            for job in self.sched.get_jobs():
                print job
                if job.name != 'monitorDaemon':
                    self.sched.unschedule_job(job)

            self.assign_jobs()

    def monitorfile(self):
        '''
		启动文件监控线程,并设定多线程运行环境。
		'''
        gfile = gio.File(self.filepath)
        monitor = gfile.monitor_file(gio.FILE_MONITOR_NONE, None)
        monitor.connect("changed", self.filechange)
        gobject.threads_init()
        gml = gobject.MainLoop()
        gml.run()

    def glob_bak(self, serConf, bakcontainer):
        '''
		负责执行一次全局备份,将备份文件上传至云存储。
		'''
        timestr = time.strftime(r"%Y-%m-%d_%H-%M-%S", time.localtime())
        print timestr
        conndb = ConnDatabase(serConf)
        connStor = ConnStorage(serConf)
        (result, bakfilepath) = conndb.conn.glob_bak()
        if result:
            connStor.upload_file(bakcontainer, bakfilepath)
        else:
            print 'global backup error!'

    def incr_bak(self, serConf, bakcontainer):
        '''
		负责执行一次增量备份,将备份文件上传至云存储。
		'''
        conndb = ConnDatabase(serConf)
        connStor = ConnStorage(serConf)
        (result, bakfilepath) = conndb.conn.incr_bak()
        if result:
            connStor.upload_file(bakcontainer, bakfilepath)
        else:
            print 'increase backup error!'
Exemple #55
0
    # calculate our events from the auto_state_events list, need to find a better way of doing this, maybe a config file.
    for event in auto_state_events:
        app.logger.info("Processing scheduled event : %s" %
                        event['event_name'])
        start_hour = event['event_start_time'].split(':')[0]
        start_minute = event['event_start_time'].split(':')[1]
        start_second = event['event_start_time'].split(':')[2]
        start_time = datetime.strptime(event['event_start_time'], time_format)
        end_time = datetime.strptime(event['event_end_time'], time_format)
        event_duration = (end_time - start_time).seconds
        sched.add_cron_job(led_chain.auto_transition,
                           hour=start_hour,
                           minute=start_minute,
                           second=start_second,
                           name=event['event_name'],
                           kwargs={
                               'state': event['event_state'],
                               'transition_duration':
                               event['transition_duration']
                           },
                           misfire_grace_time=event_duration)

    app.logger.debug("Startup job list contains : %s" % sched.get_jobs())

    try:
        app.run(host='0.0.0.0',
                port=int(app_config.get("general", "web_port")),
                use_reloader=False)
    except KeyboardInterrupt:
        app.logger.warning("Caught keyboard interupt.  Shutting down ...")
Exemple #56
0
class Scheduler(object):

    schedulr = None
    aps3 = True

    def __init__(self):

        #####
        # ApScheduler version detection
        try:
            # APScheduler 3.x implementation
            from apscheduler.schedulers.background import BackgroundScheduler
            self.schedulr = BackgroundScheduler()
            self.aps3 = True
        except ImportError:
            # APScheduler 2.x implementation
            from apscheduler.scheduler import Scheduler
            self.schedulr = Scheduler()
            self.aps3 = False

    def start(self):
        return self.schedulr.start()

    def get_job(self, name):
        if self.aps3:
            return self.schedulr.get_job(name)
        else:
            jobs = self.schedulr.get_jobs()
            for job in jobs:
                if job.name == name:
                    return job

            return None

    def add_job(self, func, trigger, args=None, kwargs=None, id=None, **trigger_args):
        if self.aps3:
            return self.schedulr.add_job(func, trigger, id=id, args=args, kwargs=kwargs, **trigger_args)
        else:
            if trigger is 'date':
                run_date = trigger_args['run_date']   # by intention: to raise if not set!
                del trigger_args['run_date']
                return self.schedulr.add_date_job(func, run_date, name=id, args=args, kwargs=kwargs)
            elif trigger is 'interval':
                # only partially implemented!!
                seconds = 0
                minutes = 0
                hours = 0
                if 'seconds' in trigger_args:
                    seconds = trigger_args.get('seconds', 0)
                    del trigger_args['seconds']

                if 'minutes' in trigger_args:
                    minutes = trigger_args.get('minutes', 0)
                    del trigger_args['minutes']

                if 'hours' in trigger_args:
                    hours = trigger_args.get('hours', 0)
                    del trigger_args['hours']

                return self.schedulr.add_interval_job(func, name=id,
                                                      hours=hours, minutes=minutes, seconds=seconds,
                                                      args=args, kwargs=kwargs)
            elif trigger is 'cron':
                # only partially implemented!!
                second = 0
                minute = 0
                hour = 0
                if 'second' in trigger_args:
                    second = trigger_args.get('second', 0)
                    del trigger_args['second']

                if 'minute' in trigger_args:
                    minute = trigger_args.get('minute', 0)
                    del trigger_args['minute']

                if 'hour' in trigger_args:
                    hour = trigger_args.get('hour', 0)
                    del trigger_args['hour']

                return self.schedulr.add_cron_job(func, name=id, hour=hour, minute=minute, second=second)
            else:
                raise NotImplementedError

    def shutdown(self):
        return self.schedulr.shutdown()

    # https://github.com/ralphwetzel/theonionbox/issues/19#issuecomment-263110953
    def check_tz(self):
        from tzlocal import get_localzone

        try:
            # APScheduler 3.x
            from apscheduler.util import astimezone

        except ImportError:
            # https://github.com/ralphwetzel/theonionbox/issues/31
            # APScheduler 2.x
            # import six
            from pytz import timezone, utc
            from datetime import tzinfo

            # copied here from apscheduler/util.py (version 3.4)
            # copyright Alex Grönholm
            # https://github.com/agronholm/apscheduler

            def astimezone(obj):
                """
                Interprets an object as a timezone.

                :rtype: tzinfo

                """
                # if isinstance(obj, six.string_types):
                if isinstance(obj, (str, unicode)):
                    return timezone(obj)
                if isinstance(obj, tzinfo):
                    if not hasattr(obj, 'localize') or not hasattr(obj, 'normalize'):
                        raise TypeError('Only timezones from the pytz library are supported')
                    if obj.zone == 'local':
                        raise ValueError(
                            'Unable to determine the name of the local timezone -- you must explicitly '
                            'specify the name of the local timezone. Please refrain from using timezones like '
                            'EST to prevent problems with daylight saving time. Instead, use a locale based '
                            'timezone name (such as Europe/Helsinki).')
                    return obj
                if obj is not None:
                    raise TypeError('Expected tzinfo, got %s instead' % obj.__class__.__name__)

        tz = get_localzone()
        try:
            res = astimezone(tz)
        except ValueError as ve:
            return False

        return True