def test_serialize_deserialize_entry(dt):
    entry = ScheduleEntry(
        name='entry-1',
        task='entry-1-task',
        schedule=dt,
        args=('arg1', 'arg2'),
        kwargs={
            'key1': 'val1',
            'key2': 'val2'
        },
        last_run_at=datetime.now(),
        total_run_count=1,
        options={},
    )

    decoded_entry = deserialize_entry(serialize_entry(entry))

    assert decoded_entry.__reduce__() == entry.__reduce__()
Beispiel #2
0
 def _merge(self, schedule):
     for name, entry_dict in schedule.iteritems():
         entry = ScheduleEntry(name, **entry_dict)
         if name not in self._schedule:
             self._schedule[name] = entry
         else:
             # _lock() the existing entry so that these values aren't changed
             # while we're merging them.
             lock = self._lock(name)
             if lock:
                 try:
                     existing = self._schedule.sync(name)
                     if existing:
                         entry.last_run_at = existing.last_run_at
                         entry.total_run_count = existing.total_run_count
                     self._schedule[name] = entry
                 finally:
                     self._unlock(lock)
Beispiel #3
0
    def test_entries_save_load(self):
        entries = {
            'entry-1':
            ScheduleEntry(name='entry-1', last_run_at=datetime.utcnow()),
        }

        self.store.save_entries(entries)

        loaded = self.store.load_entries()

        assert entries.keys() == loaded.keys()
Beispiel #4
0
 def _load(self, record):
     try:
         entry = ScheduleEntry(record['name'], record['task'],
                               record['last_run_at'],
                               record['total_run_count'],
                               to_schedule(record['schedule']),
                               record['args'], record['kwargs'],
                               record['options'])
         dict.__setitem__(self, entry.name, entry)
         return entry
     except Exception as e:
         return None
Beispiel #5
0
    def _merge(self, schedule):
        """schedule_keys = self.__redis_connection.hgetall(ENTRY_LIST_KEY).keys()

        if len(schedule_keys) > 0:
            self.__redis_connection.hdel(ENTRY_LIST_KEY, *schedule_keys)"""

        for name, entry_dict in list(schedule.items()):
            entry = ScheduleEntry(name, **entry_dict)
            if name not in self._schedule:
                self._schedule[name] = entry
            else:
                # _lock() the existing entry so that these values aren't changed
                # while we're merging them.
                lock = self._lock(name)
                if lock:
                    try:
                        existing = self._schedule.sync(name)
                        if existing:
                            entry.last_run_at = existing.last_run_at
                            entry.total_run_count = existing.total_run_count
                        self._schedule[name] = entry
                    finally:
                        self._unlock(lock)
    def _merge(self, schedule):
        """schedule_keys = self.__redis_connection.hgetall(ENTRY_LIST_KEY).keys()

        if len(schedule_keys) > 0:
            self.__redis_connection.hdel(ENTRY_LIST_KEY, *schedule_keys)"""

        for name, entry_dict in schedule.items():
            entry = ScheduleEntry(name, **entry_dict)
            if name not in self._schedule:
                self._schedule[name] = entry
            else:
                # _lock() the existing entry so that these values aren't changed
                # while we're merging them.
                lock = self._lock(name)
                if lock:
                    try:
                        existing = self._schedule.sync(name)
                        if existing:
                            entry.last_run_at = existing.last_run_at
                            entry.total_run_count = existing.total_run_count
                        self._schedule[name] = entry
                    finally:
                        self._unlock(lock)
Beispiel #7
0
def deserialize_entry(entry):
    """
    Deserialize ScheduleEntry from dictionary.

    Helps deserialize entry from json, yml and any other formats.

    :param entry:
    :return:
    """
    return ScheduleEntry(
        name=entry['name'],
        task=entry['task'],
        schedule=decode_schedule(entry['schedule']),
        args=entry['args'],
        kwargs=entry['kwargs'],
        last_run_at=decode_datetime(entry['last_run_at']),
        total_run_count=entry['total_run_count'],
        options=entry['options'],
    )
def test_serialize_entry_json_serializable(dt):
    entry = ScheduleEntry(
        name='entry-1',
        task='entry-1-task',
        schedule=dt,
        args=('arg1', 'arg2'),
        kwargs={
            'key1': 'val1',
            'key2': 'val2'
        },
        last_run_at=datetime.now(),
        total_run_count=1,
        options={},
    )

    obj = serialize_entry(entry)

    try:
        json.dumps(obj)
    except Exception as e:
        pytest.fail(e)
Beispiel #9
0
    def test_seralization_1(self):
        sch = schedules.schedule(run_every=timedelta(seconds=365))
        date = datetime(2017, 5, 3, 23, 3, 47)
        entry = ScheduleEntry(name='everytime',
                              task='celery.runtask',
                              schedule=sch,
                              args=['hello', 'world', 5],
                              kwargs={'time': 4, 'name': 'testers'},
                              options={'echo': True},
                              last_run_at=datetime.now(timezone.utc),
                              total_run_count=99)

        dumped = EntrySchema(strict=True).dump(entry).data
        loaded = EntrySchema(strict=True).load(dumped).data

        assert entry.name == loaded.name
        assert entry.task == loaded.task
        assert entry.schedule.run_every == loaded.schedule.run_every
        assert entry.args == loaded.args
        assert entry.kwargs == loaded.kwargs
        assert entry.options == loaded.options
        assert entry.last_run_at == loaded.last_run_at
        assert entry.total_run_count == loaded.total_run_count
Beispiel #10
0
def test_validate_celerybeat_schedule(name, entry):
    entry = ScheduleEntry(name=name, app=app, **entry)
    assert entry.task in app.tasks
Beispiel #11
0
 def __init__(self, limit_run_time=None, *args, **kwargs):
     self.limit_run_time = limit_run_time or 0
     ScheduleEntry.__init__(self, *args, **kwargs)
Beispiel #12
0
def test_validate_celerybeat_schedule(name, entry):
    entry = ScheduleEntry(name=name, app=app, **entry)
    assert entry.task in app.tasks
    mod_name = app.tasks[entry.task].__module__
    assert mod_name in settings.CELERY_IMPORTS, f"{mod_name} is missing from CELERY_IMPORTS"
 def __init__(self, times=None, *args, **kwargs):
     self.times = times
     ScheduleEntry.__init__(self, *args, **kwargs)
Beispiel #14
0
 def make_entry(self, data):
     return ScheduleEntry(**data)