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 main(): oncall_config = read_config( os.environ.get('ONCALL_CFG_PATH', '/home/oncall/config/config.yaml')) mysql_config = oncall_config['db']['conn']['kwargs'] # It often takes several seconds for MySQL to start up. oncall dies upon start # if it can't immediately connect to MySQL, so we have to wait for it. wait_for_mysql(mysql_config) if 'DOCKER_DB_BOOTSTRAP' in os.environ: if not os.path.exists(initializedfile): initialize_mysql_schema(mysql_config) os.system('/usr/local/bin/uwsgi -y /home/oncall/daemons/uwsgi.yaml:prod')
# Copyright (c) LinkedIn Corporation. All rights reserved. Licensed under the BSD-2 Clause license. # See LICENSE in the project root for license information. import os from oncall import utils from oncall.app import init init(utils.read_config(os.environ['CONFIG'])) from oncall.app import application # noqa