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

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

Repository I/O implementation (suite).

File size: 6.1 KB
Line 
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
27
28
29
30
31
32
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
57# -- methods -- #
58
59def init():
60        if mode==CSTE_MODE_LOCAL_REPO:
61                repo.connect()
62        elif mode==CSTE_MODE_REMOTE_REPO:
63                _CONNECTION = "postgresql://postgres:Silence107!@localhost:5432/prodiguer"
64                prodiguer_shared.connect(_CONNECTION)
65        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
66                pass
67        else:
68                raise Exception("ERR004 - incorrect mode")
69
70def free():
71        if mode==CSTE_MODE_LOCAL_REPO:
72                repo.free()
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
82def populate_tables_with_sample():
83        repo.populate_tables_with_sample()
84
85def retrieve_simulations():
86        return repo.retrieve_simulations()
87
88def test():
89        """
90        not used
91        """
92
93        repo.create_message("test2", 2, "bla2")
94        commit()
95
96        repo.update_simulation_status('1pctCO22', 'ERROR')
97        commit()
98
99        repo.create_message("test3", 3, "bla3")
100        rollback()
101
102def cleanup():
103        if mode==CSTE_MODE_LOCAL_REPO:
104                repo.cleanup()
105        elif mode==CSTE_MODE_REMOTE_REPO:
106                raise Exception("ERR707")
107        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
108                pass
109        else:
110                raise Exception("ERR007 - incorrect mode")
111
112def commit():
113        if mode==CSTE_MODE_LOCAL_REPO:
114                repo.commit()
115        elif mode==CSTE_MODE_REMOTE_REPO:
116                elixir.session.commit()
117        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
118                pass
119        else:
120                raise Exception("ERR002 - incorrect mode")
121
122def rollback():
123        if mode==CSTE_MODE_LOCAL_REPO:
124                repo.rollback()
125        elif mode==CSTE_MODE_REMOTE_REPO:
126                elixir.session.rollback()
127        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
128                pass
129        else:
130                raise Exception("ERR003 - incorrect mode")
131
132def retrieve_simulation(name):
133        simulation=None
134
135        if mode==CSTE_MODE_LOCAL_REPO:
136                simulation=repo.retrieve_simulation(name)
137        elif mode==CSTE_MODE_REMOTE_REPO:
138
139                # prepare args
140                # ..
141
142                # execute
143                s=repo.retrieve_simulation(name)
144
145                # process return values
146                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..
147
148        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
149                pass
150        else:
151                raise Exception("ERR014 - incorrect mode")
152
153        return simulation
154
155def delete_simulation(name):
156        if mode==CSTE_MODE_LOCAL_REPO:
157                repo.delete_simulation(name)
158        elif mode==CSTE_MODE_REMOTE_REPO:
159
160                # prepare args
161                # ..
162
163                # execute
164                repo.delete_simulation(name)
165
166                # process return values
167                # ..
168
169        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
170                pass
171        else:
172                raise Exception("ERR015 - incorrect mode")
173
174def create_simulation(simulation):
175        if mode==CSTE_MODE_LOCAL_REPO:
176                repo.create_simulation(simulation)
177        elif mode==CSTE_MODE_REMOTE_REPO:
178
179                # prepare args
180                # ..
181
182                # execute
183                repo.create_simulation(activity)
184
185                # process return values
186                # ..
187
188        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
189                pass
190        else:
191                raise Exception("ERR016 - incorrect mode")
192
193def update_simulation_status(simulation):
194        if mode==CSTE_MODE_LOCAL_REPO:
195                repo.update_simulation_status(simulation)
196        elif mode==CSTE_MODE_REMOTE_REPO:
197
198                # prepare args
199                # ..
200
201                # execute
202                repo.update_simulation_status(name)
203
204                # process return values
205                # ..
206
207        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
208                pass
209        else:
210                raise Exception("ERR017 - incorrect mode")
211
212def retrieve_messages(simulation):
213        message=None
214
215        if mode==CSTE_MODE_LOCAL_REPO:
216                message=repo.retrieve_messages(simulation)
217        elif mode==CSTE_MODE_REMOTE_REPO:
218
219                # prepare args
220                # ..
221
222                # execute
223                repo.retrieve_messages(name)
224
225                # process return values
226                # ..
227
228        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
229                pass
230        else:
231                raise Exception("ERR018 - incorrect mode")
232
233        return message
234
235def delete_messages(simulation):
236        if mode==CSTE_MODE_LOCAL_REPO:
237                repo.delete_messages(name)
238        elif mode==CSTE_MODE_REMOTE_REPO:
239
240                # prepare args
241                # ..
242
243                # execute
244                repo.delete_messages(name)
245
246                # process return values
247                # ..
248
249        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
250                pass
251        else:
252                raise Exception("ERR019 - incorrect mode")
253
254def create_message(message):
255        if mode==CSTE_MODE_LOCAL_REPO:
256                repo.create_message(message)
257        elif mode==CSTE_MODE_REMOTE_REPO:
258
259                # prepare args
260                # ..
261
262                # execute
263                repo.create_message()
264
265                # process return values
266                # ..
267
268        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
269                pass
270        else:
271                raise Exception("ERR020 - incorrect mode")
272
273def retrieve_last_message(simulation):
274        message=None
275
276        if mode==CSTE_MODE_LOCAL_REPO:
277                message=repo.retrieve_last_message(simulation)
278        elif mode==CSTE_MODE_REMOTE_REPO:
279
280                # prepare args
281                # ..
282
283                # execute
284                repo.retrieve_last_message(simulation)
285
286                # process return values
287                # ..
288
289        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
290                pass
291        else:
292                raise Exception("ERR021 - incorrect mode")
293
294        return message
295
296
297# --- higher level methods --- #
298
299def get_running_simulations():
300        running_simulation=[]
301
302        for s in retrieve_simulation():
303                if s.status=="running":
304                        running_simulation.append(s)
305                       
306        return running_simulation
307
308
Note: See TracBrowser for help on using the repository browser.