def initialize_periodic_calls(): for pcall in PERIODIC_CALLS: name = pcall['name'] pattern = pcall.get('pattern') or CONF.get('cron', name) pcall_db = api.get_periodic_call_by_name(name) start_time = datetime.datetime.now() next_time = croniter(pattern, start_time).get_next(datetime.datetime) target_method = pcall['func_path'] arguments = pcall.get('arguments', {}) values = { 'execution_time': next_time, 'pattern': pattern, 'target_method': target_method, 'arguments': json.dumps(arguments), 'processing': False } if not pcall_db: values.update({'name': name}) pcall_db = api.create_periodic_call(values) else: pcall_db = api.update_periodic_call(name, values) SEMAPHORES[pcall_db.id] = semaphore.Semaphore(pcall.get('threads', 1))
def delete_reminder(name, user): user_reminders = db_api.get_periodic_calls(user_id=user) if not user_reminders: raise RuntimeError("У Вас еще нет напоминалок.") reminder_to_delete = db_api.get_periodic_call_by_name(name) if reminder_to_delete.id in [r.id for r in user_reminders]: db_api.delete_periodic_call(name) else: raise RuntimeError("Неправильный id.")