def program_session_create_or_add(connection: Connection, hostname: str, username: str, elapsed_seconds: int, program_name: str, pids: Iterable[int]) -> int: "Either create a new program session or update an existing one." program = Program.search(connection, name=program_name) program_session = ProgramSession.search(connection, program=program.id, hostname=hostname, username=username, end=None) if program_session is None: program_session_id = ProgramSession.insert( connection, end=None, hostname=hostname, pids=",".join(sorted(pids)), program=program.id, start=datetime.datetime.now(), username=username, ) LOGGER.debug("Created new program session %s", program_session_id) else: program_session_id = program_session.id ProgramSession.update( connection, program_session_id, pids=",".join(sorted(pids)), ) LOGGER.debug("Updated program session %d to have pids %s", program_session_id, sorted(pids)) return program_session_id
def program_session_close_except( connection: Connection, hostname: str, exempt_program_names: Iterable[str], ) -> None: "Close all program_sessions, except any for the named programs." now = datetime.datetime.now() programs = Program.list(connection) program_id_to_name = {program.id: program.name for program in programs} open_sessions = ProgramSession.list(connection, hostname=hostname, end=None) for program_session in open_sessions: program_name = program_id_to_name[program_session.program] if program_name in exempt_program_names: continue ProgramSession.update( connection, program_session.id, end=now, ) LOGGER.info("Ended program session %s", program_session.id)