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

Last change on this file since 865 was 865, checked in by jripsl, 11 years ago
  • repository I/O impl.
File size: 5.7 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
30CSTE_MODE_LOCAL_REPO="local_repo"
31CSTE_MODE_REMOTE_REPO="remote_repo"
32CSTE_MODE_REMOTE_REPO_STUB="remote_repo_stub"
33
34# set mode
35mode=CSTE_MODE_LOCAL_REPO # CSTE_MODE_LOCAL_REPO, CSTE_MODE_REMOTE_REPO, CSTE_MODE_REMOTE_REPO_STUB
36
37# set repository driver
38if mode==CSTE_MODE_REMOTE_REPO_STUB:
39        import prodiguer_shared.repo.mq.hooks_stub as repo
40elif mode==CSTE_MODE_REMOTE_REPO:
41        import prodiguer_shared.repo.mq.hooks as repo
42elif mode==CSTE_MODE_LOCAL_REPO:
43        import local_repo as repo
44else:
45        raise Exception("ERR001 - incorrect mode")
46
47
48
49
50# -- methods -- #
51
52def init():
53        if mode==CSTE_MODE_LOCAL_REPO:
54                local_repo.connect()
55        elif mode==CSTE_MODE_REMOTE_REPO:
56                _CONNECTION = "postgresql://postgres:Silence107!@localhost:5432/prodiguer"
57                prodiguer_shared.connect(_CONNECTION)
58        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
59                pass
60        else:
61                raise Exception("ERR004 - incorrect mode")
62
63def free():
64        if mode==CSTE_MODE_LOCAL_REPO:
65                local_repo.close()
66        elif mode==CSTE_MODE_REMOTE_REPO:
67
68                #prodiguer_shared.close()
69                pass
70        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
71                pass
72        else:
73                raise Exception("ERR009 - incorrect mode")
74
75def test():
76        repo.create_message("test2", 2, "bla2")
77        commit()
78
79        repo.update_simulation_status('1pctCO22', 'ERROR')
80        commit()
81
82        repo.create_message("test3", 3, "bla3")
83        rollback()
84
85def commit():
86        if mode==CSTE_MODE_LOCAL_REPO:
87                local_repo.commit()
88        elif mode==CSTE_MODE_REMOTE_REPO:
89                elixir.session.commit()
90        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
91                pass
92        else:
93                raise Exception("ERR002 - incorrect mode")
94
95def rollback():
96        if mode==CSTE_MODE_LOCAL_REPO:
97                local_repo.rollback()
98        elif mode==CSTE_MODE_REMOTE_REPO:
99                elixir.session.rollback()
100        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
101                pass
102        else:
103                raise Exception("ERR003 - incorrect mode")
104
105def retrieve_simulation(name):
106        simulation=None
107
108        if mode==CSTE_MODE_LOCAL_REPO:
109                simulation=repo.retrieve_simulation(name)
110        elif mode==CSTE_MODE_REMOTE_REPO:
111
112                # prepare args
113                # ..
114
115                # execute
116                s=repo.retrieve_simulation(name)
117
118                # process return values
119                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..
120
121        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
122                pass
123        else:
124                raise Exception("ERR014 - incorrect mode")
125
126    return simulation
127
128def delete_simulation(name):
129        if mode==CSTE_MODE_LOCAL_REPO:
130                repo.delete_simulation(name)
131        elif mode==CSTE_MODE_REMOTE_REPO:
132
133                # prepare args
134                # ..
135
136                # execute
137                repo.delete_simulation(name)
138
139                # process return values
140                # ..
141
142        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
143                pass
144        else:
145                raise Exception("ERR015 - incorrect mode")
146
147def create_simulation(simulation):
148        if mode==CSTE_MODE_LOCAL_REPO:
149                repo.create_simulation(simulation)
150        elif mode==CSTE_MODE_REMOTE_REPO:
151
152                # prepare args
153                # ..
154
155                # execute
156                repo.create_simulation(activity)
157
158                # process return values
159                # ..
160
161        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
162                pass
163        else:
164                raise Exception("ERR016 - incorrect mode")
165
166def update_simulation_status(simulation):
167        if mode==CSTE_MODE_LOCAL_REPO:
168                repo.update_simulation_status(simulation)
169        elif mode==CSTE_MODE_REMOTE_REPO:
170
171                # prepare args
172                # ..
173
174                # execute
175                repo.update_simulation_status(name)
176
177                # process return values
178                # ..
179
180        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
181                pass
182        else:
183                raise Exception("ERR017 - incorrect mode")
184
185def retrieve_messages(simulation):
186        message=None
187
188        if mode==CSTE_MODE_LOCAL_REPO:
189                message=repo.retrieve_messages(simulation)
190        elif mode==CSTE_MODE_REMOTE_REPO:
191
192                # prepare args
193                # ..
194
195                # execute
196                repo.retrieve_messages(name)
197
198                # process return values
199                # ..
200
201        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
202                pass
203        else:
204                raise Exception("ERR018 - incorrect mode")
205
206        return message
207
208def delete_messages(simulation):
209        if mode==CSTE_MODE_LOCAL_REPO:
210                repo.delete_messages(name)
211        elif mode==CSTE_MODE_REMOTE_REPO:
212
213                # prepare args
214                # ..
215
216                # execute
217                repo.delete_messages(name)
218
219                # process return values
220                # ..
221
222        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
223                pass
224        else:
225                raise Exception("ERR019 - incorrect mode")
226
227def create_message(message):
228        if mode==CSTE_MODE_LOCAL_REPO:
229                repo.create_message(message)
230        elif mode==CSTE_MODE_REMOTE_REPO:
231
232                # prepare args
233                # ..
234
235                # execute
236                repo.create_message()
237
238                # process return values
239                # ..
240
241        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
242                pass
243        else:
244                raise Exception("ERR020 - incorrect mode")
245
246def retrieve_last_message(simulation):
247        message=None
248
249        if mode==CSTE_MODE_LOCAL_REPO:
250                message=repo.retrieve_last_message(simulation)
251        elif mode==CSTE_MODE_REMOTE_REPO:
252
253                # prepare args
254                # ..
255
256                # execute
257                repo.retrieve_last_message(simulation)
258
259                # process return values
260                # ..
261
262        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
263                pass
264        else:
265                raise Exception("ERR021 - incorrect mode")
266
267        return message
268
269
270# --- higher level methods --- #
271
272def get_running_simulations():
273        running_simulation=[]
274
275        for s in retrieve_simulation():
276                if s.status=="running":
277                        running_simulation.append(s)
278                       
279        return running_simulation
Note: See TracBrowser for help on using the repository browser.