Package: lifeguard
LifeguardExamples of UsageSee a complete example at:https://github.com/LifeguardSystem/lifeguard-exampleSettings FileIn the root of project should be exists a file calledlifeguard_settings.pylike the example:"""Lifeguard Settings"""importlifeguard_mongodb# other dependecies# Plugins modulesPLUGINS=[lifeguard_mongodb,# other plugins]# You can execute code in the lifeguard startup processdefsetup(_lifeguard_context):passCreate a validationTo create a validation you should create a file intovalidationsdirectory. The file should ends with_validation.py.
Example:fromlifeguardimportNORMAL,PROBLEM,change_statusfromlifeguard.actions.databaseimportsave_result_into_databasefromlifeguard.actions.notificationsimportnotify_in_single_messagefromlifeguard.http_clientimportgetfromlifeguard.loggerimportlifeguard_loggerasloggerfromlifeguard.validationsimportValidationResponse,validation@validation("check if pudim is alive",actions=[save_result_into_database,notify_in_single_message],schedule={"every":{"minutes":1}},)defpudim_is_alive():status=NORMALresult=requests.get("http://pudim.com.br")logger.info("pudim status code:%s",result.status_code)ifresult.status_code!=200:status=change_status(status,PROBLEM)returnValidationResponse(status,{status:result.status_code},)Starting with version 1.3.0, it is possible to create validations using yaml files. The file should ends with_validation.yaml. See the example:validations:-validation_name:"validation_name"description:"descriptionofvalidation"actions:-lifeguard.actions.database.save_result_into_databaseschedule:every:minutes:1settings:notification:update_thread_interval:3600execute:command:path.to.module.functionargs:-"arg1"-"arg2"Validation ActionsAction is a simple python function with only 2 arguments: a validation response and a dict called settings. These settings are the parameter called settings in validation.defcustom_action(validation_response,settings):passBuiltin validations can be found inWiki.Create a custom controllerTo create a custom controller with decorators see the exampleimportjsonfromlifeguard.controllersimportcontroller@controller("/hello/<name>")defhello(name):returnjson.dumps({"name":name})This file should be in thecontrollersdirectory and should ends with_controller.py.Init LifeguardExecute:lifeguardTo init only web server:lifeguard --no-schedulerTo init only scheduler:lifeguard --no-serverSettingsTo see all settings avaiable run command:lifeguard -dBuiltin EndpointsRecover StatusTo get global status and all validations.GET /lifeguard/status/complete{"status":"NORMAL","validations":[{"validation_name":"pudim","status":"NORMAL","details":{"NORMAL":200},"settings":{"notification":{"notify":true}},"last_execution":"2021-06-15T10:46"},{"validation_name":"my site","status":"NORMAL","details":{"NORMAL":200},"settings":{"notification":{"notify":true}},"last_execution":"2021-06-15T10:46"}]}To get global status and only non normal validations.GET /lifeguard/status{"status":"PROBLEM","validations":[{"validation_name":"my site","status":"PROBLEM","details":{"NORMAL":200},"settings":{"notification":{"notify":true}},"last_execution":"2021-06-15T10:46"}]}AuthenticationBuiltin MethodsBasic AuthenticationSet users in lifeguard context like in the example:# in lifeguard_settings.pyfromlifeguard.authimportBASIC_AUTH_METHODdefsetup(lifeguard_context):lifeguard_context.auth_method=BASIC_AUTH_METHODlifeguard_context.users=[{"username":"test","password":"pass"}]CorsTo enable cors use the following settings:# in lifeguard_settings.pydefsetup(lifeguard_context):lifeguard_context.cors_settings={r"/*":{"origins":"*"}}