-
Notifications
You must be signed in to change notification settings - Fork 8
Add memory backend for in-memory testing #165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0045c12
Add optional fakeredis backend for in-memory testing
zzstoatzz e8676db
Address PR review feedback
zzstoatzz 757c6ce
Fix error message and skip fakeredis tests when not installed
zzstoatzz 18baaa7
Install fakeredis in CI and improve test coverage
zzstoatzz 203b135
Document fakeredis backend with examples and usage guide
zzstoatzz 32ab3c3
Address PR review feedback
zzstoatzz 5f5fcf1
Use fixed fakeredis fork and achieve 100% test coverage
zzstoatzz 7bb861a
Fix Python 3.13 coverage by adding pragma comments
zzstoatzz a0b98ac
Fix CI job name formatting to use matrix.backend.name
zzstoatzz ea3b4ab
Add comment about temporary fakeredis fork dependency
zzstoatzz 551ce86
Move fakeredis fork to dev dependencies only
zzstoatzz 5c6a9ae
Update lock file after moving fakeredis to dev dependencies
zzstoatzz b0ab96f
Simplify memory backend to use single shared FakeServer
zzstoatzz 09d497d
Update fakeredis fork to fix Python 3.13 ResourceWarning
zzstoatzz 78414b2
Update fakeredis to fix Python 3.13 ResourceWarning
zzstoatzz 385f9ed
Merge branch 'main' into nate/backend-abstraction
chrisguidry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,3 +9,5 @@ __pycache__/ | |
| build/ | ||
| dist/ | ||
| wheels/ | ||
|
|
||
| .coverage.* | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| #!/usr/bin/env python3 | ||
| """ | ||
| Example: Local Development Without Redis | ||
|
|
||
| This example demonstrates using Docket with the in-memory backend for | ||
| local development, prototyping, or situations where you don't have Redis | ||
| available but still want to use Docket's task scheduling features. | ||
|
|
||
| Use cases: | ||
| - Local development on a laptop without Docker/Redis | ||
| - Quick prototyping and experimentation | ||
| - Educational/tutorial environments | ||
| - Desktop applications that need background tasks | ||
| - CI/CD environments without Redis containers | ||
| - Single-process utilities that benefit from task scheduling | ||
|
|
||
| Limitations: | ||
| - Single process only (no distributed workers) | ||
| - Data stored in memory (lost on restart) | ||
| - Performance may differ from real Redis | ||
|
|
||
| To run: | ||
| uv run examples/local_development.py | ||
| """ | ||
|
|
||
| import asyncio | ||
| from datetime import datetime, timedelta, timezone | ||
|
|
||
| from docket import Docket, Worker | ||
| from docket.dependencies import Perpetual, Retry | ||
|
|
||
|
|
||
| # Example 1: Simple immediate task | ||
| async def process_file(filename: str) -> None: | ||
| print(f"📄 Processing file: {filename}") | ||
| await asyncio.sleep(0.5) # Simulate work | ||
| print(f"✅ Completed: {filename}") | ||
|
|
||
|
|
||
| # Example 2: Scheduled task with retry | ||
| async def backup_data(target: str, retry: Retry = Retry(attempts=3)) -> None: | ||
| print(f"💾 Backing up to: {target}") | ||
| await asyncio.sleep(0.3) | ||
| print(f"✅ Backup complete: {target}") | ||
|
|
||
|
|
||
| # Example 3: Periodic background task | ||
| async def health_check( | ||
| perpetual: Perpetual = Perpetual(every=timedelta(seconds=2), automatic=True), | ||
| ) -> None: | ||
| print(f"🏥 Health check at {datetime.now(timezone.utc).strftime('%H:%M:%S')}") | ||
|
|
||
|
|
||
| async def main(): | ||
| print("🚀 Starting Docket with in-memory backend (no Redis required!)\n") | ||
|
|
||
| # Use memory:// URL for in-memory operation | ||
| async with Docket(name="local-dev", url="memory://local-dev") as docket: | ||
| # Register tasks | ||
| docket.register(process_file) | ||
| docket.register(backup_data) | ||
| docket.register(health_check) | ||
|
|
||
| # Schedule some immediate tasks | ||
| print("Scheduling immediate tasks...") | ||
| await docket.add(process_file)("report.pdf") | ||
| await docket.add(process_file)("data.csv") | ||
| await docket.add(process_file)("config.json") | ||
|
|
||
| # Schedule a future task | ||
| in_two_seconds = datetime.now(timezone.utc) + timedelta(seconds=2) | ||
| print("Scheduling backup for 2 seconds from now...") | ||
| await docket.add(backup_data, when=in_two_seconds)("/tmp/backup") | ||
|
|
||
| # The periodic task will be auto-scheduled by the worker | ||
| print("Setting up periodic health check...\n") | ||
|
|
||
| # Run worker to process tasks | ||
| print("=" * 60) | ||
| async with Worker(docket, concurrency=2) as worker: | ||
| # Run for 6 seconds to see the periodic task execute a few times | ||
| print("Worker running for 6 seconds...\n") | ||
| try: | ||
| await asyncio.wait_for(worker.run_forever(), timeout=6.0) | ||
| except asyncio.TimeoutError: | ||
| print("\n" + "=" * 60) | ||
| print("✨ Demo complete!") | ||
|
|
||
| # Show final state | ||
| snapshot = await docket.snapshot() | ||
| print("\nFinal state:") | ||
| print(f" Snapshot time: {snapshot.taken.strftime('%H:%M:%S')}") | ||
| print(f" Future tasks: {len(snapshot.future)}") | ||
| print(f" Running tasks: {len(snapshot.running)}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I might have misled you, sorry! Now that I see this, we probably do just want
memory://as the URL for all of these (notmemory://<docket-name>) because the docket name is specified separately, I forgot about that part. It works either way, honestly. I'll leave it to you!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All keys are prefixed with the docket name, which is how we can have many dockets on the same redis. But again, it works either way