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

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

Improve "config-card" file handling.
Use explicite function for "repo_io" module initialization

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