-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.py
41 lines (32 loc) · 1.08 KB
/
service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import json
from typing import Optional
from datetime import datetime
class Status(object):
STATES = [
"running",
"stopped",
"unknown",
"pending",
"disabled",
"failing"
]
def __init__(self, status):
self.status = self.__to_status(status)
def __to_status(self, status):
return status if str(status).lower() in self.STATES else 'unknown'
def __str__(self):
return self.status
class Service(object):
def __init__(self, service_id: str, name: Optional[str], host: str, port: int, status: Status,
registered_at: datetime, last_health_check: Optional[datetime] = None):
self.service_id = service_id
self.name = name
self.host = host
self.port = port
self.status = status
self.registered_at = registered_at
self.last_health_check = last_health_check
def __str__(self):
return self.__dict__
def json(self):
return json.dumps(self.__str__(), default=lambda o: o.isoformat() if isinstance(o, datetime) else str(o))