source: trunk/Monitoring/smon/repo_io.py @ 877

Last change on this file since 877 was 877, checked in by jripsl, 11 years ago

Fix bug (incorrect simulation_id was used in message table).

File size: 6.2 KB
RevLine 
[859]1# -*- coding: ISO-8859-1 -*-
2
3##################################
4#  @program        smon
5#  @description    simulation monitor
6#  @copyright      Copyright “(c)2009 Centre National de la Recherche Scientifique CNRS.
7#                             All Rights Reserved”
8#  @svn_file       $Id: repo_io.py 2599 2013-03-24 19:01:23Z jripsl $
9#  @version        $Rev: 2599 $
10#  @lastrevision   $Date: 2013-03-24 20:01:23 +0100 (Sun, 24 Mar 2013) $
11#  @license        CeCILL (http://dods.ipsl.jussieu.fr/jripsl/smon/LICENSE)
12##################################
13
14"""
15This module contains repository I/O code
16"""
17
18import sys
19
20# line below is to include Prodiguer database I/O library in the search path
21sys.path.append("/home/jripsl/snapshot/src")
22
23# import Prodiguer database I/O library
24import elixir
25import prodiguer_shared
26
[865]27
28
29
30
[859]31
32
[871]33# --- module static initialization --- #
34
35CSTE_MODE_LOCAL_REPO="local_repo"
36CSTE_MODE_REMOTE_REPO="remote_repo"
37CSTE_MODE_REMOTE_REPO_STUB="remote_repo_stub"
38
39# set mode
40mode=CSTE_MODE_LOCAL_REPO # CSTE_MODE_LOCAL_REPO, CSTE_MODE_REMOTE_REPO, CSTE_MODE_REMOTE_REPO_STUB
41
42# set repository driver
43if mode==CSTE_MODE_REMOTE_REPO_STUB:
44        import prodiguer_shared.repo.mq.hooks_stub as repo
45elif mode==CSTE_MODE_REMOTE_REPO:
46        import prodiguer_shared.repo.mq.hooks as repo
47elif mode==CSTE_MODE_LOCAL_REPO:
48        import local_repo as repo
49else:
50        raise Exception("ERR001 - incorrect mode")
51
52
53
54
55
56
[865]57# -- methods -- #
[859]58
59def init():
[865]60        if mode==CSTE_MODE_LOCAL_REPO:
[866]61                repo.connect()
[865]62        elif mode==CSTE_MODE_REMOTE_REPO:
63                _CONNECTION = "postgresql://postgres:Silence107!@localhost:5432/prodiguer"
[859]64                prodiguer_shared.connect(_CONNECTION)
[865]65        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
[859]66                pass
67        else:
68                raise Exception("ERR004 - incorrect mode")
69
[865]70def free():
71        if mode==CSTE_MODE_LOCAL_REPO:
[871]72                repo.free()
[865]73        elif mode==CSTE_MODE_REMOTE_REPO:
74
75                #prodiguer_shared.close()
76                pass
77        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
78                pass
79        else:
80                raise Exception("ERR009 - incorrect mode")
81
[871]82def populate_tables_with_sample():
[877]83        """
84        used only by "repo-state" pgm
85        """
86       
[871]87        repo.populate_tables_with_sample()
88
89def retrieve_simulations():
[877]90        """
91        used by get_running_simulations
92        """
[871]93        return repo.retrieve_simulations()
94
[859]95def test():
[875]96        """
97        not used
98        """
99
[865]100        repo.create_message("test2", 2, "bla2")
[859]101        commit()
102
[865]103        repo.update_simulation_status('1pctCO22', 'ERROR')
[859]104        commit()
105
[865]106        repo.create_message("test3", 3, "bla3")
[859]107        rollback()
108
[875]109def cleanup():
110        if mode==CSTE_MODE_LOCAL_REPO:
111                repo.cleanup()
112        elif mode==CSTE_MODE_REMOTE_REPO:
113                raise Exception("ERR707")
114        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
115                pass
116        else:
117                raise Exception("ERR007 - incorrect mode")
118
[859]119def commit():
[865]120        if mode==CSTE_MODE_LOCAL_REPO:
[866]121                repo.commit()
[865]122        elif mode==CSTE_MODE_REMOTE_REPO:
[859]123                elixir.session.commit()
[865]124        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
[859]125                pass
126        else:
127                raise Exception("ERR002 - incorrect mode")
128
129def rollback():
[865]130        if mode==CSTE_MODE_LOCAL_REPO:
[866]131                repo.rollback()
[865]132        elif mode==CSTE_MODE_REMOTE_REPO:
[859]133                elixir.session.rollback()
[865]134        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
[859]135                pass
136        else:
137                raise Exception("ERR003 - incorrect mode")
138
139def retrieve_simulation(name):
[865]140        simulation=None
[859]141
[865]142        if mode==CSTE_MODE_LOCAL_REPO:
143                simulation=repo.retrieve_simulation(name)
144        elif mode==CSTE_MODE_REMOTE_REPO:
145
146                # prepare args
147                # ..
148
149                # execute
150                s=repo.retrieve_simulation(name)
151
152                # process return values
153                simulation=smon.types.Simulation(exec_start_date=s.ExecutionStartDate,exec_end_date=s.ExecutionEndDate,status=s.ExecutionState) # ExecutionState example: EXECUTION_STATE_RUNNING, EXECUTION_STATE_SET..
154
155        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
156                pass
157        else:
158                raise Exception("ERR014 - incorrect mode")
159
[866]160        return simulation
[865]161
[859]162def delete_simulation(name):
[865]163        if mode==CSTE_MODE_LOCAL_REPO:
164                repo.delete_simulation(name)
165        elif mode==CSTE_MODE_REMOTE_REPO:
[859]166
[865]167                # prepare args
168                # ..
169
170                # execute
171                repo.delete_simulation(name)
172
173                # process return values
174                # ..
175
176        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
177                pass
178        else:
179                raise Exception("ERR015 - incorrect mode")
180
[859]181def create_simulation(simulation):
[865]182        if mode==CSTE_MODE_LOCAL_REPO:
183                repo.create_simulation(simulation)
184        elif mode==CSTE_MODE_REMOTE_REPO:
[859]185
[865]186                # prepare args
187                # ..
[859]188
[865]189                # execute
190                repo.create_simulation(activity)
[859]191
[865]192                # process return values
193                # ..
[859]194
[865]195        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
196                pass
197        else:
198                raise Exception("ERR016 - incorrect mode")
[859]199
[865]200def update_simulation_status(simulation):
201        if mode==CSTE_MODE_LOCAL_REPO:
202                repo.update_simulation_status(simulation)
203        elif mode==CSTE_MODE_REMOTE_REPO:
[859]204
[865]205                # prepare args
206                # ..
[859]207
[865]208                # execute
209                repo.update_simulation_status(name)
210
211                # process return values
212                # ..
213
214        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
215                pass
216        else:
217                raise Exception("ERR017 - incorrect mode")
218
219def retrieve_messages(simulation):
220        message=None
221
222        if mode==CSTE_MODE_LOCAL_REPO:
223                message=repo.retrieve_messages(simulation)
224        elif mode==CSTE_MODE_REMOTE_REPO:
225
226                # prepare args
227                # ..
228
229                # execute
230                repo.retrieve_messages(name)
231
232                # process return values
233                # ..
234
235        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
236                pass
237        else:
238                raise Exception("ERR018 - incorrect mode")
239
240        return message
241
242def delete_messages(simulation):
243        if mode==CSTE_MODE_LOCAL_REPO:
244                repo.delete_messages(name)
245        elif mode==CSTE_MODE_REMOTE_REPO:
246
247                # prepare args
248                # ..
249
250                # execute
251                repo.delete_messages(name)
252
253                # process return values
254                # ..
255
256        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
257                pass
258        else:
259                raise Exception("ERR019 - incorrect mode")
260
[877]261def create_message(message,simulation):
[865]262        if mode==CSTE_MODE_LOCAL_REPO:
[877]263                repo.create_message(message,simulation)
[865]264        elif mode==CSTE_MODE_REMOTE_REPO:
265
266                # prepare args
267                # ..
268
269                # execute
270                repo.create_message()
271
272                # process return values
273                # ..
274
275        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
276                pass
277        else:
278                raise Exception("ERR020 - incorrect mode")
279
280def retrieve_last_message(simulation):
281        message=None
282
283        if mode==CSTE_MODE_LOCAL_REPO:
284                message=repo.retrieve_last_message(simulation)
285        elif mode==CSTE_MODE_REMOTE_REPO:
286
287                # prepare args
288                # ..
289
290                # execute
291                repo.retrieve_last_message(simulation)
292
293                # process return values
294                # ..
295
296        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
297                pass
298        else:
299                raise Exception("ERR021 - incorrect mode")
300
301        return message
302
303
[859]304# --- higher level methods --- #
305
306def get_running_simulations():
307        running_simulation=[]
308
[876]309        for s in retrieve_simulations():
[859]310                if s.status=="running":
311                        running_simulation.append(s)
312                       
313        return running_simulation
[866]314
315
Note: See TracBrowser for help on using the repository browser.