source: cows_wps/trunk/process_modules/wait_for_file_deletion.py @ 7014

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows_wps/trunk/process_modules/wait_for_file_deletion.py@7014
Revision 7014, 2.6 KB checked in by astephen, 10 years ago (diff)

renamed ddp_process_support to process_support

Line 
1#!/usr/bin/env python
2#$ -S /usr/bin/python
3
4"""
5wait_for_file_deletion.py
6=============
7
8A test process that will continue running untill a file being monitored is
9deleted. This is to allow the easy testing of long running processes.
10
11"""
12
13import os
14import time
15import sys
16from datetime import datetime
17from cows_wps.process_handler.fileset import FileSet
18import cows_wps.process_handler.process_support as process_support
19from cows_wps.process_handler.context.process_status import STATUS
20
21PAUSE_BETWEEN_CHECKS = 1
22DELETE_ME_FILE_NAME = 'delete_me'
23
24class WaitForFileDeletion(object):
25   
26    def __init__(self):
27        self.filename = DELETE_ME_FILE_NAME
28   
29    def __call__(self, context):
30        print "__call__ at ", datetime.fromtimestamp(time.time()).isoformat(' ')
31        self.log = context.log
32       
33        startTime = time.time()
34       
35        context.log.info('Process Started')
36        fullPath = self._createFile(context.processDir)
37       
38        context.log.info('Waiting for %s to be deleted.' % (fullPath,))
39       
40        while self._checkFileExists(fullPath):
41            time.sleep(PAUSE_BETWEEN_CHECKS)
42           
43        context.log.info('WaitForFileDelection finished!')
44       
45        context.setStatus(STATUS.COMPLETED, "Process is complete")       
46        completionTime = time.time()
47        process_support.updateJobDetailsAfterCompletion(context, startTime, completionTime)
48
49   
50    def _checkFileExists(self, fullPath):
51        """
52        Checks that the given file exists on the filesystem.
53        """
54        return os.path.exists(fullPath)
55   
56    def _createFile(self, processDir):
57        """
58        Creates the file that this process will monitor in the directory given,
59        the file created will be left empty.
60        """
61        fullPath = os.path.join(processDir + '/' + self.filename)
62        fout = open(fullPath, 'w')
63        fout.close()
64        assert(os.path.exists(fullPath))
65        return fullPath
66
67    def dryRun(self, context):
68       
69        context.log.info('WaitForFileDelection dryrun !')
70       
71        estimatedDuration = context.inputs['EstimatedDuration']
72        context.outputs['job_details']['estimated_duration'] = estimatedDuration
73
74        context.outputs['FileSet'] = FileSet()       
75
76        context.setStatus(STATUS.ACCEPTED, "Process execution request has been accepted", 0)
77        process_support.updateJobDetailsAfterDryRun(context, estimatedDuration)
78        context.save()
79   
80    def onProcessQuit(self):
81        quitDateTime = datetime.fromtimestamp(time.time())
82        self.log.warning("onProcessQuit called at %s" % (quitDateTime.isoformat(' '),))
83        sys.stdout.flush()
Note: See TracBrowser for help on using the repository browser.