-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
source/jormungandr/jormungandr/interfaces/v1/backends_status.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from jormungandr import i_manager | ||
from jormungandr.protobuf_to_dict import protobuf_to_dict | ||
from jormungandr.interfaces.v1.StatedResource import StatedResource | ||
from navitiacommon import type_pb2, request_pb2 | ||
from jormungandr import exceptions | ||
from collections import defaultdict | ||
import gevent, gevent.pool | ||
from jormungandr import app | ||
|
||
|
||
class BackendsStatus(StatedResource): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(self, *args, **kwargs) | ||
|
||
def get(self): | ||
regions = i_manager.get_regions() | ||
|
||
response = defaultdict(list) | ||
|
||
pool = gevent.pool.Pool(app.config.get('BACKENDS_STATUS_GREENLET_POOL_SIZE', 10)) | ||
|
||
def do(instance_name, pt_planner_type, pt_planner, req): | ||
try: | ||
resp = pt_planner.send_and_receive(req) | ||
raw_resp_dict = protobuf_to_dict(resp, use_enum_labels=True) | ||
return instance_name, pt_planner_type, raw_resp_dict, None | ||
except exceptions.DeadSocketException as e: | ||
return ( | ||
instance_name, | ||
pt_planner_type, | ||
None, | ||
'instance {} backend {} did not respond because: {}'.format( | ||
instance_name, pt_planner_type, str(e) | ||
), | ||
) | ||
|
||
futures = [] | ||
|
||
req = request_pb2.Request() | ||
req.requested_api = type_pb2.STATUS | ||
|
||
for key_region in regions: | ||
instance = i_manager.instances[key_region] | ||
for pt_planner_type, pt_planner in instance.get_all_pt_planners(): | ||
futures.append(pool.spawn(do, instance.name, pt_planner_type, pt_planner, req)) | ||
|
||
found_err = False | ||
status_code = 200 | ||
|
||
for future in gevent.iwait(futures): | ||
instance_name, pt_planner_type, res, err = future.get() | ||
found_err |= err is not None | ||
response[pt_planner_type].append( | ||
{ | ||
instance_name: res, | ||
'err': err, | ||
} | ||
) | ||
if found_err: | ||
status_code = 503 | ||
|
||
return response, status_code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters