main commit
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2025-10-16 16:30:25 +09:00
parent 91c7e04474
commit 537e7b363f
1146 changed files with 45926 additions and 77196 deletions

View File

@@ -1,6 +1,4 @@
"""Worker name utilities."""
from __future__ import annotations
import os
import socket
from functools import partial
@@ -24,18 +22,13 @@ NODENAME_DEFAULT = 'celery'
gethostname = memoize(1, Cache=dict)(socket.gethostname)
__all__ = (
'worker_direct',
'gethostname',
'nodename',
'anon_nodename',
'nodesplit',
'default_nodename',
'node_format',
'host_format',
'worker_direct', 'gethostname', 'nodename',
'anon_nodename', 'nodesplit', 'default_nodename',
'node_format', 'host_format',
)
def worker_direct(hostname: str | Queue) -> Queue:
def worker_direct(hostname):
"""Return the :class:`kombu.Queue` being a direct route to a worker.
Arguments:
@@ -53,20 +46,21 @@ def worker_direct(hostname: str | Queue) -> Queue:
)
def nodename(name: str, hostname: str) -> str:
def nodename(name, hostname):
"""Create node name from name/hostname pair."""
return NODENAME_SEP.join((name, hostname))
def anon_nodename(hostname: str | None = None, prefix: str = 'gen') -> str:
def anon_nodename(hostname=None, prefix='gen'):
"""Return the nodename for this process (not a worker).
This is used for e.g. the origin task message field.
"""
return nodename(''.join([prefix, str(os.getpid())]), hostname or gethostname())
return nodename(''.join([prefix, str(os.getpid())]),
hostname or gethostname())
def nodesplit(name: str) -> tuple[None, str] | list[str]:
def nodesplit(name):
"""Split node name into tuple of name/hostname."""
parts = name.split(NODENAME_SEP, 1)
if len(parts) == 1:
@@ -74,21 +68,21 @@ def nodesplit(name: str) -> tuple[None, str] | list[str]:
return parts
def default_nodename(hostname: str) -> str:
def default_nodename(hostname):
"""Return the default nodename for this process."""
name, host = nodesplit(hostname or '')
return nodename(name or NODENAME_DEFAULT, host or gethostname())
def node_format(s: str, name: str, **extra: dict) -> str:
def node_format(s, name, **extra):
"""Format worker node name (name@host.com)."""
shortname, host = nodesplit(name)
return host_format(s, host, shortname or NODENAME_DEFAULT, p=name, **extra)
return host_format(
s, host, shortname or NODENAME_DEFAULT, p=name, **extra)
def _fmt_process_index(prefix: str = '', default: str = '0') -> str:
def _fmt_process_index(prefix='', default='0'):
from .log import current_process_index
index = current_process_index()
return f'{prefix}{index}' if index else default
@@ -96,19 +90,13 @@ def _fmt_process_index(prefix: str = '', default: str = '0') -> str:
_fmt_process_index_with_prefix = partial(_fmt_process_index, '-', '')
def host_format(s: str, host: str | None = None, name: str | None = None, **extra: dict) -> str:
def host_format(s, host=None, name=None, **extra):
"""Format host %x abbreviations."""
host = host or gethostname()
hname, _, domain = host.partition('.')
name = name or hname
keys = dict(
{
'h': host,
'n': name,
'd': domain,
'i': _fmt_process_index,
'I': _fmt_process_index_with_prefix,
},
**extra,
)
keys = dict({
'h': host, 'n': name, 'd': domain,
'i': _fmt_process_index, 'I': _fmt_process_index_with_prefix,
}, **extra)
return simple_format(s, keys)