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 add_reminder(message, name, count, user, pattern, text): validate_reminder(count, user, text) next_time = utils.get_next_time(pattern) pcall = db_api.create_periodic_call( { 'name': name, 'arguments': json.dumps({ 'message': message, 'text': text }), 'target_method': 'vk_bot.bot.actions.answer_on_message', 'pattern': pattern, 'remaining_executions': count, 'execution_time': next_time, 'user_id': user } ) utils.add_semaphore(pcall.id, 1) return pcall