-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathscheduler.py
More file actions
851 lines (700 loc) · 31.7 KB
/
scheduler.py
File metadata and controls
851 lines (700 loc) · 31.7 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
import multiprocessing as mp
import os
import random
import shutil
from typing import Dict, Optional, Type, Union
import fsspec
import psutil
from jupyter_core.paths import jupyter_data_dir
from jupyter_server.transutils import _i18n
from jupyter_server.utils import to_os_path
from sqlalchemy import and_, asc, desc, func
from traitlets import Instance
from traitlets import Type as TType
from traitlets import Unicode, default
from traitlets.config import LoggingConfigurable
from jupyter_scheduler.environments import EnvironmentManager
from jupyter_scheduler.exceptions import (
IdempotencyTokenError,
InputUriError,
SchedulerError,
)
from jupyter_scheduler.models import (
CountJobsQuery,
CreateJob,
CreateJobDefinition,
CreateJobFromDefinition,
DescribeJob,
DescribeJobDefinition,
JobFile,
ListJobDefinitionsQuery,
ListJobDefinitionsResponse,
ListJobsQuery,
ListJobsResponse,
SortDirection,
Status,
UpdateJob,
UpdateJobDefinition,
)
from jupyter_scheduler.orm import (
Job,
JobDefinition,
NotificationsConfigTable,
create_session,
)
from jupyter_scheduler.utils import create_output_directory, create_output_filename
class BaseScheduler(LoggingConfigurable):
"""Base class for schedulers. A default implementation
is provided in the `Scheduler` class, but extension creators
can provide their own scheduler by subclassing this class.
By implementing this class, you will replace both the service
API and the persistence layer for the scheduler.
"""
staging_path = Unicode(
config=True,
help=_i18n(
"""Full path to staging location, where output
files will be stored after job execution completes. This
could be a local or remote path including cloud storage.
Default value is jupyter data directory.
"""
),
)
output_directory = Unicode(
default_value="jobs",
config=True,
help=_i18n(
"""Local path to the directory where job files
will be downloaded. This directory will host sub-directories
for each new job.
"""
),
)
@default("staging_path")
def _default_staging_path(self):
return os.path.join(jupyter_data_dir(), "scheduler_staging_area")
execution_manager_class = TType(
allow_none=True,
klass="jupyter_scheduler.executors.ExecutionManager",
default_value="jupyter_scheduler.executors.DefaultExecutionManager",
config=True,
help=_i18n("The execution manager class to use."),
)
root_dir = Unicode(help=_i18n("Jupyter server root directory"))
environments_manager = Instance(
klass="jupyter_scheduler.environments.EnvironmentManager",
help=_i18n("Environment manager instance"),
)
def __init__(
self, root_dir: str, environments_manager: Type[EnvironmentManager], config=None, **kwargs
):
super().__init__(config=config, **kwargs)
self.root_dir = root_dir
self.environments_manager = environments_manager
def create_job(self, model: CreateJob) -> str:
"""Creates a new job record, may trigger execution of the job.
In case a task runner is actually handling execution of the jobs,
this method should just create the job record.
"""
raise NotImplementedError("must be implemented by subclass")
def update_job(self, job_id: str, model: UpdateJob):
"""Updates job metadata in the persistence store,
for example name, status etc. In case of status
change to STOPPED, should call stop_job
"""
raise NotImplementedError("must be implemented by subclass")
def list_jobs(self, query: ListJobsQuery) -> ListJobsResponse:
"""Returns list of all jobs filtered by query"""
raise NotImplementedError("must be implemented by subclass")
def count_jobs(self, query: CountJobsQuery) -> int:
"""Returns number of jobs filtered by query"""
raise NotImplementedError("must be implemented by subclass")
def get_job(self, job_id: str, job_files: Optional[bool] = True) -> DescribeJob:
"""Returns job record for a single job.
Parameters
----------
job_id : str
Unique identifier for the job
job_files : bool, optional
If True, checks for job files in local path
and populates the job_files property. When
False, `add_job_files` should not be called.
"""
raise NotImplementedError("must be implemented by subclass")
def delete_job(self, job_id: str):
"""Deletes the job record, stops the job if running"""
raise NotImplementedError("must be implemented by subclass")
def stop_job(self, job_id: str):
"""Stops the job, this is not analogous
to the REST API that will be called to
stop the job. Front end will call the PUT
API with status update to STOPPED, which will
call the stop_job method.
"""
raise NotImplementedError("must be implemented by subclass")
def create_job_definition(self, model: CreateJobDefinition) -> str:
"""Creates a new job definition record,
consider this as the template for creating
recurring/scheduled jobs.
"""
raise NotImplementedError("must be implemented by subclass")
def update_job_definition(self, job_definition_id: str, model: UpdateJobDefinition):
"""Updates job definition metadata in the persistence store,
should only impact all future jobs.
"""
raise NotImplementedError("must be implemented by subclass")
def delete_job_definition(self, job_definition_id: str):
"""Deletes the job definition record,
implementors can optionally stop all running jobs
"""
raise NotImplementedError("must be implemented by subclass")
def get_job_definition(self, job_definition_id: str) -> DescribeJobDefinition:
"""Returns job definition record for a single job definition"""
raise NotImplementedError("must be implemented by subclass")
def list_job_definitions(self, query: ListJobDefinitionsQuery) -> ListJobDefinitionsResponse:
"""Returns list of all job definitions filtered by query"""
raise NotImplementedError("must be implemented by subclass")
def create_job_from_definition(self, job_definition_id: str, model: CreateJobFromDefinition):
"""Creates a new job based on a job definition"""
raise NotImplementedError("must be implemented by subclass")
def get_staging_paths(self, model: Union[DescribeJob, DescribeJobDefinition]) -> Dict[str, str]:
"""Returns full staging paths for all job files
Notes
-----
Any path supported by `fsspec https://filesystem-spec.readthedocs.io/en/latest/index.html`_
is a valid return value. For staging files that
are stored as tar or compressed tar archives, this
should specify the first entry with a key as `tar`
or `tar.gz` and value as path to the archive file;
the values for the actual format files will be just
the path to them in the archive, in most cases just
the filename. For input files, the key 'input' is
expected.
Examples
--------
>>> self.get_staging_paths(1)
{
'ipynb': '/job_files/helloworld-2022-10-10.ipynb',
'html': '/job_files/helloworld-2022-10-10.html',
'input': '/job_files/helloworld.ipynb'
}
For files that are archived as tar or compressed tar
>>> self.get_staging_paths(2)
{
'tar.gz': '/job_files/helloworld.tar.gz',
'ipynb': 'helloworld-2022-10-10.ipynb',
'html': 'helloworld-2022-10-10.html',
'input': 'helloworld.ipynb'
}
Parameters
----------
job_id : str
Unique identifier for the job
Returns
-------
Dictionary with keys as output format and values
as full path to the job file in staging location.
"""
raise NotImplementedError("must be implemented by subclass")
def file_exists(self, path: str):
"""Returns True if the file exists, else returns False.
API-style wrapper for os.path.isfile
Parameters
----------
path : string
The relative path to the file (with '/' as separator)
Returns
-------
exists : bool
Whether the file exists.
"""
root = os.path.abspath(self.root_dir)
os_path = to_os_path(path, root)
if not (os.path.abspath(os_path) + os.path.sep).startswith(root):
return False
else:
return os.path.isfile(os_path)
def get_job_filenames(self, model: DescribeJob) -> Dict[str, str]:
"""Returns dictionary mapping output formats to
the job filenames in the JupyterLab workspace.
Notes
-----
This should be called by both `add_job_files` and
`JobFilesManager` to ensure that job output and
input files are written to the expected filepaths.
For input files, the key `input` is expected.
Examples
--------
>>> self.get_job_filenames(model)
{
'ipynb': 'helloworld-2022-10-10.ipynb',
'html': 'helloworld-2022-10-10.html',
'input': 'helloworld.ipynb'
}
"""
filenames = {}
for output_format in model.output_formats:
filenames[output_format] = create_output_filename(
model.input_filename, model.create_time, output_format
)
filenames["input"] = model.input_filename
return filenames
def add_job_files(self, model: DescribeJob):
"""Adds `job_files` to the model, ensures job files
are present in the local workspace. These should be
added in `get_job` and `list_job` APIs once the rest
of the model is populated.
"""
mapping = self.environments_manager.output_formats_mapping()
job_files = []
output_filenames = self.get_job_filenames(model)
output_dir = os.path.relpath(self.get_local_output_path(model), self.root_dir)
for output_format in model.output_formats:
filename = output_filenames[output_format]
output_path = os.path.join(output_dir, filename)
job_files.append(
JobFile(
display_name=mapping[output_format],
file_format=output_format,
file_path=output_path if self.file_exists(output_path) else None,
)
)
# Add input file
filename = model.input_filename
format = "input"
output_path = os.path.join(output_dir, filename)
job_files.append(
JobFile(
display_name="Input",
file_format=format,
file_path=output_path if self.file_exists(output_path) else None,
)
)
model.job_files = job_files
model.downloaded = all(job_file.file_path for job_file in job_files)
def get_local_output_path(self, model: DescribeJob) -> str:
"""Returns the local output directory path
where all the job files will be downloaded
from the staging location.
"""
output_dir_name = create_output_directory(model.input_filename, model.job_id)
return os.path.join(self.root_dir, self.output_directory, output_dir_name)
class Scheduler(BaseScheduler):
_db_session = None
task_runner_class = TType(
allow_none=True,
config=True,
default_value="jupyter_scheduler.task_runner.TaskRunner",
klass="jupyter_scheduler.task_runner.BaseTaskRunner",
help=_i18n(
"The class that handles the job creation of scheduled jobs from job definitions."
),
)
db_url = Unicode(help=_i18n("Scheduler database url"))
task_runner = Instance(allow_none=True, klass="jupyter_scheduler.task_runner.BaseTaskRunner")
def __init__(
self,
root_dir: str,
environments_manager: Type[EnvironmentManager],
db_url: str,
config=None,
**kwargs,
):
super().__init__(
root_dir=root_dir, environments_manager=environments_manager, config=config, **kwargs
)
self.db_url = db_url
if self.task_runner_class:
self.task_runner = self.task_runner_class(scheduler=self, config=config)
@property
def db_session(self):
if not self._db_session:
self._db_session = create_session(self.db_url)
return self._db_session
def copy_input_file(self, input_uri: str, copy_to_path: str):
"""Copies the input file to the staging directory"""
input_filepath = os.path.join(self.root_dir, input_uri)
with fsspec.open(input_filepath) as input_file:
with fsspec.open(copy_to_path, "wb") as output_file:
output_file.write(input_file.read())
def create_job(self, model: CreateJob) -> str:
if not model.job_definition_id and not self.file_exists(model.input_uri):
raise InputUriError(model.input_uri)
input_path = os.path.join(self.root_dir, model.input_uri)
if not self.execution_manager_class.validate(self.execution_manager_class, input_path):
raise SchedulerError(
"""There is no kernel associated with the notebook. Please open
the notebook, select a kernel, and re-submit the job to execute.
"""
)
with self.db_session() as session:
if model.idempotency_token:
job = (
session.query(Job)
.filter(Job.idempotency_token == model.idempotency_token)
.first()
)
if job:
raise IdempotencyTokenError(model.idempotency_token)
if not model.output_formats:
model.output_formats = []
orm_notifications_config = None
if model.notifications_config:
orm_notifications_config = NotificationsConfigTable(
**model.notifications_config.dict()
)
session.add(orm_notifications_config)
job_data = model.dict(exclude={"input_uri", "notifications_config"})
job_data["notifications_config"] = orm_notifications_config
job = Job(**job_data)
session.add(job)
session.commit()
staging_paths = self.get_staging_paths(DescribeJob.from_orm(job))
self.copy_input_file(model.input_uri, staging_paths["input"])
# The MP context forces new processes to not be forked on Linux.
# This is necessary because `asyncio.get_event_loop()` is bugged in
# forked processes in Python versions below 3.12. This method is
# called by `jupyter_core` by `nbconvert` in the default executor.
#
# See: https://github.com/python/cpython/issues/66285
# See also: https://github.com/jupyter/jupyter_core/pull/362
mp_ctx = mp.get_context("spawn")
p = mp_ctx.Process(
target=self.execution_manager_class(
job_id=job.job_id,
staging_paths=staging_paths,
root_dir=self.root_dir,
db_url=self.db_url,
).process
)
p.start()
job.pid = p.pid
session.commit()
job_id = job.job_id
return job_id
def update_job(self, job_id: str, model: UpdateJob):
with self.db_session() as session:
session.query(Job).filter(Job.job_id == job_id).update(model.dict(exclude_none=True))
session.commit()
def list_jobs(self, query: ListJobsQuery) -> ListJobsResponse:
with self.db_session() as session:
jobs = session.query(Job)
if query.status:
jobs = jobs.filter(Job.status == query.status)
if query.job_definition_id:
jobs = jobs.filter(Job.job_definition_id == query.job_definition_id)
if query.start_time:
jobs = jobs.filter(Job.start_time >= query.start_time)
if query.name:
jobs = jobs.filter(Job.name.like(f"{query.name}%"))
if query.tags:
jobs = jobs.filter(and_(Job.tags.contains(tag) for tag in query.tags))
total = jobs.count()
if query.sort_by:
for sort_field in query.sort_by:
direction = desc if sort_field.direction == SortDirection.desc else asc
jobs = jobs.order_by(direction(getattr(Job, sort_field.name)))
next_token = int(query.next_token) if query.next_token else 0
jobs = jobs.limit(query.max_items).offset(next_token)
jobs = jobs.all()
next_token = next_token + len(jobs)
if next_token >= total:
next_token = None
jobs_list = []
for job in jobs:
model = DescribeJob.from_orm(job)
self.add_job_files(model=model)
jobs_list.append(model)
list_jobs_response = ListJobsResponse(
jobs=jobs_list,
next_token=next_token,
total_count=total,
)
return list_jobs_response
def count_jobs(self, query: CountJobsQuery) -> int:
with self.db_session() as session:
count = (
session.query(func.count(Job.job_id)).filter(Job.status == query.status).scalar()
)
return count if count else 0
def get_job(self, job_id: str, job_files: Optional[bool] = True) -> DescribeJob:
with self.db_session() as session:
job_record = session.query(Job).filter(Job.job_id == job_id).one()
model = DescribeJob.from_orm(job_record)
if job_files:
self.add_job_files(model=model)
return model
def delete_job(self, job_id: str):
with self.db_session() as session:
job_record = session.query(Job).filter(Job.job_id == job_id).one()
if Status(job_record.status) == Status.IN_PROGRESS:
self.stop_job(job_id)
staging_paths = self.get_staging_paths(DescribeJob.from_orm(job_record))
if staging_paths:
path = os.path.dirname(next(iter(staging_paths.values())))
if os.path.exists(path):
shutil.rmtree(path)
session.query(Job).filter(Job.job_id == job_id).delete()
session.commit()
def stop_job(self, job_id):
with self.db_session() as session:
job_record = session.query(Job).filter(Job.job_id == job_id).one()
job = DescribeJob.from_orm(job_record)
process_id = job_record.pid
if process_id and job.status == Status.IN_PROGRESS:
session.query(Job).filter(Job.job_id == job_id).update({"status": Status.STOPPING})
session.commit()
current_process = psutil.Process()
children = current_process.children(recursive=True)
for proc in children:
if process_id == proc.pid:
proc.kill()
session.query(Job).filter(Job.job_id == job_id).update(
{"status": Status.STOPPED}
)
session.commit()
break
def create_job_definition(self, model: CreateJobDefinition) -> str:
with self.db_session() as session:
if not self.file_exists(model.input_uri):
raise InputUriError(model.input_uri)
orm_notifications_config = None
if model.notifications_config:
orm_notifications_config = NotificationsConfigTable(
**model.notifications_config.dict()
)
session.add(orm_notifications_config)
session.flush()
job_definition_data = model.dict(
exclude={"input_uri", "notifications_config"}, exclude_none=True
)
job_definition = JobDefinition(
**job_definition_data, notifications_config=orm_notifications_config
)
session.add(job_definition)
session.commit()
job_definition_id = job_definition.job_definition_id
staging_paths = self.get_staging_paths(DescribeJobDefinition.from_orm(job_definition))
self.copy_input_file(model.input_uri, staging_paths["input"])
if self.task_runner and job_definition.schedule:
self.task_runner.add_job_definition(job_definition_id)
return job_definition_id
def update_job_definition(self, job_definition_id: str, model: UpdateJobDefinition):
with self.db_session() as session:
filtered_query = session.query(JobDefinition).filter(
JobDefinition.job_definition_id == job_definition_id
)
describe_job_definition = DescribeJobDefinition.from_orm(filtered_query.one())
if (
(
not model.input_uri
or (
model.input_uri
and describe_job_definition.input_filename
== os.path.basename(model.input_uri)
)
)
and describe_job_definition.schedule == model.schedule
and describe_job_definition.timezone == model.timezone
and (model.active == None or describe_job_definition.active == model.active)
):
return
updates = model.dict(exclude_none=True, exclude={"input_uri"})
if model.input_uri:
new_input_filename = os.path.basename(model.input_uri)
staging_paths = self.get_staging_paths(describe_job_definition)
staging_directory = os.path.dirname(staging_paths["input"])
self.copy_input_file(
model.input_uri, os.path.join(staging_directory, new_input_filename)
)
updates["input_filename"] = new_input_filename
filtered_query.update(updates)
session.commit()
schedule = (
session.query(JobDefinition.schedule)
.filter(JobDefinition.job_definition_id == job_definition_id)
.scalar()
)
if self.task_runner and schedule:
self.task_runner.update_job_definition(job_definition_id, model)
def delete_job_definition(self, job_definition_id: str):
with self.db_session() as session:
jobs = session.query(Job).filter(Job.job_definition_id == job_definition_id)
for job in jobs:
self.delete_job(job.job_id)
schedule = (
session.query(JobDefinition.schedule)
.filter(JobDefinition.job_definition_id == job_definition_id)
.scalar()
)
session.query(JobDefinition).filter(
JobDefinition.job_definition_id == job_definition_id
).delete()
session.commit()
if self.task_runner and schedule:
self.task_runner.delete_job_definition(job_definition_id)
def get_job_definition(self, job_definition_id: str) -> DescribeJobDefinition:
with self.db_session() as session:
job_definition = (
session.query(JobDefinition)
.filter(JobDefinition.job_definition_id == job_definition_id)
.one()
)
return DescribeJobDefinition.from_orm(job_definition)
def list_job_definitions(self, query: ListJobDefinitionsQuery) -> ListJobDefinitionsResponse:
with self.db_session() as session:
definitions = session.query(JobDefinition)
if query.create_time:
definitions = definitions.filter(JobDefinition.create_time >= query.create_time)
if query.name:
definitions = definitions.filter(JobDefinition.name.like(f"{query.name}%"))
if query.tags:
definitions = definitions.filter(
and_(JobDefinition.tags.contains(tag) for tag in query.tags)
)
total = definitions.count()
if query.sort_by:
for sort_field in query.sort_by:
direction = desc if sort_field.direction == SortDirection.desc else asc
definitions = definitions.order_by(
direction(getattr(JobDefinition, sort_field.name))
)
next_token = int(query.next_token) if query.next_token else 0
definitions = definitions.limit(query.max_items).offset(next_token)
definitions = definitions.all()
next_token = next_token + len(definitions)
if next_token >= total:
next_token = None
list_response = ListJobDefinitionsResponse(
job_definitions=[
DescribeJobDefinition.from_orm(definition) for definition in definitions or []
],
next_token=next_token,
total_count=total,
)
return list_response
def create_job_from_definition(self, job_definition_id: str, model: CreateJobFromDefinition):
job_id = None
definition = self.get_job_definition(job_definition_id)
if definition:
input_uri = self.get_staging_paths(definition)["input"]
attributes = definition.dict(exclude={"schedule", "timezone"}, exclude_none=True)
attributes = {**attributes, **model.dict(exclude_none=True), "input_uri": input_uri}
job_id = self.create_job(CreateJob(**attributes))
return job_id
def get_staging_paths(self, model: Union[DescribeJob, DescribeJobDefinition]) -> Dict[str, str]:
staging_paths = {}
if not model:
return staging_paths
id = model.job_id if isinstance(model, DescribeJob) else model.job_definition_id
for output_format in model.output_formats:
filename = create_output_filename(
model.input_filename, model.create_time, output_format
)
staging_paths[output_format] = os.path.join(self.staging_path, id, filename)
staging_paths["input"] = os.path.join(self.staging_path, id, model.input_filename)
return staging_paths
class ArchivingScheduler(Scheduler):
"""Scheduler that captures all files in output directory in an archive."""
execution_manager_class = TType(
klass="jupyter_scheduler.executors.ExecutionManager",
default_value="jupyter_scheduler.executors.ArchivingExecutionManager",
config=True,
)
def get_staging_paths(self, model: Union[DescribeJob, DescribeJobDefinition]) -> Dict[str, str]:
staging_paths = {}
if not model:
return staging_paths
id = model.job_id if isinstance(model, DescribeJob) else model.job_definition_id
for output_format in model.output_formats:
filename = create_output_filename(
model.input_filename, model.create_time, output_format
)
# Use the staging directory to capture output files
staging_paths[output_format] = os.path.join(self.staging_path, id, filename)
# Create an output archive file
staging_paths["tar.gz"] = os.path.join(
self.staging_path,
id,
create_output_filename(model.input_filename, model.create_time, "tar.gz"),
)
staging_paths["input"] = os.path.join(self.staging_path, id, model.input_filename)
return staging_paths
class SchedulerWithErrors(Scheduler):
"""
Use only for testing exceptions, not to be used in production
This scheduler uses the default `Scheduler`, but randomly
raises the `SchedulerError` to help view/test errors in the
UI. To use this, specify the fully classified class name at
at start up or add to one of the server config files.
Usage
-----
>> jupyter lab --SchedulerApp.scheduler_class=jupyter_scheduler.scheduler.SchedulerWithErrors
"""
def _should_raise_error(self, probability=0.5):
return random.random() < probability
def create_job(self, model: CreateJob) -> str:
if self._should_raise_error():
raise SchedulerError("Failed create job because of a deliberate exception.")
else:
return super().create_job(model)
def update_job(self, job_id: str, model: UpdateJob):
if self._should_raise_error():
raise SchedulerError("Failed update job because of a deliberate exception.")
else:
super().update_job(job_id, model)
def list_jobs(self, query: ListJobsQuery) -> ListJobsResponse:
if self._should_raise_error():
raise SchedulerError("Failed list jobs because of a deliberate exception.")
else:
return super().list_jobs(query)
def count_jobs(self, query: CountJobsQuery) -> int:
if self._should_raise_error():
raise SchedulerError("Failed count jobs because of a deliberate exception.")
else:
return super().count_jobs(query)
def get_job(self, job_id: str, job_files: Optional[bool] = True) -> DescribeJob:
if self._should_raise_error():
raise SchedulerError("Failed get job because of a deliberate exception.")
else:
return super().get_job(job_id, job_files)
def delete_job(self, job_id: str):
if self._should_raise_error():
raise SchedulerError("Failed delete job because of a deliberate exception.")
else:
super().delete_job(job_id)
def stop_job(self, job_id: str):
if self._should_raise_error():
raise SchedulerError("Failed stop job because of a deliberate exception.")
else:
super().stop_job(job_id)
def create_job_definition(self, model: CreateJobDefinition) -> str:
if self._should_raise_error():
raise SchedulerError("Failed create job definition because of a deliberate exception.")
else:
return super().create_job_definition(model)
def update_job_definition(self, job_definition_id: str, model: UpdateJobDefinition):
if self._should_raise_error():
raise SchedulerError("Failed update job definition because of a deliberate exception.")
else:
super().update_job_definition(job_definition_id, model)
def delete_job_definition(self, job_definition_id: str):
if self._should_raise_error():
raise SchedulerError("Failed delete job definition because of a deliberate exception.")
else:
super().delete_job_definition(job_definition_id)
def get_job_definition(self, job_definition_id: str) -> DescribeJobDefinition:
if self._should_raise_error():
raise SchedulerError("Failed get job definition because of a deliberate exception.")
else:
return super().get_job_definition(job_definition_id)
def list_job_definitions(self, query: ListJobDefinitionsQuery) -> ListJobDefinitionsResponse:
if self._should_raise_error():
raise SchedulerError("Failed list job definitions because of a deliberate exception.")
else:
return super().list_job_definitions(query)
def create_job_from_definition(self, job_definition_id: str, model: CreateJobFromDefinition):
if self._should_raise_error():
raise SchedulerError("Failed list jobs because of a deliberate exception.")
else:
return super().create_job_from_definition(job_definition_id, model)