def testCreateTransaction(self): """ _testCreateTransaction_ Create a workflow and commit it to the database and then roll the transaction back. Use the workflow's exists() method to verify that the workflow does not exist before create() is called, exists after create() is called and does not exist after the transaction is rolled back. """ myThread = threading.currentThread() myThread.transaction.begin() testWorkflow = Workflow(spec="spec.xml", owner="Simon", name="wf001", task='Test') self.assertEqual(testWorkflow.exists(), False, "ERROR: Workflow exists before it was created") testWorkflow.create() self.assertTrue(testWorkflow.exists() > 0, "ERROR: Workflow does not exist after it has been created") myThread.transaction.rollback() self.assertEqual(testWorkflow.exists(), False, "ERROR: Workflow exists after the transaction was rolled back.") return
def testCreateDeleteExists(self): """ _testCreateDeleteExists_ Test the create(), delete() and exists() methods of the workflow class by creating and deleting a workflow. The exists() method will be called before and after creation and after deletion. """ testWorkflow = Workflow(spec="spec.xml", owner="Simon", name="wf001", task='Test', wfType="ReReco") self.assertEqual(testWorkflow.exists(), False, "ERROR: Workflow exists before it was created") testWorkflow.create() self.assertTrue(testWorkflow.exists() > 0, "ERROR: Workflow does not exist after it has been created") testWorkflow.create() testWorkflow.delete() self.assertEqual(testWorkflow.exists(), False, "ERROR: Workflow exists after it has been deleted") return
def testCreateTransaction(self): """ _testCreateTransaction_ Create a workflow and commit it to the database and then roll the transaction back. Use the workflow's exists() method to verify that the workflow does not exist before create() is called, exists after create() is called and does not exist after the transaction is rolled back. """ myThread = threading.currentThread() myThread.transaction.begin() testWorkflow = Workflow(spec="spec.xml", owner="Simon", name="wf001", task='Test') self.assertEqual(testWorkflow.exists(), False, "ERROR: Workflow exists before it was created") testWorkflow.create() self.assertTrue( testWorkflow.exists() > 0, "ERROR: Workflow does not exist after it has been created") myThread.transaction.rollback() self.assertEqual( testWorkflow.exists(), False, "ERROR: Workflow exists after the transaction was rolled back.") return
def testCreateDeleteExists(self): """ _testCreateDeleteExists_ Test the create(), delete() and exists() methods of the workflow class by creating and deleting a workflow. The exists() method will be called before and after creation and after deletion. """ testWorkflow = Workflow(spec="spec.xml", owner="Simon", name="wf001", task='Test', wfType="ReReco") self.assertEqual(testWorkflow.exists(), False, "ERROR: Workflow exists before it was created") testWorkflow.create() self.assertTrue( testWorkflow.exists() > 0, "ERROR: Workflow does not exist after it has been created") testWorkflow.create() testWorkflow.delete() self.assertEqual(testWorkflow.exists(), False, "ERROR: Workflow exists after it has been deleted") return
def killWorkflows(self, workflows): """ _killWorkflows_ Delete all the information in couch and WMBS about the given workflow, go through all subscriptions and delete one by one. The input is a dictionary with workflow names as keys, fully loaded WMWorkloads and subscriptions lists as values """ for workflow in workflows: logging.info("Deleting workflow %s" % workflow) try: #Get the task-workflow ids, sort them by ID, #higher ID first so we kill #the leaves of the tree first, root last workflowsIDs = workflows[workflow]["workflows"].keys() workflowsIDs.sort(reverse=True) #Now go through all tasks and load the WMBS workflow objects wmbsWorkflows = [] for wfID in workflowsIDs: wmbsWorkflow = Workflow(id=wfID) wmbsWorkflow.load() wmbsWorkflows.append(wmbsWorkflow) #Time to shoot one by one for wmbsWorkflow in wmbsWorkflows: if self.uploadPublishInfo: self.createAndUploadPublish(wmbsWorkflow) #Load all the associated subscriptions and shoot them one by one subIDs = workflows[workflow]["workflows"][wmbsWorkflow.id] for subID in subIDs: subscription = Subscription(id=subID) subscription['workflow'] = wmbsWorkflow subscription.load() subscription.deleteEverything() #Check that the workflow is gone if wmbsWorkflow.exists(): #Something went bad, this workflow #should be gone by now msg = "Workflow %s, Task %s was not deleted completely" % ( wmbsWorkflow.name, wmbsWorkflow.task) raise TaskArchiverPollerException(msg) #Now delete directories _, taskDir = getMasterName(startDir=self.jobCacheDir, workflow=wmbsWorkflow) logging.info("About to delete work directory %s" % taskDir) if os.path.exists(taskDir): if os.path.isdir(taskDir): shutil.rmtree(taskDir) else: # What we think of as a working directory is not a directory # This should never happen and there is no way we can recover # from this here. Bail out now and have someone look at things. msg = "Work directory is not a directory, this should never happen: %s" % taskDir raise TaskArchiverPollerException(msg) else: msg = "Attempted to delete work directory but it was already gone: %s" % taskDir logging.debug(msg) spec = workflows[workflow]["spec"] topTask = spec.getTopLevelTask()[0] # Now take care of the sandbox sandbox = getattr(topTask.data.input, 'sandbox', None) if sandbox: sandboxDir = os.path.dirname(sandbox) if os.path.isdir(sandboxDir): shutil.rmtree(sandboxDir) logging.debug("Sandbox dir deleted") else: logging.error( "Attempted to delete sandbox dir but it was already gone: %s" % sandboxDir) except Exception, ex: msg = "Critical error while deleting workflow %s\n" % workflow msg += str(ex) msg += str(traceback.format_exc()) logging.error(msg) self.sendAlert(2, msg=msg)
def killWorkflows(self, workflows): """ _killWorkflows_ Delete all the information in couch and WMBS about the given workflow, go through all subscriptions and delete one by one. The input is a dictionary with workflow names as keys, fully loaded WMWorkloads and subscriptions lists as values """ for workflow in workflows: logging.info("Deleting workflow %s" % workflow) try: #Get the task-workflow ids, sort them by ID, #higher ID first so we kill #the leaves of the tree first, root last workflowsIDs = workflows[workflow]["workflows"].keys() workflowsIDs.sort(reverse = True) #Now go through all tasks and load the WMBS workflow objects wmbsWorkflows = [] for wfID in workflowsIDs: wmbsWorkflow = Workflow(id = wfID) wmbsWorkflow.load() wmbsWorkflows.append(wmbsWorkflow) #Time to shoot one by one for wmbsWorkflow in wmbsWorkflows: if self.uploadPublishInfo: self.createAndUploadPublish(wmbsWorkflow) #Load all the associated subscriptions and shoot them one by one subIDs = workflows[workflow]["workflows"][wmbsWorkflow.id] for subID in subIDs: subscription = Subscription(id = subID) subscription['workflow'] = wmbsWorkflow subscription.load() subscription.deleteEverything() #Check that the workflow is gone if wmbsWorkflow.exists(): #Something went bad, this workflow #should be gone by now msg = "Workflow %s, Task %s was not deleted completely" % (wmbsWorkflow.name, wmbsWorkflow.task) raise TaskArchiverPollerException(msg) #Now delete directories _, taskDir = getMasterName(startDir = self.jobCacheDir, workflow = wmbsWorkflow) logging.info("About to delete work directory %s" % taskDir) if os.path.exists(taskDir): if os.path.isdir(taskDir): shutil.rmtree(taskDir) else: # What we think of as a working directory is not a directory # This should never happen and there is no way we can recover # from this here. Bail out now and have someone look at things. msg = "Work directory is not a directory, this should never happen: %s" % taskDir raise TaskArchiverPollerException(msg) else: msg = "Attempted to delete work directory but it was already gone: %s" % taskDir logging.debug(msg) spec = workflows[workflow]["spec"] topTask = spec.getTopLevelTask()[0] # Now take care of the sandbox sandbox = getattr(topTask.data.input, 'sandbox', None) if sandbox: sandboxDir = os.path.dirname(sandbox) if os.path.isdir(sandboxDir): shutil.rmtree(sandboxDir) logging.debug("Sandbox dir deleted") else: logging.error("Attempted to delete sandbox dir but it was already gone: %s" % sandboxDir) except Exception, ex: msg = "Critical error while deleting workflow %s\n" % workflow msg += str(ex) msg += str(traceback.format_exc()) logging.error(msg) self.sendAlert(2, msg = msg)