def run_service(db, start_service, end_service): service_id = os.environ['RAFIKI_SERVICE_ID'] service_type = os.environ['RAFIKI_SERVICE_TYPE'] container_id = os.environ.get('HOSTNAME', 'localhost') configure_logging('service-{}-{}'.format(service_id, container_id)) def _sigterm_handler(_signo, _stack_frame): logger.warn("Terminal signal received: %s, %s" % (_signo, _stack_frame)) # Mark service as stopped in DB with db: service = db.get_service(service_id) db.mark_service_as_stopped(service) end_service(service_id, service_type) exit(0) signal.signal(signal.SIGINT, _sigterm_handler) signal.signal(signal.SIGTERM, _sigterm_handler) # Mark service as running in DB with db: service = db.get_service(service_id) db.mark_service_as_running(service) try: logger.info('Starting service {}...'.format(service_id)) start_service(service_id, service_type) logger.info('Ending service {}...'.format(service_id)) # Mark service as stopped in DB with db: service = db.get_service(service_id) db.mark_service_as_stopped(service) end_service(service_id, service_type) except Exception as e: logger.error('Error while running service:') logger.error(traceback.format_exc()) # Mark service as errored in DB with db: service = db.get_service(service_id) db.mark_service_as_errored(service) end_service(service_id, service_type) raise e
def run_worker(meta_store, start_worker, stop_worker): service_id = os.environ['RAFIKI_SERVICE_ID'] service_type = os.environ['RAFIKI_SERVICE_TYPE'] container_id = os.environ.get('HOSTNAME', 'localhost') configure_logging('service-{}-worker-{}'.format(service_id, container_id)) def _sigterm_handler(_signo, _stack_frame): logger.warn("Terminal signal received: %s, %s" % (_signo, _stack_frame)) stop_worker() exit(0) signal.signal(signal.SIGINT, _sigterm_handler) signal.signal(signal.SIGTERM, _sigterm_handler) # Mark service as running in DB with meta_store: service = meta_store.get_service(service_id) meta_store.mark_service_as_running(service) try: logger.info('Starting worker "{}" for service of ID "{}"...'.format( container_id, service_id)) start_worker(service_id, service_type, container_id) logger.info('Stopping worker...') stop_worker() except Exception as e: logger.error('Error while running worker:') logger.error(traceback.format_exc()) # Mark service as errored in DB with meta_store: service = meta_store.get_service(service_id) meta_store.mark_service_as_errored(service) stop_worker() raise e
import os from rafiki.utils.log import configure_logging from rafiki.admin import Admin from rafiki.admin.app import app configure_logging('admin') if __name__ == "__main__": # Run seed logic for admin at start-up admin = Admin() admin.seed() # Run Flask app app.run( host='0.0.0.0', port=os.getenv('ADMIN_PORT', 3000), threaded=True)
import sys import os import signal import traceback import logging from rafiki.constants import ServiceType from rafiki.utils.log import configure_logging service_id = os.environ['RAFIKI_SERVICE_ID'] service_type = os.environ['RAFIKI_SERVICE_TYPE'] container_id = os.environ.get('HOSTNAME', 'localhost') configure_logging('service-{}-{}'.format(service_id, container_id)) logger = logging.getLogger(__name__) def sigterm_handler(_signo, _stack_frame): print("SIGTERM received: %s, %s" % (_signo, _stack_frame)) exit_worker() worker = None def exit_worker(): if worker is not None: worker.stop() print('Worker stopped gracefully.')
import os from rafiki.utils.log import configure_logging from rafiki.advisor.app import app configure_logging('advisor') if __name__ == "__main__": # No threading since data is in-memory app.run(host='0.0.0.0', port=os.getenv('ADVISOR_PORT', 8001), threaded=False, debug=True)