Skip to main content

Lifeguard

Build Status Lifeguard Core Publish PyPI version

Examples of Usage

See a complete example at: https://github.com/LifeguardSystem/lifeguard-example

Settings File

In the root of project should be exists a file called lifeguard_settings.py like the example:

"""
Lifeguard Settings
"""
import lifeguard_mongodb
# other dependecies

# Plugins modules
PLUGINS = [
    lifeguard_mongodb,
    # other plugins
]

# You can execute code in the lifeguard startup process
def setup(_lifeguard_context):
    pass

Create a validation

To create a validation you should create a file into validations directory. The file should ends with _validation.py. Example:

from lifeguard import NORMAL, PROBLEM, change_status
from lifeguard.actions.database import save_result_into_database
from lifeguard.actions.notifications import notify_in_single_message
from lifeguard.http_client import get
from lifeguard.logger import lifeguard_logger as logger
from lifeguard.validations import ValidationResponse, validation


@validation(
    "check if pudim is alive",
    actions=[save_result_into_database, notify_in_single_message],
    schedule={"every": {"minutes": 1}},
)
def pudim_is_alive():
    status = NORMAL
    result = requests.get("http://pudim.com.br")
    logger.info("pudim status code: %s", result.status_code)

    if result.status_code != 200:
        status = change_status(status, PROBLEM)

    return ValidationResponse(
        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: "description of validation"
    actions:
      - lifeguard.actions.database.save_result_into_database
    schedule:
      every:
        minutes: 1
    settings:
      notification:
        update_thread_interval: 3600
    execute:
      command: path.to.module.function
      args:
        - "arg1"
        - "arg2"

Validation Actions

Action 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.

def custom_action(validation_response, settings):
    pass

Builtin validations can be found in Wiki.

Create a custom controller

To create a custom controller with decorators see the example

import json

from lifeguard.controllers import controller


@controller("/hello/<name>")
def hello(name):
    return json.dumps({"name": name})

This file should be in the controllers directory and should ends with _controller.py.

Init Lifeguard

Execute: lifeguard

To init only web server: lifeguard --no-scheduler

To init only scheduler: lifeguard --no-server

Settings

To see all settings avaiable run command:

lifeguard -d

Builtin Endpoints

Recover Status

To 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"
        }
    ]
}

Authentication

Builtin Methods

Basic Authentication

Set users in lifeguard context like in the example:

# in lifeguard_settings.py
from lifeguard.auth import BASIC_AUTH_METHOD

def setup(lifeguard_context):
    lifeguard_context.auth_method = BASIC_AUTH_METHOD
    lifeguard_context.users = [{"username": "test", "password": "pass"}]

Cors

To enable cors use the following settings:

# in lifeguard_settings.py
def setup(lifeguard_context):
    lifeguard_context.cors_settings = {r"/*": {"origins": "*"}}

Release files for lifeguard 1.5.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lifeguard 1.5.2
File Size Uploaded
lifeguard-1.5.2.tar.gz 31.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for lifeguard 1.5.2
File Interpreter ABI Platform
lifeguard-1.5.2-py3-none-any.whl Python 3 none any Details

Total release size: 73.5 kB

Release files / lifeguard-1.5.2.tar.gz

Download URL lifeguard-1.5.2.tar.gz
Size 31.2 kB
Tags Source
SHA-256 checksum
How to use checksums
e245167ab7944acc2fe8ed3949eeae1b1534ac2e6f3cf061cb4a216ce50f1f9f
BLAKE2b-256 checksum
How to use checksums
535e2d4ebc5a1af996ceee9623e70c4e834933ff48b36e77225596731978286b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release files / lifeguard-1.5.2-py3-none-any.whl

Download URL lifeguard-1.5.2-py3-none-any.whl
Size 42.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3db66a581f2c35747c342f01683d5a4e7b8c338a26c7bfd002d2f8549cdeca4f
BLAKE2b-256 checksum
How to use checksums
57918a461b0f8a3968084e18328e1d99404aa43847c002320c3bcb6268396ddd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release history Release notifications | RSS feed

This release

1.5.2 This release

2 release files

1.5.1

2 release files

1.5.0

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.0.48

2 release files

0.0.47

2 release files

0.0.45

2 release files

0.0.43

2 release files

0.0.42

2 release files

0.0.41

2 release files

0.0.40

2 release files

0.0.39

2 release files

0.0.38

2 release files

0.0.37

2 release files

0.0.34

2 release files

0.0.32

2 release files

0.0.31

2 release files

0.0.28

2 release files

0.0.24

2 release files

0.0.23

2 release files

0.0.22

2 release files

0.0.21

2 release files

0.0.20

2 release files

0.0.19

2 release files

0.0.18

2 release files

0.0.17

2 release files

0.0.16

2 release files

0.0.15

2 release files

0.0.14

2 release files

0.0.13

2 release files

0.0.12

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page