A simple system state test utility
Project description
A lightweight application environment checker.
Python 3 only. The future is now.
Getting started
To use Preflyt, install it pip install preflyt
, and then invoke it:
import preflyt
ok, results = preflyt.check([
# Assert the presence and value of the $APP_ENV environment variable
{"checker": "env", "name": "APP_ENV", "value": "production"},
# Assert that a file at the given path exists
{"checker": "file", "path": DATA_FILE},
])
if ok:
print("Preflyt checks passed.")
run()
else:
print("Preflyt checks failed!")
for result in result:
if not result["success"]:
print("{checker[checker]}: {message}".format(**result))
Philosophy
You know what sucks? Kicking off a long running data ingestion/processing task only to discover, near the end, that an external dependency (e.g. webservice, binary) is missing or otherwise inaccessible. “I know what I’ll do!” you, the frustrated programmer, exclaims. Choose your own adventure:
“I’m going to manually verify things are as they should be before I kick off the task.”
Congratulations, you just played yourself. Not only do you run the risk of forgetting a checklist item, but now you have to enforce this practice within your team.
“I’m going to programatically check things on script start.”
Getting warm! Hopefully your solution is configuration driven. Even then, what are the odds you wind up with this boilerplate across your scripts?
# settings.py if env_name == "production": ES_HOST = "http://example.com" POSTGRES_HOST = "10.0.1.120" ENABLE_DATA_DIR_CHECK = True else: ES_HOST = "localhost:9200" POSTGRES_HOST = "localhost" ENABLE_DATA_DIR_CHECK = False DATA_DIR = "/mnt/data/dir" DATA_FILE = "/mnt/data/dir/metadata.json" POSTGRES_PORT = 5432 # run.py if not requests.get(settings.ES_HOST).status_ok: #Now you've got a requests dependency print("Elasticsearch is unreachable.") sys.exit(1) if settings.ENABLE_DATA_DIR_CHECK and not os.path.exists(settings.DATA_DIR): # Whoops, should have used `isdir` print("Can't access: ", settings.DATA_DIR) sys.exit(1) if not os.path.exists(settings.DATA_FILE): # Whoops, should have used `isfile` print("Can't access: ", settings.DATA_FILE) sys.exit(1) try: postgres.connect(settings.POSTGRES_HOST, settings.POSTGRES_PORT) except Exception as exe: print(exe) sys.exit(1)
And so forth. You’ve now got a crazy-long series of if statements in your code, and changing the checks is a code change, not a configuration change. Also, you’ve generated boilerplate that should be abstracted and reused.
“I’m going to programatically check things on script start… with Preflyt!”
Bingo. That ugly series of code above?
# settings.py checks = [ {"checker": "web", "url": ES_HOST}, {"checker": "psql", "host": POSTGRES_HOST, "port": POSTGRES_PORT}, {"checker": "file", "path": DATA_FILE}, ] if envname == "production": checks.append({"checker": "dir", "path": DATA_DIR}) # run.py import preflyt ok, results = preflyt.check(settings.checks) if not ok: print([result in results if not result["success"]]) sys.exit(1)
Now all the checks your performing are defined in configuration, and no boilerplate!
Contributing
Additional checkers are more than welcome! The goal is to keep this package free of dependencies, so cleverness is appreciated :-)
Please write tests for whatever checkers you wish to submit. Preflyt uses nose. Development packages can be installed via pip install -e .[test]
, and tests can be run via nosetests .
.
License
MIT, Copyright (c) 2016 The HumanGeo Group, LLC. See the LICENSE file for more information.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file preflyt-0.3.0.tar.gz
.
File metadata
- Download URL: preflyt-0.3.0.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bd6ccd039edbf3b764491de10f5cd6f3e94e3fa7cb95ddfb4507d6bb55b2a92d |
|
MD5 | 4b057f067fb5e115afe4b2073af753b1 |
|
BLAKE2b-256 | 37be97b669297fb7ef8dca0753ba026f0f9f62773a0438d88704899443c7d359 |