Skip to content

Commit

Permalink
Added --lazy flag
Browse files Browse the repository at this point in the history
  • Loading branch information
blackandred committed Jun 27, 2019
1 parent ba61acb commit 65df70f
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 8 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
CHANGES
=======

* Added --wait / WAIT\_TIME, secured \`domain-expiration\` check against CPU overload
* Added docs, clean up
* Resolved unit tests
* Resolved "rate limit exceeded" in domain-expiration check, by adding waits
Expand Down
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ ADD . /infracheck
ADD .git /infracheck/

ENV CHECK_INTERVAL="*/1 * * * *" \
WAIT_TIME=0
WAIT_TIME=0\
LAZY=false

RUN cd /infracheck \
&& git remote remove origin || true \
Expand Down
9 changes: 8 additions & 1 deletion docs/source/cache.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ It can be harmful to the server to run all checks on each HTTP endpoint call, so

How often really the checks are performing depends on your configuration, how often you execute **infracheck --force**

--force
`--force`
-------

The *--force* parameter means that the application will write checks results to a cache.
Expand All @@ -24,3 +24,10 @@ Some checks could call external APIs, those can have limits. A good example is a
It is recommended to run a separate infracheck instance with less frequent checking, eg. once a day - see CHECK_INTERVAL in docker, and crontab in standalone installation.

You can also use `--wait` switch to set waiting in seconds between single checks (in docker it is `WAIT_TIME` variable)

--lazy
------

When running a ex. HTTP endpoint without `--force`, then application is only reading results of previously executed checks that are usually executed in background using cron.
**Without lazy** the checks that were not executed yet will be showing "Not ready yet" and a **status equals to false**.
If you really need to avoid such case, **then you can allow running a check on-demand by enabling** `--lazy` **flag** (LAZY=true in docker).
3 changes: 2 additions & 1 deletion docs/source/first-steps.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ List of supported environment variables:

- CHECK_INTERVAL="*/1 * * * *"
- WAIT_TIME=0
- LAZY=false
**Without Docker**

Expand All @@ -89,7 +90,7 @@ List of supported environment variables:
infracheck --directory=/your-project-directory-path-there
# run a webserver
infracheck --directory=/your-project-directory-path-there --server --server-port=7422
infracheck --directory=/your-project-directory-path-there --server --server-port=7422 --lazy
# set up a scheduled checking
echo "*/1 * * * * infracheck --directory=/your-project-directory-path-there --force" >> /etc/crontabs/root
Expand Down
8 changes: 7 additions & 1 deletion entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ fi

chmod +x /entrypoint.cron.sh

ARGS=""

if [[ ${LAZY} == "true" ]] || ${LAZY} == "1" ]]; then
ARGS="${ARGS} --lazy "
fi

# allow to pass custom arguments from docker run command
echo "#!/bin/bash" > /entrypoint.cmd.sh
echo "infracheck --server --server-port 8000 $@" >> /entrypoint.cmd.sh
echo "infracheck --server --server-port 8000 ${ARGS} $@" >> /entrypoint.cmd.sh
chmod +x /entrypoint.cmd.sh

exec supervisord -c /infracheck/supervisord.conf
9 changes: 8 additions & 1 deletion infracheck/infracheck/bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ def main():
help='Seconds between doing checks (works only in --force mode)',
default=0
)
parser.add_argument(
'--lazy',
help='If check result is not ready, then the ' +
'check result will be populated on-demand, even if --force is not active',
default=False,
action='store_true'
)
parser.add_argument(
'--server-path-prefix',
help='Optional path prefix to the routing, eg. /this-is-a-secret will make urls looking like: '
Expand Down Expand Up @@ -110,7 +117,7 @@ def main():
sys.exit(0)

# action: perform health checking
result = app.perform_checks(force=parsed.force, wait_time=parsed.wait)
result = app.perform_checks(force=parsed.force, wait_time=parsed.wait, lazy=parsed.lazy)
print(json.dumps(result, sort_keys=True, indent=4, separators=(',', ': ')))

if not result['global_status']:
Expand Down
12 changes: 10 additions & 2 deletions infracheck/infracheck/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ def list_all_configs(self):
def spawn_server(self):
return self.server.run()

def perform_checks(self, force: bool, wait_time: int = 0):
def perform_checks(self, force: bool, wait_time: int = 0, lazy=False):
"""
:param force: Perform checks and write results
:param wait_time: After each check wait (in seconds)
:param lazy: If force not specified, and there is no result, then allow to perform a check on-demand
:return:
"""

configs = self.list_enabled_configs()
results = {}
global_status = True
Expand All @@ -51,7 +58,8 @@ def perform_checks(self, force: bool, wait_time: int = 0):
config = self.config_loader.load(config_name)

if not result:
result = self.runner.run(config['type'], config['input'], config.get('hooks', {}))
result = self.runner.run(config['type'], config['input'], config.get('hooks', {})) \
if lazy else ["Check not ready", False, ""]

# store in the cache
self.repository.push_to_cache(config_name, result)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def test_simply_perform_checks(self, config: dict, expected_result: bool, expect
controller.config_loader.load = mock.Mock()

with mock.patch.object(controller.config_loader, 'load', return_value=config):
performed = controller.perform_checks(force=True)
performed = controller.perform_checks(force=True, lazy=True)

self.assertEqual(expected_result, performed['checks']['example-check']['status'])

Expand Down

0 comments on commit 65df70f

Please sign in to comment.