def setUp(self): self.ram = self.create_ram() self.persist = self.create_persist() self.scheduler1 = Scheduler(ram_storage=self.create_ram(), persist_storage=self.persist) self.trigger_time = int(time() + 600) for i in xrange(self.N): self.scheduler1.add(trigger_time=self.trigger_time, ev_index=i, ev_data=[{'%s' % i: i}]) self.scheduler2 = Scheduler(ram_storage=self.create_ram(), persist_storage=self.persist)
class BasicFunctionTestCase(PersistMixin, TestCase): def setUp(self): self.ram = self.create_ram() self.persist = self.create_persist() self.scheduler = Scheduler(ram_storage=self.ram, persist_storage=self.persist) def test_add(self): event_id = self.scheduler.add(delay_time=1.0, foobar=1) self.assertGreater(event_id, 0) def test_delete(self): event_id = self.scheduler.add(delay_time=1.0, foobar=1) self.scheduler.delete(event_id) self.assertEqual(self.ram.get(event_id), None)
class DateTimeAwarenessCase(PersistMixin, TestCase): def setUp(self): self.ram = self.create_ram() self.persist = self.create_persist() self.scheduler = Scheduler(ram_storage=self.ram, persist_storage=self.persist) def test_convert(self): dt1 = datetime.now() dt2 = datetime.utcnow() delta1 = self.scheduler.timestamp2dt(self.scheduler.dt2timestamp(dt1)) - dt1 delta2 = self.scheduler.timestamp2dt(self.scheduler.dt2timestamp(dt2)) - dt2 self.assertLess(delta1.total_seconds(), 1.0) self.assertLess(delta2.total_seconds(), 1.0)
class RestoreTestCase(PersistMixin, TestCase): N = 100 def setUp(self): self.ram = self.create_ram() self.persist = self.create_persist() self.scheduler1 = Scheduler(ram_storage=self.create_ram(), persist_storage=self.persist) self.trigger_time = int(time() + 600) for i in xrange(self.N): self.scheduler1.add(trigger_time=self.trigger_time, ev_index=i, ev_data=[{'%s' % i: i}]) self.scheduler2 = Scheduler(ram_storage=self.create_ram(), persist_storage=self.persist) def test_restore(self): self.scheduler2.restore() events = self.scheduler2.get_events(self.trigger_time) self.assertEqual(len(events), self.N) for event in events: self.assertEqual(event.data.ev_data[0][str(event.data.ev_index)], event.data.ev_index)
def do_test(self, duration, events): current_time = int(time()) schedule = self.generate_schedule(duration, events, current_time=current_time) scheduler = Scheduler( ram_storage=self.ram_storages[randint(0,len(self.ram_storages)-1)](), persist_storage=self.create_persist() ) for event_time, events in schedule: if event_time == 0: continue for event in events: scheduler.add(trigger_time=event_time, key=event['key']) for i in xrange(0, duration): events = scheduler.get_events(current_time + i * 60.0) events = [ev.data for ev in events] if events != schedule[i][1]: print "fail tick = ", i events = scheduler.get_events(current_time + i * 60.0) self.assertEqual(events, schedule[i][1])
def scheduler_process(queue, ppid): """ Процесс для работы планировщика """ ram_class = import_class(settings.SCHEDULER_RAM_STORAGE) persist_class = import_class(settings.SCHEDULER_PERSISTENT_STORAGE) scheduler = Scheduler( ram_storage=ram_class(), persist_storage=persist_class( settings.SCHEDULER_DB_PERSISTENT_STORAGE, pool_recycle=settings.POOL_RECYCLE, pool_size=settings.POOL_SIZE ) ) # Thread for immediate running of incoming commands Thread(target=inc_message_thread, args=(queue, scheduler, )).start() logging.info("Restoring previous events...") scheduler.restore() logging.info("Restoring completed.") period = 15 if settings.DEBUG is False else 5 start_time = time.time() counter = 0 while 1: # Kill himself if parent is killed if os.getppid() != ppid: sys.exit() #do something events = scheduler.get_events() for event in events: scheduler.mark_as_processing(event.event_id) logging.debug("Event (id=%s) marked as processing", event.event_id) logging.debug("Sending event (id=%s) to posting queue...", event.event_id) message = dict(event_id=event.event_id) message.update(event.data) send_to_queue(settings.RABBITMQ_HOST, settings.POSTING_QUEUE, message) allowance = time.time() - counter*period - start_time logging.debug(" [x] allowance = %s, counter = %s, per cent of period = %s", allowance, counter, int(allowance/period * 100)) if allowance > period/3.0: if period - allowance >= 0.0: time.sleep(period - allowance) else: time.sleep(0.1) counter = -1 start_time = time.time() else: time.sleep(period) counter += 1
def setUp(self): self.ram = self.create_ram() self.persist = self.create_persist() self.scheduler = Scheduler(ram_storage=self.ram, persist_storage=self.persist)
# -*- coding: utf8 -*- from scheduler.core import Scheduler from time import time from scheduler.storage.ram import SimpleListStorage, RBTreeStorage from scheduler.storage.persist import FakePersistStorage from scheduler.storage.persist import DBPersistStorage from random import randint from time import time fake = FakePersistStorage() db_persist = DBPersistStorage('mysql://*****:*****@localhost/liketools_scheduler') if not db_persist.initiated(): db_persist.init() scheduler = Scheduler(ram_storage=RBTreeStorage(), persist_storage=FakePersistStorage())#db_persist) #scheduler.restore() min_time = int(time()) max_time = min_time + min_time / 2; delay = min_time / 2; for i in xrange(10**3): scheduler.add(delay_time=randint(0, delay), index=i) s = 0.0 N = 100 for i in xrange(N): print i, " iteration..." t = time()
from scheduler.core import Scheduler from scheduler.storage.ram import RBTreeStorage from scheduler.storage.persist import DBPersistStorage from time import time from random import randint N = 100 scheduler = Scheduler(ram_storage=RBTreeStorage(), persist_storage=DBPersistStorage('mysql://*****:*****@localhost/liketools')) start = time() print "Start restoring..." scheduler.restore() print "Restored..." print "Restoring takes ", (time() - start), " seconds" s = 0.0 for i in xrange(N): start = time() events = scheduler.get_events(randint(0, int(start))) s = time() - s print "Average get_events() duratios is ", (s/N), "seconds"