JobControllerPanel.py 10.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
#6, rue Jules Horowitz
#38042 Grenoble Cedex 9
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

''' 
Created on Apr 10, 2015

@author: pellegrini
'''

import collections
import cPickle
import datetime
import glob
import os
import threading
import time

import wx
import wx.lib.intctrl as intctrl

from MDANSE.Core.Platform import PLATFORM
from MDANSE.Core.Singleton import Singleton
from MDANSE.Framework.Status import convert_duration, total_seconds
from MDANSE.Framework.Jobs.JobStatus import JobState
from MDANSE.Externals.pubsub import pub
from MDANSE.Logging.Logger import LOGGER

51
from MDANSE.App.GUI.Icons import ICONS
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
from MDANSE.App.GUI.Events.JobControllerEvent import EVT_JOB_CONTROLLER, JobControllerEvent
        
class JobController(threading.Thread):
    
    __metaclass__ = Singleton
    
    def __init__(self, window, start=False):
        
        threading.Thread.__init__(self)
        
        self._window = window
        
        self._stop = threading.Event()
                                
        self._registry = collections.OrderedDict()
        
        if start:
            self.start()
        
    @property
    def registry(self):
        
        return self._registry

    def remove_job(self, job):
        
        try:
            del self._registry[job]
        except KeyError:
            pass    

    def run(self):
        
        while not self._stop.is_set():
            self.update()
            self._stop.wait(10.0)

    def stop(self):
        
        self._stop.set()
        
    def update(self):
                        
        pids = PLATFORM.get_processes_info()
                        
        # The list of the registered jobs.
        jobs = [f for f in glob.glob(os.path.join(PLATFORM.temporary_files_directory(),'*'))]
                                   
        # Loop over the job registered at the previous controller check point     
        for j in self._registry.keys():
            # Case where a job has finished during two controller check points (i.e. its temporary file has been deleted)
            
            if self._registry[j]['state'] == 'finished':
                continue
            
            if not j in jobs:
                self._registry[j]['eta'] = 'N/A'
                self._registry[j]['progress'] = 100
                self._registry[j]['state'] = 'finished'
                start = datetime.datetime.strptime(self._registry[j]["start"],"%d-%m-%Y %H:%M:%S")
                self._registry[j]['elapsed'] = '%02d:%02dh:%02dm:%02ds' % convert_duration(total_seconds(datetime.datetime.today() - start))
        
        # Loop over the job whose temporary files are still present
        for j in jobs:

            # Open the job temporary file
            try:
                f = open(j, 'rb')
                info = cPickle.load(f)
                f.close()
                
            # If the file could not be opened/unpickled for whatever reason, try at the next checkpoint
            except:
                continue

            # The job file could be opened and unpickled properly
            else:
                # Check that the unpickled object is a JobStatus object
                if not isinstance(info,JobState):
                    continue
                
                # Check that the pid of the running job corresponds to an active pid.
                running = (info['pid'] in pids)
                
                # If so, check that the corresponding pid actually corresponds to the job by comparing 
                # the job starting date and the pid creation date.
                if running:
                    jobStartingTime = datetime.datetime.strptime(info["start"],"%d-%m-%Y %H:%M:%S")
                    procStartingTime = datetime.datetime.strptime(pids[info['pid']],"%d-%m-%Y %H:%M:%S")
                    running = (jobStartingTime >= procStartingTime)
                                                    
                # Case where the job is running, update the registry with the new status    
                if running:
                    self._registry[j] = info
                    
                # Case where the job is not running 
                else:
                    info['state'] = 'aborted'
                    self._registry[j] = info          

        wx.PostEvent(self._window, JobControllerEvent(self._registry))
                                              
class JobControllerPanel(wx.ScrolledWindow):
    
    columns = [("name",(150,-1)),("pid",(60,-1)),("start",(150,-1)),("elapsed",(150,-1)),("state",(100,-1)),("progress",(-1,-1)),("eta",(120,-1)),("kill",(50,-1))]
    
    def __init__(self, parent):
        
        wx.ScrolledWindow.__init__(self, parent, id = wx.ID_ANY)

        self.SetScrollbars(pixelsPerUnitX=1, pixelsPerUnitY=1, noUnitsX=50, noUnitsY=50)
        
        self.parent = parent
                        
        self._jobs = collections.OrderedDict()
        
        self.build_panel()

        EVT_JOB_CONTROLLER(self,self.on_update)
        
        pub.subscribe(self.on_start_job,"on_start_job")

        self._jobsController = JobController(self,True)

    def __del__(self):

        self._jobsController.stop()
        while self._jobsController.is_alive():
            time.sleep(0.01)
            
    def on_start_job(self,message):
                
        self._jobsController.update()
        

    def add_job(self, name, jobStatus):
                        
        r = self._gbSizer.Rows
        self._jobs[name] = r

        name = wx.TextCtrl(self, wx.ID_ANY, style=wx.TE_READONLY|wx.ALIGN_CENTER_HORIZONTAL)
        pid = intctrl.IntCtrl(self, wx.ID_ANY, style=wx.TE_READONLY|wx.ALIGN_CENTER_HORIZONTAL)
        start = wx.TextCtrl(self, wx.ID_ANY, style=wx.TE_READONLY|wx.ALIGN_CENTER_HORIZONTAL)
        elapsed = wx.TextCtrl(self, wx.ID_ANY, style=wx.TE_READONLY|wx.ALIGN_CENTER_HORIZONTAL)
        state = wx.TextCtrl(self, wx.ID_ANY, style=wx.TE_READONLY|wx.ALIGN_CENTER_HORIZONTAL)
        progress = wx.Gauge(self, wx.ID_ANY,range=100)
        eta = wx.TextCtrl(self, wx.ID_ANY, style=wx.TE_READONLY|wx.ALIGN_CENTER_HORIZONTAL)
199
        kill = wx.BitmapButton(self, wx.ID_ANY, ICONS["stop",24,24])
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288

        self._gbSizer.Add(name    ,pos=(r,0),flag=wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL)
        self._gbSizer.Add(pid     ,pos=(r,1),flag=wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL)
        self._gbSizer.Add(start   ,pos=(r,2),flag=wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL)
        self._gbSizer.Add(elapsed ,pos=(r,3),flag=wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL)
        self._gbSizer.Add(state   ,pos=(r,4),flag=wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL)
        self._gbSizer.Add(progress,pos=(r,5),flag=wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL)
        self._gbSizer.Add(eta     ,pos=(r,6),flag=wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL)
        self._gbSizer.Add(kill    ,pos=(r,7),flag=wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL)
                                
        self._gbSizer.Layout()

        kill.Bind(wx.EVT_BUTTON, self.on_kill_job)
                        
    def build_panel(self):
        
        self._gbSizer = wx.GridBagSizer(0,0)
        
        for i,(col,s) in enumerate(self.columns):
            self._gbSizer.Add(wx.TextCtrl(self,wx.ID_ANY,value=col.upper(),size=s,style=wx.TE_READONLY|wx.ALIGN_CENTER_HORIZONTAL),pos=(0,i),flag=wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL)

        self._gbSizer.AddGrowableCol(5)
                
        self.SetSizer(self._gbSizer)

    def on_kill_job(self, event):
        
        row = self._gbSizer.GetItemPosition(event.GetEventObject())[0]
        state = self._gbSizer.FindItemAtPosition((row,4)).Window.GetValue()
        
        if state == "finished":
            return
            
        for row in range(self._gbSizer.GetCols()):
            name = self._gbSizer.FindItemAtPosition((row,0)).Window.GetValue()

        d = wx.MessageDialog(None, 'Do you really want to kill job %r ?' % name, 'Question', wx.YES_NO|wx.YES_DEFAULT|wx.ICON_EXCLAMATION)
        if d.ShowModal() == wx.ID_YES:
            try:
                pid = self._gbSizer.FindItemAtPosition((row,1)).Window.GetValue()
                PLATFORM.kill_process(pid)
            except Exception:
                LOGGER("The job %r could not be killed." % name,"error")
            else:            
                event.GetEventObject().Disable()   
                

    def update_job(self,name,jobStatus):
        
        r = self._jobs[name]
                
        for i,(name,_) in enumerate(self.columns):
            try:
                self._gbSizer.FindItemAtPosition((r,i)).Window.SetValue(jobStatus[name])
                
                if jobStatus['state'] == 'finished':
                    self._gbSizer.FindItemAtPosition((r,7)).Window.Disable()
                
            except AttributeError:
                pass
        
        info =  "\n".join(["%s = %s" % (k,v) for k,v in jobStatus.items()])
        self._gbSizer.FindItemAtPosition((r,0)).Window.SetToolTipString(info)
        
    def on_update(self, event):
                                        
        registry = event.registry
                                
        for name,jobStatus in registry.items():
                        
            if not self._jobs.has_key(name):
                if jobStatus["state"] == "aborted":
                    continue
                self.add_job(name,jobStatus)
            self.update_job(name,jobStatus)
                        
if __name__ == "__main__":
        
    app = wx.App(0)
    frame = wx.Frame(None, -1, "Job Controller")
     
    JobController = JobControllerPanel(frame)
     
    frame.Show(True)
    app.MainLoop()