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

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

Add demo scenarios.

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