1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
|
from __future__ import annotations
import asyncio
import atexit
import gc
import logging
import os
import re
import sys
import warnings
import click
from distributed import Scheduler
from distributed._signals import wait_for_signals
from distributed.preloading import validate_preload_argv
from distributed.proctitle import (
enable_proctitle_on_children,
enable_proctitle_on_current,
)
logger = logging.getLogger("distributed.scheduler")
pem_file_option_type = click.Path(exists=True, resolve_path=True)
@click.command(name="scheduler", context_settings=dict(ignore_unknown_options=True))
@click.option("--host", type=str, default="", help="URI, IP or hostname of this server")
@click.option("--port", type=str, default=None, help="Serving port")
@click.option(
"--interface",
type=str,
default=None,
help="Preferred network interface like 'eth0' or 'ib0'",
)
@click.option(
"--protocol", type=str, default=None, help="Protocol like tcp, tls, or ucx"
)
@click.option(
"--tls-ca-file",
type=pem_file_option_type,
default=None,
help="CA cert(s) file for TLS (in PEM format)",
)
@click.option(
"--tls-cert",
type=pem_file_option_type,
default=None,
help="certificate file for TLS (in PEM format)",
)
@click.option(
"--tls-key",
type=pem_file_option_type,
default=None,
help="private key file for TLS (in PEM format)",
)
# XXX default port (or URI) values should be centralized somewhere
@click.option(
"--dashboard-address",
type=str,
default=":8787",
show_default=True,
help="Address on which to listen for diagnostics dashboard",
)
@click.option(
"--dashboard/--no-dashboard",
"dashboard",
default=True,
required=False,
help="Launch the Dashboard [default: --dashboard]",
)
@click.option(
"--jupyter/--no-jupyter",
"jupyter",
default=False,
required=False,
help="Start a Jupyter Server in the same process. Warning: This will make"
"it possible for anyone with access to your dashboard address to run"
"Python code",
)
@click.option("--show/--no-show", default=False, help="Show web UI [default: --show]")
@click.option(
"--dashboard-prefix", type=str, default="", help="Prefix for the dashboard app"
)
@click.option(
"--use-xheaders",
type=bool,
default=False,
show_default=True,
help="User xheaders in dashboard app for ssl termination in header",
)
@click.option("--pid-file", type=str, default="", help="File to write the process PID")
@click.option(
"--scheduler-file",
type=str,
default="",
help="File to write connection information. "
"This may be a good way to share connection information if your "
"cluster is on a shared network file system.",
)
@click.option(
"--preload",
type=str,
multiple=True,
is_eager=True,
help="Module that should be loaded by the scheduler process "
'like "foo.bar" or "/path/to/foo.py".',
)
@click.argument(
"preload_argv", nargs=-1, type=click.UNPROCESSED, callback=validate_preload_argv
)
@click.option(
"--idle-timeout",
default=None,
type=str,
help="Time of inactivity after which to kill the scheduler",
)
@click.version_option()
def main(
host,
port,
protocol,
interface,
show,
dashboard,
dashboard_prefix,
use_xheaders,
pid_file,
tls_ca_file,
tls_cert,
tls_key,
dashboard_address,
jupyter,
**kwargs,
):
"""Launch a distributed scheduler."""
if "dask-scheduler" in sys.argv[0]:
warnings.warn(
"dask-scheduler is deprecated and will be removed in a future release; use `dask scheduler` instead",
FutureWarning,
)
g0, g1, g2 = gc.get_threshold() # https://github.com/dask/distributed/issues/1653
gc.set_threshold(g0 * 3, g1 * 3, g2 * 3)
enable_proctitle_on_current()
enable_proctitle_on_children()
if interface and "," in interface:
interface = interface.split(",")
if protocol and "," in protocol:
protocol = protocol.split(",")
if port:
if "," in port:
port = [int(p) for p in port.split(",")]
else:
port = int(port)
if port is None and (not host or not re.search(r":\d", host)):
if isinstance(protocol, list):
port = [8786] + [0] * (len(protocol) - 1)
else:
port = 8786
if isinstance(protocol, list) or isinstance(port, list):
if (not isinstance(protocol, list) or not isinstance(port, list)) or len(
port
) != len(protocol):
raise ValueError("--protocol and --port must both be lists of equal length")
sec = {
k: v
for k, v in [
("tls_ca_file", tls_ca_file),
("tls_scheduler_cert", tls_cert),
("tls_scheduler_key", tls_key),
]
if v is not None
}
if not host and (tls_ca_file or tls_cert or tls_key):
host = "tls://"
if pid_file:
with open(pid_file, "w") as f:
f.write(str(os.getpid()))
def del_pid_file():
if os.path.exists(pid_file):
os.remove(pid_file)
atexit.register(del_pid_file)
if sys.platform.startswith("linux"):
import resource # module fails importing on Windows
soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
limit = max(soft, hard // 2)
resource.setrlimit(resource.RLIMIT_NOFILE, (limit, hard))
async def run():
logger.info("-" * 47)
scheduler = Scheduler(
security=sec,
host=host,
port=port,
protocol=protocol,
interface=interface,
dashboard=dashboard,
dashboard_address=dashboard_address,
http_prefix=dashboard_prefix,
jupyter=jupyter,
**kwargs,
)
logger.info("-" * 47)
async def wait_for_scheduler_to_finish():
"""Wait for the scheduler to initialize and finish"""
await scheduler
await scheduler.finished()
async def wait_for_signals_and_close():
"""Wait for SIGINT or SIGTERM and close the scheduler upon receiving one of those signals"""
await wait_for_signals()
await scheduler.close()
wait_for_signals_and_close_task = asyncio.create_task(
wait_for_signals_and_close()
)
wait_for_scheduler_to_finish_task = asyncio.create_task(
wait_for_scheduler_to_finish()
)
done, _ = await asyncio.wait(
[wait_for_signals_and_close_task, wait_for_scheduler_to_finish_task],
return_when=asyncio.FIRST_COMPLETED,
)
# Re-raise exceptions from done tasks
[task.result() for task in done]
logger.info("Stopped scheduler at %r", scheduler.address)
try:
asyncio.run(run())
finally:
logger.info("End scheduler")
if __name__ == "__main__":
main() # pragma: no cover
|