def reset_jobs(task, date_start, date_end, instrument=None, state=None, force=False, dry_run=False): """Change the state of the specified jobs back to "Unknown". If a state is specified, select only that state. Active jobs are skipped unless the force argument is set. """ db = get_database() obsquery = {} if date_start is not None and date_end is not None: obsquery['utdate'] = Range(date_start, date_end) elif date_start is None and date_end is None: pass else: raise CommandError('only one of start and end date specified') if instrument is not None: obsquery['instrument'] = instrument if state is not None: state = JSAProcState.lookup_name(state) n_active = 0 for job in db.find_jobs(location='JAC', task=task, obsquery=obsquery, state=state): state_info = JSAProcState.get_info(job.state) # Check if the job is in an "active" state. if state_info.active and not force: logger.warning('Skipping active job %i (%s)', job.id, state_info.name) n_active += 1 continue logger.info('Resetting status of job %i (was %s)', job.id, state_info.name) if not dry_run: db.change_state(job.id, JSAProcState.UNKNOWN, 'Resetting job', state_prev=job.state) if n_active: raise CommandError('Could not reset {0} active jobs'.format(n_active))
def reset_jobs(task, date_start, date_end, instrument=None, state=None, force=False, dry_run=False): """Change the state of the specified jobs back to "Unknown". If a state is specified, select only that state. Active jobs are skipped unless the force argument is set. """ db = get_database() obsquery = {} if date_start is not None and date_end is not None: obsquery['utdate'] = Range(date_start, date_end) elif date_start is None and date_end is None: pass else: raise CommandError('only one of start and end date specified') if instrument is not None: obsquery['instrument'] = instrument if state is not None: state = JSAProcState.lookup_name(state) n_active = 0 for job in db.find_jobs(location='JAC', task=task, obsquery=obsquery, state=state): state_info = JSAProcState.get_info(job.state) # Check if the job is in an "active" state. if state_info.active and not force: logger.warning('Skipping active job %i (%s)', job.id, state_info.name) n_active += 1 continue logger.info('Resetting status of job %i (was %s)', job.id, state_info.name) if not dry_run: db.change_state(job.id, JSAProcState.UNKNOWN, 'Resetting job', state_prev=job.state) if n_active: raise CommandError( 'Could not reset {0} active jobs'.format(n_active))
def search_log_files( pattern, filename_pattern, task, project=None, state=None, after_context=None): db = get_database() re_pattern = re.compile(pattern) re_filename = re.compile(filename_pattern) if state is None: state = JSAProcState.COMPLETE else: state = JSAProcState.lookup_name(state) if after_context is None: after_context = 0 search_kwargs = { 'task': task, 'state': state, } if project is not None: search_kwargs['obsquery'] = {'project': project} jobs = [x.id for x in db.find_jobs(**search_kwargs)] for job_id in jobs: logger.debug('Checking log files for job %i', job_id) log_dir = get_log_dir(job_id) # Find the latest matching log by iterating through them in reverse # order and "breaking" after the first match. for filename in sorted(os.listdir(log_dir), reverse=True): if not re_filename.search(filename): continue logger.debug('Found log file for job %i: %s', job_id, filename) matched = 0 matched_lines = [] pathname = os.path.join(log_dir, filename) with open(pathname, 'r') as f: for line in f: if matched or re_pattern.search(line): matched += 1 matched_lines.append(line.rstrip()) if matched > after_context: break if matched: logger.info( 'Found match for job %i: %s', job_id, matched_lines[0]) for matched_line in matched_lines[1:]: logger.info( '... continuation %i: %s', job_id, matched_line) break
def search_log_files(pattern, filename_pattern, task, project=None, state=None, after_context=None): db = get_database() re_pattern = re.compile(pattern) re_filename = re.compile(filename_pattern) if state is None: state = JSAProcState.COMPLETE else: state = JSAProcState.lookup_name(state) if after_context is None: after_context = 0 search_kwargs = { 'task': task, 'state': state, } if project is not None: search_kwargs['obsquery'] = {'project': project} jobs = [x.id for x in db.find_jobs(**search_kwargs)] for job_id in jobs: logger.debug('Checking log files for job %i', job_id) log_dir = get_log_dir(job_id) # Find the latest matching log by iterating through them in reverse # order and "breaking" after the first match. for filename in sorted(os.listdir(log_dir), reverse=True): if not re_filename.search(filename): continue logger.debug('Found log file for job %i: %s', job_id, filename) matched = 0 matched_lines = [] pathname = os.path.join(log_dir, filename) with open(pathname, 'r') as f: for line in f: if matched or re_pattern.search(line): matched += 1 matched_lines.append(line.rstrip()) if matched > after_context: break if matched: logger.info('Found match for job %i: %s', job_id, matched_lines[0]) for matched_line in matched_lines[1:]: logger.info('... continuation %i: %s', job_id, matched_line) break