Skip to content
This repository has been archived by the owner on Apr 16, 2021. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
rvennam987 committed Nov 16, 2016
2 parents 602795e + 486ac0a commit 9d7de3c
Show file tree
Hide file tree
Showing 7 changed files with 6 additions and 81 deletions.
7 changes: 0 additions & 7 deletions .env.local
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
export ERP_SERVICE=http://0.0.0.0:3000
export SMTP_USER_NAME=XXX
export SMTP_PASSWORD=XXX
export SMTP_SERVER=smtp.gmail.com
export SMTP_SERVER_PORT=587
export SD_URL=https://servicediscovery.ng.bluemix.net
export SD_AUTH=XXX
export SD_STATUS=OFF
2 changes: 1 addition & 1 deletion bin/start_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def start_app():
app = create_app()
app.run(host='0.0.0.0', port=port)
except APIException as e:
print ("Application failed to register with Service Discovery")
print ("Application failed to start")


if __name__ == "__main__":
Expand Down
7 changes: 0 additions & 7 deletions manifest.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
---
declared-services:
lw-service-discovery:
label: service_discovery
plan: free
applications:
- path: .
memory: 256M
Expand All @@ -13,6 +9,3 @@ applications:
env:
LOGISTICS_WIZARD_ENV: PROD
ERP_SERVICE: XXX
SD_STATUS: OFF
services:
- lw-service-discovery
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ requests==2.10.0
PyJWT==1.4.0
decorator==4.0.4
cf-deployment-tracker==1.0.2
bluemix-service-discovery==0.0.1
1 change: 0 additions & 1 deletion server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
class Config(object):

ENVIRONMENT = env.get('LOGISTICS_WIZARD_ENV', 'DEV').upper()
SD_STATUS = env.get('SD_STATUS', 'OFF')
SECRET = env.get('SECRET', 'secret')
27 changes: 5 additions & 22 deletions server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from json import loads
from server.config import Config
from server.exceptions import APIException
from bluemix_service_discovery.service_locator import ServiceLocator


def async_helper(args):
Expand All @@ -34,25 +33,9 @@ def get_service_url(service_name):
:return: The endpoint of the input service name
"""

# Use the Service Discovery service if Prod and toggle is on
if Config.SD_STATUS == 'ON' and env.get('VCAP_SERVICES') is not None:
try:
creds = loads(env['VCAP_SERVICES'])['service_discovery'][0]['credentials']
locator = ServiceLocator(creds['url'], creds['auth_token'])
service_instances = loads(locator.get_services(service_name=service_name, status='UP',
tags=env['LOGISTICS_WIZARD_ENV']))['instances']
if len(service_instances) == 0:
raise APIException('Dependent service not available')
return 'https://%s' % service_instances[0]['endpoint']['value']
except Exception as e:
if isinstance(e, Exception):
e = e.message
raise APIException('Cannot get dependent service', user_details=str(e), internal_details=str(e))
# Otherwise, get the service endpoint from an env var
if service_name == 'lw-erp':
return env['ERP_SERVICE']
elif service_name == 'lw-recommendation':
return env['RECOMMENDATION_SERVICE']
else:
if service_name == 'lw-erp':
return env['ERP_SERVICE']
elif service_name == 'lw-recommendation':
return env['RECOMMENDATION_SERVICE']
else:
raise APIException('Unrecognized service invocation')
raise APIException('Unrecognized service invocation')
42 changes: 0 additions & 42 deletions server/web/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def create_app():
:return: A flask object/wsgi callable.
"""
import cf_deployment_tracker
from bluemix_service_discovery.service_publisher import ServicePublisher
from server.config import Config
from os import environ as env
from server.exceptions import APIException
Expand Down Expand Up @@ -100,45 +99,4 @@ def bad_request_handler(e):
logistics_wizard.errorhandler(400)(bad_request_handler)
logistics_wizard.errorhandler(404)(not_found_handler)

# Register app with Service Discovery and initiate heartbeat cycle if running in PROD
if Config.SD_STATUS == 'ON' and env.get('VCAP_APPLICATION') is not None:
from signal import signal, SIGINT, SIGTERM
from sys import exit

# Create service publisher and register service
creds = json.loads(env['VCAP_SERVICES'])['service_discovery'][0]['credentials']
publisher = ServicePublisher('lw-controller', 300, 'UP',
json.loads(env['VCAP_APPLICATION'])['application_uris'][0],
'http', tags=['logistics-wizard', 'front-end', env['LOGISTICS_WIZARD_ENV']],
url=creds['url'], auth_token=creds['auth_token'])
publisher.register_service(True)

# Set up exit handlers for gracefully killing heartbeat thread
def exit_app(*args):
deregister_app(publisher)
exit(0)
signal(SIGTERM, exit_app)
signal(SIGINT, exit_app)
atexit.register(destroy_app, publisher)

return logistics_wizard


def deregister_app(publisher):
"""
Deregister the app and stop its heartbeat (if beating)
:param: publisher Service Discovery publisher
"""
if publisher is not None and publisher.registered:
print ("Deregistering service from Service Discovery")
publisher.deregister_service()


def destroy_app(publisher):
"""
Gracefully shuts down the controller app
:param: publisher Service Discovery publisher
"""
deregister_app(publisher)

0 comments on commit 9d7de3c

Please sign in to comment.