def run_job_lite(cfg_file, log_level, log_file, exports='', hazard_output_id=None, hazard_calculation_id=None): """ Run a job using the specified config file and other options. :param str cfg_file: Path to calculation config (INI-style) files. :param str log_level: 'debug', 'info', 'warn', 'error', or 'critical' :param str log_file: Path to log file. :param exports: A comma-separated string of export types requested by the user. Currently only 'xml' is supported. """ # first of all check the database version and exit if the db is outdated upgrader.check_versions(django_db.connections['admin']) with CeleryNodeMonitor(openquake.engine.no_distribute(), interval=3): job = job_from_file_lite( cfg_file, getpass.getuser(), log_level, exports, hazard_output_id=hazard_output_id, hazard_calculation_id=hazard_calculation_id) job.ds_calc_dir = datastore.DataStore(job.id).calc_dir job.save() t0 = time.time() calc = run_calc(job, log_level, log_file, exports, lite=True) expose_outputs(calc.datastore, job) duration = time.time() - t0 if job.status == 'complete': print_results(job.id, duration, list_outputs) else: sys.exit('Calculation %s failed' % job.id) return job
def run_calc(job, log_level, log_file, exports): """ Run a calculation. :param job: :class:`openquake.engine.db.model.OqJob` instance :param str log_level: The desired logging level. Valid choices are 'debug', 'info', 'progress', 'warn', 'error', and 'critical'. :param str log_file: Complete path (including file name) to file where logs will be written. If `None`, logging will just be printed to standard output. :param exports: A comma-separated string of export types. """ # let's import the calculator classes here, when they are needed # the reason is that the command `$ oq-engine --upgrade-db` # does not need them and would raise strange errors during installation # time if the PYTHONPATH is not set and commonlib is not visible from openquake.engine.calculators import calculators # first of all check the database version and exit if the db is outdated upgrader.check_versions(django_db.connections['admin']) calculator = calculators(job) with logs.handle(job, log_level, log_file), job_stats(job): # run the job _do_run_calc(calculator, exports) return calculator
def run_job_lite(cfg_files, log_level, log_file, exports=''): """ Run a job using the specified config file and other options. :param str cfg_files: Path to calculation config (INI-style) files. :param str log_level: 'debug', 'info', 'warn', 'error', or 'critical' :param str log_file: Path to log file. :param exports: A comma-separated string of export types requested by the user. Currently only 'xml' is supported. """ # first of all check the database version and exit if the db is outdated upgrader.check_versions(django_db.connections['admin']) with CeleryNodeMonitor(openquake.engine.no_distribute(), interval=3): job = job_from_files(cfg_files, getpass.getuser(), log_level, exports) t0 = time.time() run_calc(job, log_level, log_file, exports, lite=True) duration = time.time() - t0 if job.status == 'complete': print_results(job.id, duration, list_outputs) else: sys.exit('Calculation %s failed' % job.id) return job
def run_job(cfg_file, log_level, log_file, exports, hazard_output_id=None, hazard_calculation_id=None): """ Run a job using the specified config file and other options. :param str cfg_file: Path to calculation config (INI-style) file. :param str log_level: 'debug', 'info', 'warn', 'error', or 'critical' :param str log_file: Path to log file. :param list exports: A list of export types requested by the user. Currently only 'xml' is supported. :param str hazard_ouput_id: The Hazard Output ID used by the risk calculation (can be None) :param str hazard_calculation_id: The Hazard Calculation ID used by the risk calculation (can be None) """ # first of all check the database version and exit if the db is outdated upgrader.check_versions(django_db.connections['admin']) with CeleryNodeMonitor(openquake.engine.no_distribute(), interval=3): hazard = hazard_output_id is None and hazard_calculation_id is None if log_file is not None: touch_log_file(log_file) job = job_from_file( cfg_file, getpass.getuser(), log_level, exports, hazard_output_id, hazard_calculation_id) # Instantiate the calculator and run the calculation. t0 = time.time() run_calc( job, log_level, log_file, exports, 'hazard' if hazard else 'risk') duration = time.time() - t0 if hazard: if job.status == 'complete': print_results(job.hazard_calculation.id, duration, list_hazard_outputs) else: sys.exit('Calculation %s failed' % job.hazard_calculation.id) else: if job.status == 'complete': print_results(job.risk_calculation.id, duration, list_risk_outputs) else: sys.exit('Calculation %s failed' % job.risk_calculation.id)
def run_calc(job, log_level, log_file, exports, lite=False): """ Run a calculation. :param job: :class:`openquake.engine.db.model.OqJob` instance :param str log_level: The desired logging level. Valid choices are 'debug', 'info', 'progress', 'warn', 'error', and 'critical'. :param str log_file: Complete path (including file name) to file where logs will be written. If `None`, logging will just be printed to standard output. :param exports: A comma-separated string of export types. :param lite: Flag set when the oq-lite calculators are used """ # let's import the calculator classes here, when they are needed; # the reason is that the command `$ oq-engine --upgrade-db` # does not need them and would raise strange errors during installation # time if the PYTHONPATH is not set and commonlib is not visible if lite: calc_dir = os.path.join(datastore.DATADIR, 'calc_%d' % job.id) if os.path.exists(calc_dir): os.rename(calc_dir, calc_dir + '.bak') print 'Generated %s.bak' % calc_dir from openquake.commonlib.calculators import base calculator = base.calculators(job.get_oqparam(), calc_id=job.id) calculator.job = job calculator.monitor = EnginePerformanceMonitor('', job.id) else: from openquake.engine.calculators import calculators calculator = calculators(job) # first of all check the database version and exit if the db is outdated upgrader.check_versions(django_db.connections['admin']) with logs.handle(job, log_level, log_file), job_stats(job): # run the job try: _do_run_calc(calculator, exports) except: tb = traceback.format_exc() logs.LOG.critical(tb) raise return calculator
def run_calc(job, log_level, log_file, exports, lite=False): """ Run a calculation. :param job: :class:`openquake.engine.db.model.OqJob` instance :param str log_level: The desired logging level. Valid choices are 'debug', 'info', 'progress', 'warn', 'error', and 'critical'. :param str log_file: Complete path (including file name) to file where logs will be written. If `None`, logging will just be printed to standard output. :param exports: A comma-separated string of export types. :param lite: Flag set when the oq-lite calculators are used """ # let's import the calculator classes here, when they are needed # the reason is that the command `$ oq-engine --upgrade-db` # does not need them and would raise strange errors during installation # time if the PYTHONPATH is not set and commonlib is not visible if lite: from openquake.commonlib.calculators import base calculator = base.calculators(job.get_oqparam()) calculator.job = job calculator.monitor = EnginePerformanceMonitor('', job.id) else: from openquake.engine.calculators import calculators calculator = calculators(job) # first of all check the database version and exit if the db is outdated upgrader.check_versions(django_db.connections['admin']) with logs.handle(job, log_level, log_file), job_stats(job): # run the job try: _do_run_calc(calculator, exports) except: tb = traceback.format_exc() logs.LOG.critical(tb) raise return calculator
def run_calc(job, log_level, log_file, exports, job_type): """ Run a calculation. :param job: :class:`openquake.engine.db.model.OqJob` instance which references a valid :class:`openquake.engine.db.models.RiskCalculation` or :class:`openquake.engine.db.models.HazardCalculation`. :param str log_level: The desired logging level. Valid choices are 'debug', 'info', 'progress', 'warn', 'error', and 'critical'. :param str log_file: Complete path (including file name) to file where logs will be written. If `None`, logging will just be printed to standard output. :param list exports: A (potentially empty) list of export targets. Currently only "xml" is supported. :param str job_type: 'hazard' or 'risk' """ # first of all check the database version and exit if the db is outdated upgrader.check_versions(django_db.connections['admin']) calc_mode = getattr(job, '%s_calculation' % job_type).calculation_mode calculator = get_calculator_class(job_type, calc_mode)(job) calc = job.calculation # initialize log handlers handler = (LogFileHandler(job_type, calc, log_file) if log_file else LogStreamHandler(job_type, calc)) logging.root.addHandler(handler) logs.set_level(log_level) try: with job_stats(job): # run the job _do_run_calc(calculator, exports, job_type) finally: logging.root.removeHandler(handler) return calculator