[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 | """ |
---|
| 15 | This module contains repository I/O code |
---|
| 16 | """ |
---|
| 17 | |
---|
| 18 | import sys |
---|
| 19 | |
---|
| 20 | # line below is to include Prodiguer database I/O library in the search path |
---|
| 21 | sys.path.append("/home/jripsl/snapshot/src") |
---|
| 22 | |
---|
| 23 | # import Prodiguer database I/O library |
---|
| 24 | import elixir |
---|
| 25 | import prodiguer_shared |
---|
| 26 | |
---|
[865] | 27 | |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | |
---|
[859] | 31 | |
---|
| 32 | |
---|
[871] | 33 | # --- module static initialization --- # |
---|
| 34 | |
---|
| 35 | CSTE_MODE_LOCAL_REPO="local_repo" |
---|
| 36 | CSTE_MODE_REMOTE_REPO="remote_repo" |
---|
| 37 | CSTE_MODE_REMOTE_REPO_STUB="remote_repo_stub" |
---|
| 38 | |
---|
| 39 | # set mode |
---|
| 40 | mode=CSTE_MODE_LOCAL_REPO # CSTE_MODE_LOCAL_REPO, CSTE_MODE_REMOTE_REPO, CSTE_MODE_REMOTE_REPO_STUB |
---|
| 41 | |
---|
| 42 | # set repository driver |
---|
| 43 | if mode==CSTE_MODE_REMOTE_REPO_STUB: |
---|
| 44 | import prodiguer_shared.repo.mq.hooks_stub as repo |
---|
| 45 | elif mode==CSTE_MODE_REMOTE_REPO: |
---|
| 46 | import prodiguer_shared.repo.mq.hooks as repo |
---|
| 47 | elif mode==CSTE_MODE_LOCAL_REPO: |
---|
| 48 | import local_repo as repo |
---|
| 49 | else: |
---|
| 50 | raise Exception("ERR001 - incorrect mode") |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | |
---|
[865] | 57 | # -- methods -- # |
---|
[859] | 58 | |
---|
| 59 | def 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] | 70 | def 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] | 82 | def populate_tables_with_sample(): |
---|
| 83 | repo.populate_tables_with_sample() |
---|
| 84 | |
---|
| 85 | def retrieve_simulations(): |
---|
| 86 | return repo.retrieve_simulations() |
---|
| 87 | |
---|
[859] | 88 | def test(): |
---|
[875] | 89 | """ |
---|
| 90 | not used |
---|
| 91 | """ |
---|
| 92 | |
---|
[865] | 93 | repo.create_message("test2", 2, "bla2") |
---|
[859] | 94 | commit() |
---|
| 95 | |
---|
[865] | 96 | repo.update_simulation_status('1pctCO22', 'ERROR') |
---|
[859] | 97 | commit() |
---|
| 98 | |
---|
[865] | 99 | repo.create_message("test3", 3, "bla3") |
---|
[859] | 100 | rollback() |
---|
| 101 | |
---|
[875] | 102 | def 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 | |
---|
[859] | 112 | def commit(): |
---|
[865] | 113 | if mode==CSTE_MODE_LOCAL_REPO: |
---|
[866] | 114 | repo.commit() |
---|
[865] | 115 | elif mode==CSTE_MODE_REMOTE_REPO: |
---|
[859] | 116 | elixir.session.commit() |
---|
[865] | 117 | elif mode==CSTE_MODE_REMOTE_REPO_STUB: |
---|
[859] | 118 | pass |
---|
| 119 | else: |
---|
| 120 | raise Exception("ERR002 - incorrect mode") |
---|
| 121 | |
---|
| 122 | def rollback(): |
---|
[865] | 123 | if mode==CSTE_MODE_LOCAL_REPO: |
---|
[866] | 124 | repo.rollback() |
---|
[865] | 125 | elif mode==CSTE_MODE_REMOTE_REPO: |
---|
[859] | 126 | elixir.session.rollback() |
---|
[865] | 127 | elif mode==CSTE_MODE_REMOTE_REPO_STUB: |
---|
[859] | 128 | pass |
---|
| 129 | else: |
---|
| 130 | raise Exception("ERR003 - incorrect mode") |
---|
| 131 | |
---|
| 132 | def retrieve_simulation(name): |
---|
[865] | 133 | simulation=None |
---|
[859] | 134 | |
---|
[865] | 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 | |
---|
[866] | 153 | return simulation |
---|
[865] | 154 | |
---|
[859] | 155 | def delete_simulation(name): |
---|
[865] | 156 | if mode==CSTE_MODE_LOCAL_REPO: |
---|
| 157 | repo.delete_simulation(name) |
---|
| 158 | elif mode==CSTE_MODE_REMOTE_REPO: |
---|
[859] | 159 | |
---|
[865] | 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 | |
---|
[859] | 174 | def create_simulation(simulation): |
---|
[865] | 175 | if mode==CSTE_MODE_LOCAL_REPO: |
---|
| 176 | repo.create_simulation(simulation) |
---|
| 177 | elif mode==CSTE_MODE_REMOTE_REPO: |
---|
[859] | 178 | |
---|
[865] | 179 | # prepare args |
---|
| 180 | # .. |
---|
[859] | 181 | |
---|
[865] | 182 | # execute |
---|
| 183 | repo.create_simulation(activity) |
---|
[859] | 184 | |
---|
[865] | 185 | # process return values |
---|
| 186 | # .. |
---|
[859] | 187 | |
---|
[865] | 188 | elif mode==CSTE_MODE_REMOTE_REPO_STUB: |
---|
| 189 | pass |
---|
| 190 | else: |
---|
| 191 | raise Exception("ERR016 - incorrect mode") |
---|
[859] | 192 | |
---|
[865] | 193 | def update_simulation_status(simulation): |
---|
| 194 | if mode==CSTE_MODE_LOCAL_REPO: |
---|
| 195 | repo.update_simulation_status(simulation) |
---|
| 196 | elif mode==CSTE_MODE_REMOTE_REPO: |
---|
[859] | 197 | |
---|
[865] | 198 | # prepare args |
---|
| 199 | # .. |
---|
[859] | 200 | |
---|
[865] | 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 | |
---|
| 212 | def 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 | |
---|
| 235 | def 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 | |
---|
| 254 | def 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 | |
---|
| 273 | def 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 | |
---|
[859] | 297 | # --- higher level methods --- # |
---|
| 298 | |
---|
| 299 | def 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 |
---|
[866] | 307 | |
---|
| 308 | |
---|