-
Notifications
You must be signed in to change notification settings - Fork 3
feat(cli): Composite startup verbs for the flagsmith entrypoint #240
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
6 commits
Select commit
Hold shift + click to select a range
96d73e5
feat(cli): Add composite startup verbs to the flagsmith entrypoint
khvn26 c57c5ec
feat(task-processor): Default worker tuning arguments from environment
khvn26 5def409
docs: Document the flagsmith startup verbs and their configuration
khvn26 02f363b
fix(cli): Split startup commands so their arguments dispatch correctly
khvn26 d955e51
add constants
khvn26 10cdd9a
proper env var fallback
khvn26 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
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,79 @@ | ||
| """Composite startup verbs for the `flagsmith` entrypoint. | ||
|
|
||
| These mirror the sequencing that Core API's `run-docker.sh` performed in | ||
| shell, but run in a single process so the Django boot cost is paid once. | ||
| """ | ||
|
|
||
| import os | ||
| import shlex | ||
|
|
||
| from django.conf import settings | ||
| from django.core.management import ( | ||
| execute_from_command_line as django_execute_from_command_line, | ||
| ) | ||
|
|
||
| WAIT_FOR_MIGRATIONS_TIMEOUT_SECONDS = 30 | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def _wait_for_db( | ||
| *, | ||
| wait_for_migrations: bool = False, | ||
| database: str = "default", | ||
| wait_for: int | None = None, | ||
| ) -> None: | ||
| if os.environ.get("SKIP_WAIT_FOR_DB"): | ||
| return | ||
| args = ["waitfordb", "--database", database] | ||
| if wait_for is not None: | ||
| args += ["--waitfor", str(wait_for)] | ||
| if wait_for_migrations: | ||
| args.append("--migrations") | ||
| django_execute_from_command_line(["flagsmith", *args]) | ||
|
|
||
|
|
||
| def _migrate() -> None: | ||
| _wait_for_db() | ||
| databases: list[str] = getattr(settings, "FLAGSMITH_MIGRATE_DATABASES", ["default"]) | ||
| for database in databases: | ||
| django_execute_from_command_line( | ||
| ["flagsmith", "migrate", "--database", database] | ||
| ) | ||
| django_execute_from_command_line(["flagsmith", "createcachetable"]) | ||
|
|
||
|
|
||
| def migrate(argv: list[str], *, prog: str) -> None: | ||
| """Migrate the configured databases, then create the cache table.""" | ||
| if argv: | ||
| django_execute_from_command_line(["flagsmith", "migrate", *argv]) | ||
| return | ||
| _migrate() | ||
|
|
||
|
|
||
| def serve(argv: list[str], *, prog: str) -> None: | ||
| """Wait for the database, then start the API server.""" | ||
| _wait_for_db() | ||
| django_execute_from_command_line(["flagsmith", "start", "api", *argv]) | ||
|
|
||
|
|
||
| def run_task_processor(argv: list[str], *, prog: str) -> None: | ||
| """Migrate, wait for migrations to be applied, then start the task processor.""" | ||
| _migrate() | ||
|
matthewelwell marked this conversation as resolved.
|
||
| databases: list[str] = getattr( | ||
| settings, "FLAGSMITH_WAIT_FOR_MIGRATIONS_DATABASES", ["default"] | ||
| ) | ||
| for database in databases: | ||
| _wait_for_db( | ||
| wait_for_migrations=True, | ||
| database=database, | ||
| wait_for=WAIT_FOR_MIGRATIONS_TIMEOUT_SECONDS, | ||
| ) | ||
| django_execute_from_command_line(["flagsmith", "start", "task-processor", *argv]) | ||
|
|
||
|
|
||
| def migrate_and_serve(argv: list[str], *, prog: str) -> None: | ||
| """Migrate, run any configured startup commands, then start the API server.""" | ||
| _migrate() | ||
| startup_commands: list[str] = getattr(settings, "FLAGSMITH_STARTUP_COMMANDS", []) | ||
| for command in startup_commands: | ||
| django_execute_from_command_line(["flagsmith", *shlex.split(command)]) | ||
| django_execute_from_command_line(["flagsmith", "start", "api", *argv]) | ||
|
matthewelwell marked this conversation as resolved.
coderabbitai[bot] marked this conversation as resolved.
|
||
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,4 @@ | ||
| DEFAULT_TASK_PROCESSOR_NUM_THREADS: int = 5 | ||
| DEFAULT_TASK_PROCESSOR_SLEEP_INTERVAL_MS: int = 500 | ||
| DEFAULT_TASK_PROCESSOR_GRACE_PERIOD_MS: int = 20000 | ||
| DEFAULT_TASK_PROCESSOR_QUEUE_POP_SIZE: int = 10 |
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.
Uh oh!
There was an error while loading. Please reload this page.