本文整理汇总了Python中celery.schedules.schedule方法的典型用法代码示例。如果您正苦于以下问题:Python schedules.schedule方法的具体用法?Python schedules.schedule怎么用?Python schedules.schedule使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类celery.schedules
的用法示例。
在下文中一共展示了schedules.schedule方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: from_entry
# 需要导入模块: from celery import schedules [as 别名]
# 或者: from celery.schedules import schedule [as 别名]
def from_entry(cls, name, session, skip_fields=('relative', 'options'), **entry):
"""
PeriodicTask
:param session:
:param name:
:param skip_fields:
:param entry:
:return:
"""
fields = dict(entry)
for skip_field in skip_fields:
fields.pop(skip_field, None)
schedule = fields.pop('schedule')
model_schedule, model_field = cls.to_model_schedule(schedule, session)
fields[model_field] = model_schedule
fields['args'] = json.dumps(fields.get('args') or [])
fields['kwargs'] = json.dumps(fields.get('kwargs') or {})
model, _ = PeriodicTask.update_or_create(session, name=name, defaults=fields)
cls.save_model(session, model)
return cls(model)
示例2: from_schedule
# 需要导入模块: from celery import schedules [as 别名]
# 或者: from celery.schedules import schedule [as 别名]
def from_schedule(cls, dbsession, schedule):
spec = {'minute': schedule._orig_minute,
'hour': schedule._orig_hour,
'day_of_week': schedule._orig_day_of_week,
'day_of_month': schedule._orig_day_of_month,
'month_of_year': schedule._orig_month_of_year}
try:
query = dbsession.query(CrontabSchedule)
query = query.filter_by(**spec)
existing = query.one()
return existing
except NoResultFound:
return cls(**spec)
except MultipleResultsFound:
query = dbsession.query(CrontabSchedule)
query = query.filter_by(**spec)
query.delete()
dbsession.commit()
return cls(**spec)
示例3: from_entry
# 需要导入模块: from celery import schedules [as 别名]
# 或者: from celery.schedules import schedule [as 别名]
def from_entry(cls, name, skip_fields=('relative', 'options'), **entry):
options = entry.get('options') or {}
fields = dict(entry)
for skip_field in skip_fields:
fields.pop(skip_field, None)
schedule = fields.pop('schedule')
model_schedule, model_field = cls.to_model_schedule(schedule)
fields[model_field] = model_schedule
fields['args'] = fields.get('args') or []
fields['kwargs'] = fields.get('kwargs') or {}
fields['queue'] = options.get('queue')
fields['exchange'] = options.get('exchange')
fields['routing_key'] = options.get('routing_key')
query = dbsession.query(DatabaseSchedulerEntry)
query = query.filter_by(name=name)
db_entry = query.first()
if db_entry is None:
new_entry = DatabaseSchedulerEntry(**fields)
new_entry.name = name
dbsession.add(new_entry)
dbsession.commit()
db_entry = new_entry
return cls(db_entry)
示例4: from_schedule
# 需要导入模块: from celery import schedules [as 别名]
# 或者: from celery.schedules import schedule [as 别名]
def from_schedule(cls, session, schedule):
spec = {
'minute': schedule._orig_minute,
'hour': schedule._orig_hour,
'day_of_week': schedule._orig_day_of_week,
'day_of_month': schedule._orig_day_of_month,
'month_of_year': schedule._orig_month_of_year,
}
if schedule.tz:
spec.update({
'timezone': schedule.tz.zone
})
model = session.query(CrontabSchedule).filter_by(**spec).first()
if not model:
model = cls(**spec)
session.add(model)
session.commit()
return model
示例5: _unpack_fields
# 需要导入模块: from celery import schedules [as 别名]
# 或者: from celery.schedules import schedule [as 别名]
def _unpack_fields(cls, session, schedule,
args=None, kwargs=None, relative=None, options=None,
**entry):
"""
**entry sample:
{'task': 'celery.backend_cleanup',
'schedule': <crontab: 0 4 * * * (m/h/d/dM/MY)>,
'options': {'expires': 43200}}
"""
model_schedule, model_field = cls.to_model_schedule(session, schedule)
entry.update(
# the model_id which to relationship
{model_field + '_id': model_schedule.id},
args=dumps(args or []),
kwargs=dumps(kwargs or {}),
**cls._unpack_options(**options or {})
)
return entry
示例6: all_as_schedule
# 需要导入模块: from celery import schedules [as 别名]
# 或者: from celery.schedules import schedule [as 别名]
def all_as_schedule(self):
# TODO:
session = self.Session()
with session_cleanup(session):
logger.debug('DatabaseScheduler: Fetching database schedule')
# get all enabled PeriodicTask
models = session.query(self.Model).filter_by(enabled=True).all()
s = {}
for model in models:
try:
s[model.name] = self.Entry(model,
app=self.app,
Session=self.Session,
session=session)
except ValueError:
pass
return s
示例7: sync
# 需要导入模块: from celery import schedules [as 别名]
# 或者: from celery.schedules import schedule [as 别名]
def sync(self):
"""override"""
logger.info('Writing entries...')
_tried = set()
_failed = set()
try:
while self._dirty:
name = self._dirty.pop()
try:
self.schedule[name].save() # save to database
logger.debug(
'{name} save to database'.format(name=name))
_tried.add(name)
except (KeyError) as exc:
logger.error(exc)
_failed.add(name)
except sqlalchemy.exc.IntegrityError as exc:
logger.exception('Database error while sync: %r', exc)
except Exception as exc:
logger.exception(exc)
finally:
# retry later, only for the failed ones
self._dirty |= _failed
示例8: schedule
# 需要导入模块: from celery import schedules [as 别名]
# 或者: from celery.schedules import schedule [as 别名]
def schedule(self):
initial = update = False
if self._initial_read:
logger.debug('DatabaseScheduler: initial read')
initial = update = True
self._initial_read = False
elif self.schedule_changed():
# when you updated the `PeriodicTasks` model's `last_update` field
logger.info('DatabaseScheduler: Schedule changed.')
update = True
if update:
self.sync()
self._schedule = self.all_as_schedule()
# the schedule changed, invalidate the heap in Scheduler.tick
if not initial:
self._heap = []
self._heap_invalidated = True
if logger.isEnabledFor(logging.DEBUG):
logger.debug('Current schedule:\n%s', '\n'.join(
repr(entry) for entry in values(self._schedule)),
)
# logger.debug(self._schedule)
return self._schedule
示例9: test_serialize_entry_json_serializable
# 需要导入模块: from celery import schedules [as 别名]
# 或者: from celery.schedules import schedule [as 别名]
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)
示例10: serialize_entry
# 需要导入模块: from celery import schedules [as 别名]
# 或者: from celery.schedules import schedule [as 别名]
def serialize_entry(entry):
"""
Serialize ScheduleEntry to json-valid dictionary.
Helps serialize entry to json, yml and any other formats.
:param entry: ScheduleEntry
:return: json-valid dictionary
"""
return {
'name': entry.name,
'task': entry.task,
'schedule': encode_schedule(entry.schedule),
'args': entry.args,
'kwargs': entry.kwargs,
'last_run_at': encode_datetime(entry.last_run_at),
'total_run_count': entry.total_run_count,
'options': entry.options
}
示例11: deserialize_entry
# 需要导入模块: from celery import schedules [as 别名]
# 或者: from celery.schedules import schedule [as 别名]
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'],
)
示例12: test_parsing_dict
# 需要导入模块: from celery import schedules [as 别名]
# 或者: from celery.schedules import schedule [as 别名]
def test_parsing_dict(self):
task = PeriodicTask(**self.task_dict)
assert task.name == 'bruteforce_ssh.AlertBruteforceSsh'
assert task.task == 'bruteforce_ssh.AlertBruteforceSsh'
assert isinstance(task._id, str)
assert task.args == []
assert task.kwargs == {}
assert task.enabled is True
assert task.queue is None
assert task.exchange is None
assert task.routing_key is None
assert task.last_run_at is None
assert task.run_immediately is False
assert task.total_run_count is 0
assert task.schedule_type == 'interval'
assert task.schedule_str == '1.0 seconds'
assert isinstance(task.celery_schedule, Interval) is True
assert isinstance(task.schedule, celery_sched) is True
示例13: schedule
# 需要导入模块: from celery import schedules [as 别名]
# 或者: from celery.schedules import schedule [as 别名]
def schedule(self):
return schedules.schedule(datetime.timedelta(**{self.period.code: self.every}))
示例14: from_schedule
# 需要导入模块: from celery import schedules [as 别名]
# 或者: from celery.schedules import schedule [as 别名]
def from_schedule(cls, session, schedule, period='seconds'):
every = max(schedule.run_every.total_seconds(), 0)
obj = cls.filter_by(session, every=every, period=period).first()
if obj is None:
return cls(every=every, period=period)
else:
return obj
示例15: is_due
# 需要导入模块: from celery import schedules [as 别名]
# 或者: from celery.schedules import schedule [as 别名]
def is_due(self):
if not self.model.enabled:
return False, 5.0 # 5 second delay for re-enable.
return self.schedule.is_due(self.last_run_at)