Skip to main content

Quit Datasette if it has not received traffic for a specified time period

Project description

datasette-scale-to-zero

PyPI Changelog Tests License

Quit Datasette if it has not received traffic for a specified time period

Some hosting providers such as Fly offer a scale to zero mechanism, where servers can shut down and will be automatically started when new traffic arrives.

This plugin can be used to configure Datasette to quit X minutes (or seconds, or hours) after the last request it received. It can also cause the Datasette server to exit after a configured maximum time whether or not it is receiving traffic.

Installation

Install this plugin in the same environment as Datasette.

datasette install datasette-scale-to-zero

Configuration

This plugin will only take effect if it has been configured with a duration or max_age value. Without at least one of these settings the plugin will do nothing.

Add the following to your metadata.json or metadata.yml configuration file:

{
    "plugins": {
        "datasette-scale-to-zero": {
            "duration": "10m"
        }
    }
}

This will cause Datasette to quit if it has not received any HTTP traffic for 10 minutes.

You can set this value using a suffix of m for minutes, h for hours or s for seconds.

To cause Datasette to exit if the server has been running for longer than a specific time, use "max_age":

{
    "plugins": {
        "datasette-scale-to-zero": {
            "max_age": "10h"
        }
    }
}

This example will exit the Datasette server if it has been running for more than ten hours.

You can use "duration" and "max_age" together in the same configuration file:

{
    "plugins": {
        "datasette-scale-to-zero": {
            "max_age": "10h",
            "duration": "5m"
        }
    }
}

This example will quit if no traffic has been received in five minutes, or if the server has been running for ten hours.

Configuring a shutdown HTTP message

You can also configure the plugin to send an HTTP request somewhere right before it quits, using the "shutdown_url" option:

{
    "plugins": {
        "datasette-scale-to-zero": {
            "duration": "10m",
            "shutdown_url": "https://example.com/shutdown"
        }
    }
}

You can add additional headers to the GET request - for example to send Authorization headers - using "shutdown_headers":

{
    "plugins": {
        "datasette-scale-to-zero": {
            "duration": "10m",
            "shutdown_url": "https://example.com/shutdown",
            "shutdown_headers": {
                "Authorization": "Bearer secret"
            }
        }
    }
}

Use "shutdown_method" to set a different HTTP method, e.g. for POST. You can also set shutdown_body to specify the body that should be sent with the request:

{
    "plugins": {
        "datasette-scale-to-zero": {
            "duration": "10m",
            "shutdown_url": "https://example.com/shutdown",
            "shutdown_method": "POST",
            "shutdown_headers": {
                "Authorization": "Bearer secret",
                "Content-Type": "application/json"
            },
            "shutdown_body": "{\"message\": \"shutting down\"}"
        }
    }
}

Development

To set up this plugin locally, first checkout the code. Then create a new virtual environment:

cd datasette-scale-to-zero
python3 -m venv venv
source venv/bin/activate

Now install the dependencies and test dependencies:

pip install -e '.[test]'

To run the tests:

pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

datasette_scale_to_zero-0.3.2.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

datasette_scale_to_zero-0.3.2-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file datasette_scale_to_zero-0.3.2.tar.gz.

File metadata

  • Download URL: datasette_scale_to_zero-0.3.2.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for datasette_scale_to_zero-0.3.2.tar.gz
Algorithm Hash digest
SHA256 457d97d389df85b5e5666d050f1f102cdeaa61714d781db82d0287519fd26736
MD5 c686851dc64c8d373ed9906472345af7
BLAKE2b-256 d445b8d59d4f4b9ed9fd5337bda954fcac65647eeca3cda6610a7180526f4220

See more details on using hashes here.

Provenance

The following attestation bundles were made for datasette_scale_to_zero-0.3.2.tar.gz:

Publisher: publish.yml on simonw/datasette-scale-to-zero

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file datasette_scale_to_zero-0.3.2-py3-none-any.whl.

File metadata

File hashes

Hashes for datasette_scale_to_zero-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c2da2f3c63323c127685bd43450b626615e156331322893aa9eb2779bd4ff952
MD5 a07d4a74499d3a0ba913515b7061cb53
BLAKE2b-256 7b3ab03af90f15f0d8efad8c18746b2ef410f806a1b81035238b3fe66333cbd5

See more details on using hashes here.

Provenance

The following attestation bundles were made for datasette_scale_to_zero-0.3.2-py3-none-any.whl:

Publisher: publish.yml on simonw/datasette-scale-to-zero

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page