def init_notifier(config): db.init(config['db']) global default_timezone default_timezone = config['notifier'].get('default_timezone', 'US/Pacific') if config['notifier']['skipsend']: global send_message send_message = blackhole
def main(): config = utils.read_config(sys.argv[1]) db.init(config['db']) cycle_time = config.get('scheduler_cycle_time', 3600) while 1: connection = db.connect() db_cursor = connection.cursor(db.DictCursor) start = time.time() # Iterate through all teams db_cursor.execute('SELECT id, name, scheduling_timezone FROM team WHERE active = TRUE') teams = db_cursor.fetchall() for team in teams: team_id = team['id'] # Get rosters for team db_cursor.execute('SELECT `id`, `name` FROM `roster` WHERE `team_id` = %s', team_id) rosters = db_cursor.fetchall() if db_cursor.rowcount == 0: continue logger.info('scheduling for team: %s', team['name']) events = [] for roster in rosters: roster_id = roster['id'] # Get schedules for each roster schedules = get_schedules({'team_id': team_id, 'roster_id': roster_id}) for schedule in schedules: if schedule['auto_populate_threshold'] <= 0: continue logger.info('\t\tschedule: %s', str(schedule['id'])) schedule['timezone'] = team['scheduling_timezone'] # Calculate events for schedule future_events, last_epoch = calculate_future_events(schedule, db_cursor) role_id = get_role_id(schedule['role'], db_cursor) for epoch in future_events: # Add (start_time, schedule_id, role_id, roster_id, epoch_events) to events events.append((min([ev['start'] for ev in epoch]), schedule['id'], role_id, roster_id, epoch)) set_last_epoch(schedule['id'], last_epoch, db_cursor) # Create events in the db, associating a user to them # Iterate through events in order of start time to properly assign users for event_info in sorted(events, key=lambda x: x[0]): _, schedule_id, role_id, roster_id, epoch = event_info user_id = find_least_active_available_user_id(team_id, role_id, roster_id, epoch, db_cursor) if not user_id: logger.info('Failed to find available user') continue logger.info('Found user: %s', user_id) create_events(team_id, schedule_id, user_id, epoch, role_id, db_cursor) connection.commit() # Sleep until next time sleep_time = cycle_time - (time.time() - start) if sleep_time > 0: logger.info('Sleeping for %s seconds' % sleep_time) time.sleep(cycle_time - (time.time() - start)) else: logger.info('Schedule loop took %s seconds, skipping sleep' % (time.time() - start)) db_cursor.close() connection.close()
def main(): setup_logger() config = utils.read_config(sys.argv[1]) user_sync_config = config.get('user_sync') if not user_sync_config: sys.exit('user_sync config not found!') sync_module = user_sync_config.get('module') if not sync_module: sys.exit('user_sync module not found!') db.init(config['db']) spawn(importlib.import_module(sync_module).main, config).join()
def main(): config = utils.read_config(sys.argv[1]) db.init(config['db']) cycle_time = config.get('scheduler_cycle_time', 3600) schedulers = {} while 1: connection = db.connect() db_cursor = connection.cursor(db.DictCursor) start = time.time() # Load all schedulers db_cursor.execute('SELECT name FROM scheduler') schedulers = {} for row in db_cursor: try: scheduler_name = row['name'] if scheduler_name not in schedulers: schedulers[scheduler_name] = load_scheduler(scheduler_name) except (ImportError, AttributeError): logger.exception('Failed to load scheduler %s, skipping', row['name']) # Iterate through all teams db_cursor.execute( 'SELECT id, name, scheduling_timezone FROM team WHERE active = TRUE' ) teams = db_cursor.fetchall() for team in teams: logger.info('scheduling for team: %s', team['name']) schedule_map = defaultdict(list) for schedule in get_schedules({'team_id': team['id']}): schedule_map[schedule['scheduler']['name']].append(schedule) for scheduler_name, schedules in schedule_map.iteritems(): schedulers[scheduler_name].schedule(team, schedules, (connection, db_cursor)) # Sleep until next time sleep_time = cycle_time - (time.time() - start) if sleep_time > 0: logger.info('Sleeping for %s seconds' % sleep_time) time.sleep(cycle_time - (time.time() - start)) else: logger.info('Schedule loop took %s seconds, skipping sleep' % (time.time() - start)) db_cursor.close() connection.close()
def require_db(request): # Read config based on pytest root directory. Assumes config lives at oncall/configs/config.yaml cfg_path = os.path.join(str(request.config.rootdir), 'configs/config.yaml') with open(cfg_path) as f: config = yaml.safe_load(f) db.init(config['db'])
def require_db(): db.init(db_config)