def test_invalid_object(): """Ensure there is an error if an object with an unknown state type is given. """ with pytest.raises(KeyError): state.deserialize_state({state.LABEL_STATE_TYPE: 'unknown'}) with pytest.raises(ValueError): state.deserialize_state({ state.LABEL_STATE_TYPE: 'unknown', state.LABEL_CREATED_AT: CREATED_AT })
def callback_function(result, lock, tasks, service): """Callback function for executed tasks.Removes the task from the task index and updates the run state in the underlying database. Parameters ---------- result: (string, dict) Tuple of task identifier and serialized state of the workflow run lock: multiprocessing.Lock Lock for concurrency control tasks: dict Task index of the backend service: contextlib,contextmanager Context manager to create an instance of the service API. """ run_id, rundir, state_dict = result logging.info('finished run {} with {}'.format(run_id, state_dict)) with lock: if run_id in tasks: # Close the pool and remove the entry from the task index pool, _ = tasks[run_id] pool.close() del tasks[run_id] state = serialize.deserialize_state(state_dict) try: with service() as api: api.runs().update_run(run_id=run_id, state=state, rundir=rundir) except Exception as ex: logging.error(ex) logging.debug('\n'.join(util.stacktrace(ex)))
def test_running_state(): """Test serialization/deserialization of running states.""" s = state.StateRunning(created_at=util.to_datetime(CREATED_AT), started_at=util.to_datetime(STARTED_AT)) s = state.deserialize_state(state.serialize_state(s)) assert s.is_running() validate_date(s.created_at, util.to_datetime(CREATED_AT)) validate_date(s.started_at, util.to_datetime(STARTED_AT))
def test_error_state(): """Test serialization/deserialization of error states.""" s = state.StateError(created_at=util.to_datetime(CREATED_AT), started_at=util.to_datetime(STARTED_AT), stopped_at=util.to_datetime(FINISHED_AT), messages=['there', 'were', 'errors']) s = state.deserialize_state(state.serialize_state(s)) assert s.is_error() assert s.messages == ['there', 'were', 'errors'] validate_date(s.created_at, util.to_datetime(CREATED_AT)) validate_date(s.started_at, util.to_datetime(STARTED_AT)) validate_date(s.stopped_at, util.to_datetime(FINISHED_AT))
def test_success_state(tmpdir): """Test serialization/deserialization of success states.""" filename = os.path.join(str(tmpdir), 'results.json') util.write_object(filename=filename, obj={'A': 1}) s = state.StateSuccess(created_at=util.to_datetime(CREATED_AT), started_at=util.to_datetime(STARTED_AT), finished_at=util.to_datetime(FINISHED_AT), files=['myfile1', 'myfile2']) s = state.deserialize_state(state.serialize_state(s)) assert s.is_success() assert len(s.files) == 2 assert 'myfile1' in s.files assert 'myfile2' in s.files validate_date(s.created_at, util.to_datetime(CREATED_AT)) validate_date(s.started_at, util.to_datetime(STARTED_AT)) validate_date(s.finished_at, util.to_datetime(FINISHED_AT))
def exec_workflow( self, run: RunObject, template: WorkflowTemplate, arguments: Dict, config: Optional[Dict] = None) -> Tuple[WorkflowState, str]: """Initiate the execution of a given workflow template for a set of argument values. This will start a new process that executes a serial workflow asynchronously. The serial workflow engine executes workflows on the local machine and therefore uses the file system to store temporary run files. The path to the run folder is returned as the second value in the result tuple. The first value in the result tuple is the state of the workflow after the process is stated. If the workflow is executed asynchronously the state will be RUNNING. Otherwise, the run state should be an inactive state. The set of arguments is not further validated. It is assumed that the validation has been performed by the calling code (e.g., the run service manager). The optional configuration object can be used to override the worker configuration that was provided at object instantiation. Expects a dictionary with an element `workers` that contains a mapping of container identifier to a container worker configuration object. If the state of the run handle is not pending, an error is raised. Parameters ---------- run: flowserv.model.base.RunObject Handle for the run that is being executed. template: flowserv.model.template.base.WorkflowTemplate Workflow template containing the parameterized specification and the parameter declarations. arguments: dict Dictionary of argument values for parameters in the template. config: dict, default=None Optional object to overwrite the worker configuration settings. Returns ------- flowserv.model.workflow.state.WorkflowState, string Raises ------ flowserv.error.DuplicateRunError """ # Get the run state. Ensure that the run is in pending state if not run.is_pending(): raise RuntimeError("invalid run state '{}'".format(run.state)) state = run.state() rundir = os.path.join(self.runsdir, run.run_id) # Get the worker configuration. worker_config = self.worker_config if not config else config.get( 'workers') # Get the source directory for static workflow files. sourcedir = self.fs.workflow_staticdir(run.workflow.workflow_id) # Get the list of workflow steps and the generated output files. steps, run_args, outputs = parser.parse_template(template=template, arguments=arguments) try: # Copy template files to the run folder. self.fs.copy_folder(key=sourcedir, dst=rundir) # Store any given file arguments in the run folder. for key, para in template.parameters.items(): if para.is_file() and key in arguments: file = arguments[key] file.source().store(os.path.join(rundir, file.target())) # Create top-level folder for all expected result files. util.create_directories(basedir=rundir, files=outputs) # Start a new process to run the workflow. Make sure to catch all # exceptions to set the run state properly state = state.start() if self.is_async: # Raise an error if the service manager is not given. if self.service is None: raise ValueError('service manager not given') # Run steps asynchronously in a separate process pool = Pool(processes=1) task_callback_function = partial(callback_function, lock=self.lock, tasks=self.tasks, service=self.service) with self.lock: self.tasks[run.run_id] = (pool, state) pool.apply_async(run_workflow, args=(run.run_id, rundir, state, outputs, steps, run_args, WorkerFactory(config=worker_config)), callback=task_callback_function) return state, rundir else: # Run steps synchronously and block the controller until done _, _, state_dict = run_workflow( run_id=run.run_id, rundir=rundir, state=state, output_files=outputs, steps=steps, arguments=run_args, workers=WorkerFactory(config=worker_config)) return serialize.deserialize_state(state_dict), rundir except Exception as ex: # Set the workflow runinto an ERROR state logging.error(ex) return state.error(messages=util.stacktrace(ex)), rundir
def test_pending_state(): """Test serialization/deserialization of pending states.""" s = state.StatePending(created_at=util.to_datetime(CREATED_AT)) s = state.deserialize_state(state.serialize_state(s)) assert s.is_pending() validate_date(s.created_at, util.to_datetime(CREATED_AT))
def exec_workflow( self, run: RunObject, template: WorkflowTemplate, arguments: Dict, staticfs: StorageVolume, config: Optional[Dict] = None ) -> Tuple[WorkflowState, StorageVolume]: """Initiate the execution of a given workflow template for a set of argument values. This will start a new process that executes a serial workflow asynchronously. The serial workflow engine executes workflows on the local machine and therefore uses the file system to store temporary run files. The path to the run folder is returned as the second value in the result tuple. The first value in the result tuple is the state of the workflow after the process is stated. If the workflow is executed asynchronously the state will be RUNNING. Otherwise, the run state should be an inactive state. The set of arguments is not further validated. It is assumed that the validation has been performed by the calling code (e.g., the run service manager). The optional configuration object can be used to override the worker configuration that was provided at object instantiation. Expects a dictionary with an element `workers` that contains a mapping of container identifier to a container worker configuration object. If the state of the run handle is not pending, an error is raised. Parameters ---------- run: flowserv.model.base.RunObject Handle for the run that is being executed. template: flowserv.model.template.base.WorkflowTemplate Workflow template containing the parameterized specification and the parameter declarations. arguments: dict Dictionary of argument values for parameters in the template. staticfs: flowserv.volume.base.StorageVolume Storage volume that contains the static files from the workflow template. config: dict, default=None Optional object to overwrite the worker configuration settings. Returns ------- flowserv.model.workflow.state.WorkflowState, flowserv.volume.base.StorageVolume """ # Get the run state. Raise an error if the run is not in pending state. if not run.is_pending(): raise RuntimeError("invalid run state '{}'".format(run.state)) state = run.state() # Create configuration dictionary that merges the engine global # configuration with the workflow-specific one. run_config = self.config if self.config is not None else dict() if config: run_config.update(config) # Get the list of workflow steps, run arguments, and the list of output # files that the workflow is expected to generate. steps, run_args, outputs = parser.parse_template(template=template, arguments=arguments) # Create and prepare storage volume for run files. runstore = self.fs.get_store_for_folder(key=util.join( self.runsdir, run.run_id), identifier=DEFAULT_STORE) try: # Copy template files to the run folder. files = staticfs.copy(src=None, store=runstore) # Store any given file arguments and additional input files # that are required by actor parameters into the run folder. for key, para in template.parameters.items(): if para.is_file() and key in arguments: for key in arguments[key].copy(target=runstore): files.append(key) elif para.is_actor() and key in arguments: input_files = arguments[key].files for f in input_files if input_files else []: for key in f.copy(target=runstore): files.append(key) # Create factory objects for storage volumes. volumes = volume_manager(specs=run_config.get('volumes', []), runstore=runstore, runfiles=files) # Create factory for workers. Include mapping of workflow steps to # the worker that are responsible for their execution. workers = WorkerPool(workers=run_config.get('workers', []), managers={ doc['step']: doc['worker'] for doc in run_config.get('workflow', []) }) # Start a new process to run the workflow. Make sure to catch all # exceptions to set the run state properly. state = state.start() if self.is_async: # Run steps asynchronously in a separate process pool = Pool(processes=1) task_callback_function = partial(callback_function, lock=self.lock, tasks=self.tasks, service=self.service) with self.lock: self.tasks[run.run_id] = (pool, state) pool.apply_async(run_workflow, args=(run.run_id, state, outputs, steps, run_args, volumes, workers), callback=task_callback_function) return state, runstore else: # Run steps synchronously and block the controller until done _, _, state_dict = run_workflow(run_id=run.run_id, state=state, output_files=outputs, steps=steps, arguments=run_args, volumes=volumes, workers=workers) return serialize.deserialize_state(state_dict), runstore except Exception as ex: # Set the workflow run into an ERROR state logging.error(ex, exc_info=True) return state.error(messages=util.stacktrace(ex)), runstore