|
12 | 12 | import os
|
13 | 13 | import sys
|
14 | 14 | import json
|
| 15 | +import time |
15 | 16 |
|
16 | 17 | from types import SimpleNamespace
|
17 | 18 | from multiprocessing import Lock
|
@@ -197,19 +198,48 @@ def update_status_record(uuid, data):
|
197 | 198 | session.commit()
|
198 | 199 |
|
199 | 200 |
|
| 201 | +def force_failed_status(uuid): |
| 202 | + with current_session as session: |
| 203 | + r = session.query(StatusRecord).filter_by(uuid=str(uuid)) |
| 204 | + if r.count() < 1: |
| 205 | + return |
| 206 | + status_record = r.one() |
| 207 | + data = json.loads(status_record.data.decode("utf-8")) |
| 208 | + data['status'] = { |
| 209 | + "status": "failed", |
| 210 | + "time": time.strftime('%Y-%m-%dT%H:%M:%SZ', time.localtime()), |
| 211 | + "message": "Process has crashed" |
| 212 | + } |
| 213 | + status_record.timestamp = datetime.datetime.now() |
| 214 | + status_record.data = json.dumps(data).encode("utf-8") |
| 215 | + session.commit() |
| 216 | + |
| 217 | + |
200 | 218 | # Get store instance data from uuid
|
201 | 219 | def get_status_record(uuid):
|
202 | 220 | with current_session as session:
|
| 221 | + if sys.platform == "linux": |
| 222 | + # Check if the current process is fail |
| 223 | + r = session.query(ProcessInstance).filter_by(uuid=str(uuid)) |
| 224 | + # If no process instance is found then there is no status records |
| 225 | + if r.count() < 1: |
| 226 | + return None |
| 227 | + process_record = r.one() |
| 228 | + if process_record.status not in {WPS_STATUS.FAILED, WPS_STATUS.SUCCEEDED}: |
| 229 | + if not os.path.exists(os.path.join("/proc", str(process_record.pid))): |
| 230 | + store_status(process_record.uuid, WPS_STATUS.FAILED, "Process crashed", 100) |
| 231 | + force_failed_status(process_record.uuid) |
| 232 | + |
203 | 233 | r = session.query(StatusRecord).filter_by(uuid=str(uuid))
|
204 |
| - if r.count(): |
205 |
| - status_record = r.one() |
206 |
| - # Ensure new item to avoid change in database |
207 |
| - # FIXME: There is a better solution ? |
208 |
| - attrs = ["uuid", "timestamp", "data"] |
209 |
| - status_record = SimpleNamespace(**{k: getattr(status_record, k) for k in attrs}) |
210 |
| - status_record.data = json.loads(status_record.data.decode("utf-8")) |
211 |
| - return status_record |
212 |
| - return None |
| 234 | + if r.count() < 1: |
| 235 | + return None |
| 236 | + status_record = r.one() |
| 237 | + # Ensure new item to avoid change in database |
| 238 | + # FIXME: There is a better solution ? |
| 239 | + attrs = ["uuid", "timestamp", "data"] |
| 240 | + status_record = SimpleNamespace(**{k: getattr(status_record, k) for k in attrs}) |
| 241 | + status_record.data = json.loads(status_record.data.decode("utf-8")) |
| 242 | + return status_record |
213 | 243 |
|
214 | 244 |
|
215 | 245 | def update_pid(uuid, pid):
|
|
0 commit comments