6
6
7
7
import fsspec
8
8
import psutil
9
+ from dask .distributed import Client as DaskClient
10
+ from distributed import LocalCluster
9
11
from jupyter_core .paths import jupyter_data_dir
10
12
from jupyter_server .transutils import _i18n
11
13
from jupyter_server .utils import to_os_path
@@ -381,6 +383,12 @@ def get_local_output_path(
381
383
else :
382
384
return os .path .join (self .root_dir , self .output_directory , output_dir_name )
383
385
386
+ async def stop_extension (self ):
387
+ """
388
+ Placeholder method for a cleanup code to run when the server is stopping.
389
+ """
390
+ pass
391
+
384
392
385
393
class Scheduler (BaseScheduler ):
386
394
_db_session = None
@@ -395,6 +403,12 @@ class Scheduler(BaseScheduler):
395
403
),
396
404
)
397
405
406
+ dask_cluster_url = Unicode (
407
+ allow_none = True ,
408
+ config = True ,
409
+ help = "URL of the Dask cluster to connect to." ,
410
+ )
411
+
398
412
db_url = Unicode (help = _i18n ("Scheduler database url" ))
399
413
400
414
task_runner = Instance (allow_none = True , klass = "jupyter_scheduler.task_runner.BaseTaskRunner" )
@@ -414,6 +428,15 @@ def __init__(
414
428
if self .task_runner_class :
415
429
self .task_runner = self .task_runner_class (scheduler = self , config = config )
416
430
431
+ self .dask_client : DaskClient = self ._get_dask_client ()
432
+
433
+ def _get_dask_client (self ):
434
+ """Creates and configures a Dask client."""
435
+ if self .dask_cluster_url :
436
+ return DaskClient (self .dask_cluster_url )
437
+ cluster = LocalCluster (processes = True )
438
+ return DaskClient (cluster )
439
+
417
440
@property
418
441
def db_session (self ):
419
442
if not self ._db_session :
@@ -777,6 +800,13 @@ def get_staging_paths(self, model: Union[DescribeJob, DescribeJobDefinition]) ->
777
800
778
801
return staging_paths
779
802
803
+ async def stop_extension (self ):
804
+ """
805
+ Cleanup code to run when the server is stopping.
806
+ """
807
+ if self .dask_client :
808
+ await self .dask_client .close ()
809
+
780
810
781
811
class ArchivingScheduler (Scheduler ):
782
812
"""Scheduler that captures all files in output directory in an archive."""
0 commit comments