Skip to main content

A mechanism for running processes which might have side effects (e.g.: calling an external API etc.) in a way that is safe by default during testing and provides useful introspection functionality.

PyPI version

It will: Run a list of tasks. If possible, it will run them asyncronously

In TEST_MODE:

  • It will not run the code
  • It will store information about the calls made in

Usage

Install and configure

pip install dj-saferunner

Config options

Django settings

SAFERUNNER_USE_SENTRY - set to True to swallow errors and log to Sentry

In your code:

before

from myapp.tasks import ping_google
ping_google(url='http://foo.com')

after

from myapp.tasks import ping_google
from saferunner.process_runner import safely_run_processes
task_list = [
    # task, args, async
    (ping_google, {"url", "http://foo.com"}, False)
]
safely_run_processes(
    run_id='identifier giving some context about where this is run',
    task_list=task_list
)

In your tests

When in test mode, by default the saferunner will not execute your code. Instead it will log your calls using django's cache

example test

def test_run_some_tasks(self):
    clear_run_cache()
    run_id = 'this_is_a_test'
    task_list = [
        (hello, {"msg": "hi"} , False),
        (do_something_slowly, {"sleep": 10}, True),
    ]
    safely_run_processes(
        run_id = run_id,
        task_list = task_list
    )
    res = get_process_run(run_id)

The results of get_process_run would look something like this:

[{
	'task_name': 'hello',
	'task_args': "OrderedDict([('msg', 'hi')])",
	'is_async': False
}, {
	'task_name': 'do_something_slowly',
	'task_args': "OrderedDict([('sleep', 10)])",
	'is_async': True
}]

Explicitly running side effect code

In your tests, You can force saferunner to execute safely run code.

You might want to do this, for example, if you want to test code inside your function from a higher level. Ideally you should not really need to do this too often. We would recommend that it is preferred to test your functions by calling them directly (as opposed to checking for side effects in a higher level function)

Run all side effect code:

Will essentially turn saferunner off for this test

@override_settings(FORCE_RUN_SIDE_EFFECTS='__all__')

You can also specify a specific task to pass through:

This will force saferunner to explicitly run this exact function/method

@override_settings(FORCE_RUN_SIDE_EFFECTS=task.name)
@override_settings(FORCE_RUN_SIDE_EFFECTS=task.__name__)

FAQ:

Q: Why not just use mocks? A: The use-case for this is for code that might get executed often (for example code in signals), and which might have unwanted side-effects if accidentally run durung tests (e.g.: send SMSes or making API calls). We want to err on the side of caution here and mock by default during testing and be explicit about when we want to run this code

PS: DHH would probably call this test induced damage!

TODO

  • Make it work Class methods

Metadata

Release files for dj-saferunner 0.0.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 dj-saferunner 0.0.2
File Size Uploaded
dj-saferunner-0.0.2.tar.gz 4.7 kB Details

Release files / dj-saferunner-0.0.2.tar.gz

Download URL dj-saferunner-0.0.2.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f2fddae12798efc26333dce8615a7e834fd368e52494e6a9d8af4e7019e91fe9
BLAKE2b-256 checksum
How to use checksums
904a2fa25c4b15c308ad4dbf141ba2d5e32d807c1934b924705613ff98ea1d1b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

Release history Release notifications | RSS feed

This release

0.0.2 This release

1 release file

0.0.1

1 release file

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