| 1 |
import functools |
| 2 |
from concurrent.futures import ThreadPoolExecutor, as_completed |
| 3 |
from typing import Any |
| 4 |
|
| 5 |
from django.conf import settings |
| 6 |
from django.core.management import call_command |
| 7 |
from django.core.management.base import BaseCommand, CommandError, OutputWrapper |
| 8 |
from django.db.utils import ProgrammingError |
| 9 |
|
| 10 |
from django_pgschemas.routing.info import DomainInfo |
| 11 |
from django_pgschemas.routing.models import get_primary_domain_for_tenant |
| 12 |
from django_pgschemas.schema import Schema, activate |
| 13 |
from django_pgschemas.utils import get_clone_reference, get_tenant_model |
| 14 |
|
| 15 |
|
| 16 |
def run_on_schema( |
| 17 |
schema_name: str, |
| 18 |
executor_codename: str, |
| 19 |
command: BaseCommand | type[BaseCommand], |
| 20 |
function_name: str | None = None, |
| 21 |
args: list[Any] | None = None, |
| 22 |
kwargs: dict[str, Any] | None = None, |
| 23 |
pass_schema_in_kwargs: bool = False, |
| 24 |
) -> str: |
| 25 |
if args is None: |
| 26 |
args = [] |
| 27 |
if kwargs is None: |
| 28 |
kwargs = {} |
| 29 |
|
| 30 |
if not isinstance(command, BaseCommand): |
| 31 |
# Parallel executor needs to pass command 'type' instead of 'instance' |
| 32 |
# Therefore, no customizations for the command can be done, nor using custom stdout, stderr |
| 33 |
command = command() |
| 34 |
|
| 35 |
command.stdout = kwargs.pop("stdout", command.stdout) |
| 36 |
if not isinstance(command.stdout, OutputWrapper): |
| 37 |
command.stdout = OutputWrapper(command.stdout) |
| 38 |
|
| 39 |
command.stderr = kwargs.pop("stderr", command.stderr) |
| 40 |
if not isinstance(command.stderr, OutputWrapper): |
| 41 |
command.stderr = OutputWrapper(command.stderr) |
| 42 |
|
| 43 |
# Since we are prepending every output with the schema_name and executor, we need to determine |
| 44 |
# whether we need to do so based on the last ending used to write. If the last write didn't end |
| 45 |
# in '\n' then we don't do the prefixing in order to keep the output looking good. |
| 46 |
class StyleFunc: |
| 47 |
last_message: str | None = None |
| 48 |
|
| 49 |
def __call__(self, message: str) -> str: |
| 50 |
last_message = self.last_message |
| 51 |
self.last_message = message |
| 52 |
if last_message is None or last_message.endswith("\n"): |
| 53 |
return "[%s:%s] %s" % ( |
| 54 |
command.style.NOTICE(executor_codename), |
| 55 |
command.style.NOTICE(schema_name), |
| 56 |
message, |
| 57 |
) |
| 58 |
return message |
| 59 |
|
| 60 |
command.stdout.style_func = StyleFunc() |
| 61 |
command.stderr.style_func = StyleFunc() |
| 62 |
|
| 63 |
if schema_name in settings.TENANTS: |
| 64 |
domains = settings.TENANTS[schema_name].get("DOMAINS", []) |
| 65 |
schema = Schema.create( |
| 66 |
schema_name=schema_name, |
| 67 |
routing=DomainInfo(domain=domains[0]) if domains else None, |
| 68 |
) |
| 69 |
elif schema_name == get_clone_reference(): |
| 70 |
schema = Schema.create(schema_name=schema_name) |
| 71 |
elif (TenantModel := get_tenant_model()) is not None: |
| 72 |
try: |
| 73 |
schema = TenantModel.objects.get(schema_name=schema_name) |
| 74 |
if (domain := get_primary_domain_for_tenant(schema)) is not None: |
| 75 |
schema.routing = DomainInfo(domain=domain.domain, folder=domain.folder) |
| 76 |
except ProgrammingError: |
| 77 |
schema = Schema.create(schema_name=schema_name) |
| 78 |
|
| 79 |
else: |
| 80 |
raise CommandError(f"Unable to find schema {schema_name}!") |
| 81 |
|
| 82 |
if pass_schema_in_kwargs: |
| 83 |
kwargs.update({"schema_name": schema_name}) |
| 84 |
|
| 85 |
activate(schema) |
| 86 |
|
| 87 |
if function_name == "special:call_command": |
| 88 |
call_command(command, *args, **kwargs) |
| 89 |
elif function_name == "special:run_from_argv": |
| 90 |
command.run_from_argv(args) |
| 91 |
elif function_name is not None: |
| 92 |
getattr(command, function_name)(*args, **kwargs) |
| 93 |
else: |
| 94 |
raise CommandError("function_name must be provided") |
| 95 |
|
| 96 |
return schema_name |
| 97 |
|
| 98 |
|
| 99 |
def sequential( |
| 100 |
schemas: list[str], |
| 101 |
command: BaseCommand | type[BaseCommand], |
| 102 |
function_name: str, |
| 103 |
args: list[Any] | None = None, |
| 104 |
kwargs: dict[str, Any] | None = None, |
| 105 |
pass_schema_in_kwargs: bool = False, |
| 106 |
) -> list[str]: |
| 107 |
runner = functools.partial( |
| 108 |
run_on_schema, |
| 109 |
executor_codename="sequential", |
| 110 |
command=command, |
| 111 |
function_name=function_name, |
| 112 |
args=args, |
| 113 |
kwargs=kwargs, |
| 114 |
pass_schema_in_kwargs=pass_schema_in_kwargs, |
| 115 |
) |
| 116 |
|
| 117 |
for schema in schemas: |
| 118 |
runner(schema) |
| 119 |
|
| 120 |
return schemas |
| 121 |
|
| 122 |
|
| 123 |
def parallel( |
| 124 |
schemas: list[str], |
| 125 |
command: BaseCommand | type[BaseCommand], |
| 126 |
function_name: str, |
| 127 |
args: list[Any] | None = None, |
| 128 |
kwargs: dict[str, Any] | None = None, |
| 129 |
pass_schema_in_kwargs: bool = False, |
| 130 |
) -> list[str]: |
| 131 |
processes = getattr(settings, "PGSCHEMAS_PARALLEL_MAX_PROCESSES", None) |
| 132 |
runner = functools.partial( |
| 133 |
run_on_schema, |
| 134 |
executor_codename="parallel", |
| 135 |
command=type(command), # Can't pass streams to children processes |
| 136 |
function_name=function_name, |
| 137 |
args=args, |
| 138 |
kwargs=kwargs, |
| 139 |
pass_schema_in_kwargs=pass_schema_in_kwargs, |
| 140 |
) |
| 141 |
|
| 142 |
with ThreadPoolExecutor(max_workers=processes) as executor: |
| 143 |
results = {executor.submit(runner, schema) for schema in schemas} |
| 144 |
as_completed(results) |
| 145 |
|
| 146 |
return schemas |