Skip to content

jobs

Attributes

logger = structlog.getLogger() module-attribute

MANIFEST_SUB_PATH = 'manifests' module-attribute

Classes

JobArchive

Bases: BaseArchive

Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
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
class JobArchive(BaseArchive):
    # @abc.abstractmethod
    # def find_matching_job_record(
    #     self, inputs_manifest: InputsManifest
    # ) -> Optional[JobRecord]:
    #     pass

    @abc.abstractmethod
    def retrieve_all_job_hashes(
        self,
        manifest_hash: Union[str, None] = None,
        inputs_hash: Union[str, None] = None,
    ) -> Iterable[str]:
        """
        Retrieve a list of all job record hashes (cids) that match the given filter arguments.

        A job record hash includes information about the module type used in the job, the module configuration, as well as input field names and value ids for the values used in those inputs.

        If the job archive retrieves its jobs in a dynamic way, this will return 'None'.
        """

    @abc.abstractmethod
    def _retrieve_record_for_job_hash(self, job_hash: str) -> Union[JobRecord, None]:
        pass

    def retrieve_record_for_job_hash(self, job_hash: str) -> Union[JobRecord, None]:

        job_record = self._retrieve_record_for_job_hash(job_hash=job_hash)
        return job_record

Functions

retrieve_all_job_hashes(manifest_hash: Union[str, None] = None, inputs_hash: Union[str, None] = None) -> Iterable[str] abstractmethod

Retrieve a list of all job record hashes (cids) that match the given filter arguments.

A job record hash includes information about the module type used in the job, the module configuration, as well as input field names and value ids for the values used in those inputs.

If the job archive retrieves its jobs in a dynamic way, this will return 'None'.

Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
46
47
48
49
50
51
52
53
54
55
56
57
58
@abc.abstractmethod
def retrieve_all_job_hashes(
    self,
    manifest_hash: Union[str, None] = None,
    inputs_hash: Union[str, None] = None,
) -> Iterable[str]:
    """
    Retrieve a list of all job record hashes (cids) that match the given filter arguments.

    A job record hash includes information about the module type used in the job, the module configuration, as well as input field names and value ids for the values used in those inputs.

    If the job archive retrieves its jobs in a dynamic way, this will return 'None'.
    """
retrieve_record_for_job_hash(job_hash: str) -> Union[JobRecord, None]
Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
64
65
66
67
def retrieve_record_for_job_hash(self, job_hash: str) -> Union[JobRecord, None]:

    job_record = self._retrieve_record_for_job_hash(job_hash=job_hash)
    return job_record

JobStore

Bases: JobArchive

Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
70
71
72
73
class JobStore(JobArchive):
    @abc.abstractmethod
    def store_job_record(self, job_record: JobRecord):
        pass

Functions

store_job_record(job_record: JobRecord) abstractmethod
Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
71
72
73
@abc.abstractmethod
def store_job_record(self, job_record: JobRecord):
    pass

JobMatcher

Bases: abc.ABC

Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
76
77
78
79
80
81
82
83
84
85
class JobMatcher(abc.ABC):
    def __init__(self, kiara: "Kiara"):

        self._kiara: Kiara = kiara

    @abc.abstractmethod
    def find_existing_job(
        self, inputs_manifest: InputsManifest
    ) -> Union[JobRecord, None]:
        pass

Functions

find_existing_job(inputs_manifest: InputsManifest) -> Union[JobRecord, None] abstractmethod
Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
81
82
83
84
85
@abc.abstractmethod
def find_existing_job(
    self, inputs_manifest: InputsManifest
) -> Union[JobRecord, None]:
    pass

NoneJobMatcher

Bases: JobMatcher

Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
88
89
90
91
92
class NoneJobMatcher(JobMatcher):
    def find_existing_job(
        self, inputs_manifest: InputsManifest
    ) -> Union[JobRecord, None]:
        return None

Functions

find_existing_job(inputs_manifest: InputsManifest) -> Union[JobRecord, None]
Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
89
90
91
92
def find_existing_job(
    self, inputs_manifest: InputsManifest
) -> Union[JobRecord, None]:
    return None

ValueIdJobMatcher

Bases: JobMatcher

Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
 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
class ValueIdJobMatcher(JobMatcher):
    def find_existing_job(
        self, inputs_manifest: InputsManifest
    ) -> Union[JobRecord, None]:

        matches = []

        for store_id, archive in self._kiara.job_registry.job_archives.items():

            match = archive.retrieve_record_for_job_hash(
                job_hash=inputs_manifest.job_hash
            )
            if match:
                matches.append(match)

        if len(matches) == 0:
            return None
        elif len(matches) > 1:
            raise Exception(
                f"Multiple stores have a record for inputs manifest '{inputs_manifest}', this is not supported (yet)."
            )

        job_record = matches[0]
        job_record._is_stored = True

        return job_record

Functions

find_existing_job(inputs_manifest: InputsManifest) -> Union[JobRecord, None]
Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
 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
def find_existing_job(
    self, inputs_manifest: InputsManifest
) -> Union[JobRecord, None]:

    matches = []

    for store_id, archive in self._kiara.job_registry.job_archives.items():

        match = archive.retrieve_record_for_job_hash(
            job_hash=inputs_manifest.job_hash
        )
        if match:
            matches.append(match)

    if len(matches) == 0:
        return None
    elif len(matches) > 1:
        raise Exception(
            f"Multiple stores have a record for inputs manifest '{inputs_manifest}', this is not supported (yet)."
        )

    job_record = matches[0]
    job_record._is_stored = True

    return job_record

DataHashJobMatcher

Bases: JobMatcher

Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
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
class DataHashJobMatcher(JobMatcher):
    def find_existing_job(
        self, inputs_manifest: InputsManifest
    ) -> Union[JobRecord, None]:

        matches = []

        for store_id, archive in self._kiara.job_registry.job_archives.items():

            match = archive.retrieve_record_for_job_hash(
                job_hash=inputs_manifest.job_hash
            )
            if match:
                matches.append(match)

        if len(matches) > 1:
            raise Exception(
                f"Multiple stores have a record for inputs manifest '{inputs_manifest}', this is not supported (yet)."
            )

        elif len(matches) == 1:

            job_record = matches[0]
            job_record._is_stored = True

            return job_record

        inputs_data_cid = inputs_manifest.calculate_inputs_data_cid(
            data_registry=self._kiara.data_registry
        )
        if not inputs_data_cid:
            return None

        inputs_data_hash = str(inputs_data_cid)

        matching_records = []
        for store_id, archive in self._kiara.job_registry.job_archives.items():
            _matches = archive.retrieve_all_job_hashes(
                manifest_hash=inputs_manifest.manifest_hash
            )
            for _match in _matches:
                _job_record = archive.retrieve_record_for_job_hash(_match)
                assert _job_record is not None
                if _job_record.inputs_data_hash == inputs_data_hash:
                    matching_records.append(_job_record)

        if not matching_records:
            return None
        elif len(matches) > 1:
            raise Exception(
                f"Multiple stores have a record for inputs manifest '{inputs_manifest}', this is not supported (yet)."
            )
        else:
            return matching_records[0]

Functions

find_existing_job(inputs_manifest: InputsManifest) -> Union[JobRecord, None]
Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
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
def find_existing_job(
    self, inputs_manifest: InputsManifest
) -> Union[JobRecord, None]:

    matches = []

    for store_id, archive in self._kiara.job_registry.job_archives.items():

        match = archive.retrieve_record_for_job_hash(
            job_hash=inputs_manifest.job_hash
        )
        if match:
            matches.append(match)

    if len(matches) > 1:
        raise Exception(
            f"Multiple stores have a record for inputs manifest '{inputs_manifest}', this is not supported (yet)."
        )

    elif len(matches) == 1:

        job_record = matches[0]
        job_record._is_stored = True

        return job_record

    inputs_data_cid = inputs_manifest.calculate_inputs_data_cid(
        data_registry=self._kiara.data_registry
    )
    if not inputs_data_cid:
        return None

    inputs_data_hash = str(inputs_data_cid)

    matching_records = []
    for store_id, archive in self._kiara.job_registry.job_archives.items():
        _matches = archive.retrieve_all_job_hashes(
            manifest_hash=inputs_manifest.manifest_hash
        )
        for _match in _matches:
            _job_record = archive.retrieve_record_for_job_hash(_match)
            assert _job_record is not None
            if _job_record.inputs_data_hash == inputs_data_hash:
                matching_records.append(_job_record)

    if not matching_records:
        return None
    elif len(matches) > 1:
        raise Exception(
            f"Multiple stores have a record for inputs manifest '{inputs_manifest}', this is not supported (yet)."
        )
    else:
        return matching_records[0]

JobRegistry

Bases: object

Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
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
class JobRegistry(object):
    def __init__(self, kiara: "Kiara"):

        self._kiara: Kiara = kiara

        self._job_matcher_cache: Dict[JobCacheStrategy, JobMatcher] = {}

        self._active_jobs: bidict[str, uuid.UUID] = bidict()
        self._failed_jobs: Dict[str, uuid.UUID] = {}
        self._finished_jobs: Dict[str, uuid.UUID] = {}
        self._archived_records: Dict[uuid.UUID, JobRecord] = {}

        self._processor: ModuleProcessor = SynchronousProcessor(kiara=self._kiara)
        self._processor.register_job_status_listener(self)
        self._job_archives: Dict[str, JobArchive] = {}
        self._default_job_store: Union[str, None] = None

        self._event_callback = self._kiara.event_registry.add_producer(self)

        # default_archive = FileSystemJobStore.create_from_kiara_context(self._kiara)
        # self.register_job_archive(default_archive, store_alias=DEFAULT_STORE_MARKER)

        # default_file_store = self._kiara.data_registry.get_archive(DEFAULT_STORE_MARKER)
        # self.register_job_archive(default_file_store, store_alias="default_data_store")  # type: ignore

    @property
    def job_matcher(self) -> JobMatcher:

        from kiara.context.runtime_config import JobCacheStrategy

        strategy = self._kiara.runtime_config.job_cache
        if is_develop():
            dev_config = get_dev_config()
            if not dev_config.job_cache:
                logger.debug(
                    "disable.job_cache",
                    reason="dev mode enabled and 'disable_job_cache' is set.",
                )
                strategy = JobCacheStrategy.no_cache

        job_matcher = self._job_matcher_cache.get(strategy, None)
        if job_matcher is None:
            if strategy == JobCacheStrategy.no_cache:
                job_matcher = NoneJobMatcher(kiara=self._kiara)
            elif strategy == JobCacheStrategy.value_id:
                job_matcher = ValueIdJobMatcher(kiara=self._kiara)
            elif strategy == JobCacheStrategy.data_hash:
                job_matcher = DataHashJobMatcher(kiara=self._kiara)
            else:
                raise Exception(f"Job cache strategy not implemented: {strategy}")
            self._job_matcher_cache[strategy] = job_matcher

        return job_matcher

    def suppoerted_event_types(self) -> Iterable[Type[KiaraEvent]]:

        return [JobArchiveAddedEvent, JobRecordPreStoreEvent, JobRecordStoredEvent]

    def register_job_archive(self, archive: JobArchive, alias: Union[str, None] = None):

        if alias is None:
            alias = str(archive.archive_id)

        if alias in self._job_archives.keys():
            raise Exception(
                f"Can't register job store, store id already registered: {alias}."
            )

        self._job_archives[alias] = archive

        is_store = False
        is_default_store = False
        if isinstance(archive, JobStore):
            is_store = True
            if self._default_job_store is None:
                self._default_job_store = alias

        event = JobArchiveAddedEvent.construct(
            kiara_id=self._kiara.id,
            job_archive_id=archive.archive_id,
            job_archive_alias=alias,
            is_store=is_store,
            is_default_store=is_default_store,
        )
        self._event_callback(event)

    @property
    def default_job_store(self) -> str:

        if self._default_job_store is None:
            raise Exception("No default job store set (yet).")
        return self._default_job_store  # type: ignore

    def get_archive(self, store_id: Union[str, None] = None) -> JobArchive:

        if store_id is None:
            store_id = self.default_job_store
            if store_id is None:
                raise Exception("Can't retrieve deafult job archive, none set (yet).")

        return self._job_archives[store_id]

    @property
    def job_archives(self) -> Mapping[str, JobArchive]:
        return self._job_archives

    def job_status_changed(
        self,
        job_id: uuid.UUID,
        old_status: Union[JobStatus, None],
        new_status: JobStatus,
    ):

        # print(f"JOB STATUS CHANGED: {job_id} - {old_status} - {new_status.value}")
        if job_id in self._active_jobs.values() and new_status is JobStatus.FAILED:
            job_hash = self._active_jobs.inverse.pop(job_id)
            self._failed_jobs[job_hash] = job_id
        elif job_id in self._active_jobs.values() and new_status is JobStatus.SUCCESS:
            job_hash = self._active_jobs.inverse.pop(job_id)

            job_record = self._processor.get_job_record(job_id)

            self._finished_jobs[job_hash] = job_id
            self._archived_records[job_id] = job_record

    def store_job_record(self, job_id: uuid.UUID):

        if job_id not in self._archived_records.keys():
            raise Exception(
                f"Can't store job with id '{job_id}': no job record with that id exists."
            )

        job_record = self._archived_records[job_id]

        if job_record._is_stored:
            logger.debug(
                "ignore.store.job_record", reason="already stored", job_id=str(job_id)
            )
            return

        store: JobStore = self.get_archive()  # type: ignore
        if not isinstance(store, JobStore):
            raise Exception("Can't store job record to archive: not writable.")

        # if job_record.job_id in self._finished_jobs.values():
        #     logger.debug(
        #         "ignore.store.job_record",
        #         reason="already stored in store",
        #         job_id=str(job_id),
        #     )
        #     return

        logger.debug(
            "store.job_record",
            job_hash=job_record.job_hash,
            module_type=job_record.module_type,
        )

        pre_store_event = JobRecordPreStoreEvent.construct(
            kiara_id=self._kiara.id, job_record=job_record
        )
        self._event_callback(pre_store_event)

        store.store_job_record(job_record)

        stored_event = JobRecordStoredEvent.construct(
            kiara_id=self._kiara.id, job_record=job_record
        )
        self._event_callback(stored_event)

    def get_job_record_in_session(self, job_id: uuid.UUID) -> JobRecord:

        return self._processor.get_job_record(job_id)

    def get_job_record(self, job_id: uuid.UUID) -> Union[JobRecord, None]:

        if job_id in self._archived_records.keys():
            return self._archived_records[job_id]

        try:
            job_record = self._processor.get_job_record(job_id=job_id)
            return job_record
        except Exception:
            pass

        job = self._processor.get_job(job_id=job_id)
        if job is not None:
            if job.status == JobStatus.FAILED:
                return None

        raise NotImplementedError()

    def retrieve_all_job_records(self) -> Mapping[str, JobRecord]:

        all_records: Dict[str, JobRecord] = {}
        for archive in self.job_archives.values():
            all_record_ids = archive.retrieve_all_job_hashes()
            if all_record_ids is None:
                return {}
            for r in all_record_ids:
                assert r not in all_records.keys()
                job_record = archive.retrieve_record_for_job_hash(r)
                assert job_record is not None
                all_records[r] = job_record

        return all_records

    def find_matching_job_record(
        self, inputs_manifest: InputsManifest
    ) -> Union[uuid.UUID, None]:
        """
        Check if a job with same inputs manifest already ran some time before.

        Arguments:
        ---------
            inputs_manifest: the manifest incl. inputs

        Returns:
        -------
            'None' if no such job exists, a (uuid) job-id if the job is currently running or has run in the past
        """
        log = logger.bind(module_type=inputs_manifest.module_type)
        if inputs_manifest.job_hash in self._active_jobs.keys():
            log.debug("job.use_running")
            return self._active_jobs[inputs_manifest.job_hash]

        if inputs_manifest.job_hash in self._finished_jobs.keys():
            job_id = self._finished_jobs[inputs_manifest.job_hash]
            return job_id

        module = self._kiara.module_registry.create_module(manifest=inputs_manifest)
        if not module.characteristics.is_idempotent:
            log.debug(
                "skip.job_matching",
                reason="module is not idempotent",
                module_type=inputs_manifest.module_type,
            )
            return None

        job_record = self.job_matcher.find_existing_job(inputs_manifest=inputs_manifest)
        if job_record is None:
            return None

        self._finished_jobs[inputs_manifest.job_hash] = job_record.job_id
        self._archived_records[job_record.job_id] = job_record
        log.debug(
            "job.found_cached_record",
            job_id=str(job_record.job_id),
            job_hash=inputs_manifest.job_hash,
            module_type=inputs_manifest.module_type,
        )
        return job_record.job_id

    def prepare_job_config(
        self, manifest: Manifest, inputs: Mapping[str, Any]
    ) -> JobConfig:

        module = self._kiara.module_registry.create_module(manifest=manifest)

        job_config = JobConfig.create_from_module(
            data_registry=self._kiara.data_registry, module=module, inputs=inputs
        )

        return job_config

    def execute(
        self,
        manifest: Manifest,
        inputs: Mapping[str, Any],
        wait: bool = False,
        job_metadata: Union[None, Any] = None,
    ) -> uuid.UUID:

        job_config = self.prepare_job_config(manifest=manifest, inputs=inputs)
        return self.execute_job(job_config, wait=wait, job_metadata=job_metadata)

    def execute_job(
        self,
        job_config: JobConfig,
        wait: bool = False,
        job_metadata: Union[None, Any] = None,
    ) -> uuid.UUID:

        log = logger.bind(
            module_type=job_config.module_type,
            module_config=job_config.module_config,
            inputs={k: str(v) for k, v in job_config.inputs.items()},
            job_hash=job_config.job_hash,
        )

        stored_job = self.find_matching_job_record(inputs_manifest=job_config)
        if stored_job is not None:
            log.debug(
                "job.use_cached",
                job_id=str(stored_job),
                module_type=job_config.module_type,
            )
            return stored_job

        if job_metadata is None:
            job_metadata = {}

        is_pipeline_step = job_metadata.get("is_pipeline_step", False)
        dbg_data = {
            "module_type": job_config.module_type,
            "is_pipeline_step": is_pipeline_step,
        }
        if is_pipeline_step:
            dbg_data["step_id"] = job_metadata["step_id"]
        log.debug("job.execute", **dbg_data)

        job_id = self._processor.create_job(
            job_config=job_config, job_metadata=job_metadata
        )
        self._active_jobs[job_config.job_hash] = job_id

        try:
            self._processor.queue_job(job_id=job_id)
        except Exception as e:
            log.error("error.queue_job", job_id=job_id)
            raise e

        if wait:
            self._processor.wait_for(job_id)

        return job_id

    def get_active_job(self, job_id: uuid.UUID) -> ActiveJob:

        if job_id in self._active_jobs.keys() or job_id in self._failed_jobs.keys():
            return self._processor.get_job(job_id)
        else:
            if job_id in self._archived_records.keys():
                raise Exception(
                    f"Can't retrieve active job with id '{job_id}': job is archived."
                )
            elif job_id in self._processor._failed_jobs.keys():
                job = self._processor.get_job(job_id)
                msg = job.error
                if not msg and job._exception:
                    msg = str(job._exception)
                    if not msg:
                        msg = repr(job._exception)
                raise FailedJobException(job=job, msg=msg, parent=job._exception)
            else:
                raise Exception(f"Can't retrieve job with id '{job_id}': no such job.")

    def get_job(self, job_id: uuid.UUID) -> ActiveJob:
        return self._processor.get_job(job_id=job_id)

    def get_job_status(self, job_id: uuid.UUID) -> JobStatus:

        if job_id in self._archived_records.keys():
            return JobStatus.SUCCESS
        elif job_id in self._failed_jobs.values():
            return JobStatus.FAILED

        return self._processor.get_job_status(job_id=job_id)

    def wait_for(self, *job_id: uuid.UUID):
        not_finished = (j for j in job_id if j not in self._archived_records.keys())
        if not_finished:
            self._processor.wait_for(*not_finished)

    def retrieve_result(self, job_id: uuid.UUID) -> ValueMapReadOnly:

        if job_id not in self._archived_records.keys():
            self._processor.wait_for(job_id)

        if job_id in self._archived_records.keys():
            job_record = self._archived_records[job_id]
            results = self._kiara.data_registry.load_values(job_record.outputs)
            return results
        elif job_id in self._failed_jobs.values():
            j = self._processor.get_job(job_id=job_id)
            raise FailedJobException(job=j, parent=j._exception)
        else:
            raise Exception(f"Could not find job with id: {job_id}")

    def execute_and_retrieve(
        self, manifest: Manifest, inputs: Mapping[str, Any]
    ) -> ValueMap:

        job_id = self.execute(manifest=manifest, inputs=inputs, wait=True)
        results = self.retrieve_result(job_id=job_id)
        return results

Attributes

job_matcher: JobMatcher property
default_job_store: str property
job_archives: Mapping[str, JobArchive] property

Functions

suppoerted_event_types() -> Iterable[Type[KiaraEvent]]
Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
233
234
235
def suppoerted_event_types(self) -> Iterable[Type[KiaraEvent]]:

    return [JobArchiveAddedEvent, JobRecordPreStoreEvent, JobRecordStoredEvent]
register_job_archive(archive: JobArchive, alias: Union[str, None] = None)
Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
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
def register_job_archive(self, archive: JobArchive, alias: Union[str, None] = None):

    if alias is None:
        alias = str(archive.archive_id)

    if alias in self._job_archives.keys():
        raise Exception(
            f"Can't register job store, store id already registered: {alias}."
        )

    self._job_archives[alias] = archive

    is_store = False
    is_default_store = False
    if isinstance(archive, JobStore):
        is_store = True
        if self._default_job_store is None:
            self._default_job_store = alias

    event = JobArchiveAddedEvent.construct(
        kiara_id=self._kiara.id,
        job_archive_id=archive.archive_id,
        job_archive_alias=alias,
        is_store=is_store,
        is_default_store=is_default_store,
    )
    self._event_callback(event)
get_archive(store_id: Union[str, None] = None) -> JobArchive
Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
272
273
274
275
276
277
278
279
def get_archive(self, store_id: Union[str, None] = None) -> JobArchive:

    if store_id is None:
        store_id = self.default_job_store
        if store_id is None:
            raise Exception("Can't retrieve deafult job archive, none set (yet).")

    return self._job_archives[store_id]
job_status_changed(job_id: uuid.UUID, old_status: Union[JobStatus, None], new_status: JobStatus)
Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
def job_status_changed(
    self,
    job_id: uuid.UUID,
    old_status: Union[JobStatus, None],
    new_status: JobStatus,
):

    # print(f"JOB STATUS CHANGED: {job_id} - {old_status} - {new_status.value}")
    if job_id in self._active_jobs.values() and new_status is JobStatus.FAILED:
        job_hash = self._active_jobs.inverse.pop(job_id)
        self._failed_jobs[job_hash] = job_id
    elif job_id in self._active_jobs.values() and new_status is JobStatus.SUCCESS:
        job_hash = self._active_jobs.inverse.pop(job_id)

        job_record = self._processor.get_job_record(job_id)

        self._finished_jobs[job_hash] = job_id
        self._archived_records[job_id] = job_record
store_job_record(job_id: uuid.UUID)
Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
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
def store_job_record(self, job_id: uuid.UUID):

    if job_id not in self._archived_records.keys():
        raise Exception(
            f"Can't store job with id '{job_id}': no job record with that id exists."
        )

    job_record = self._archived_records[job_id]

    if job_record._is_stored:
        logger.debug(
            "ignore.store.job_record", reason="already stored", job_id=str(job_id)
        )
        return

    store: JobStore = self.get_archive()  # type: ignore
    if not isinstance(store, JobStore):
        raise Exception("Can't store job record to archive: not writable.")

    # if job_record.job_id in self._finished_jobs.values():
    #     logger.debug(
    #         "ignore.store.job_record",
    #         reason="already stored in store",
    #         job_id=str(job_id),
    #     )
    #     return

    logger.debug(
        "store.job_record",
        job_hash=job_record.job_hash,
        module_type=job_record.module_type,
    )

    pre_store_event = JobRecordPreStoreEvent.construct(
        kiara_id=self._kiara.id, job_record=job_record
    )
    self._event_callback(pre_store_event)

    store.store_job_record(job_record)

    stored_event = JobRecordStoredEvent.construct(
        kiara_id=self._kiara.id, job_record=job_record
    )
    self._event_callback(stored_event)
get_job_record_in_session(job_id: uuid.UUID) -> JobRecord
Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
349
350
351
def get_job_record_in_session(self, job_id: uuid.UUID) -> JobRecord:

    return self._processor.get_job_record(job_id)
get_job_record(job_id: uuid.UUID) -> Union[JobRecord, None]
Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
def get_job_record(self, job_id: uuid.UUID) -> Union[JobRecord, None]:

    if job_id in self._archived_records.keys():
        return self._archived_records[job_id]

    try:
        job_record = self._processor.get_job_record(job_id=job_id)
        return job_record
    except Exception:
        pass

    job = self._processor.get_job(job_id=job_id)
    if job is not None:
        if job.status == JobStatus.FAILED:
            return None

    raise NotImplementedError()
retrieve_all_job_records() -> Mapping[str, JobRecord]
Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
371
372
373
374
375
376
377
378
379
380
381
382
383
384
def retrieve_all_job_records(self) -> Mapping[str, JobRecord]:

    all_records: Dict[str, JobRecord] = {}
    for archive in self.job_archives.values():
        all_record_ids = archive.retrieve_all_job_hashes()
        if all_record_ids is None:
            return {}
        for r in all_record_ids:
            assert r not in all_records.keys()
            job_record = archive.retrieve_record_for_job_hash(r)
            assert job_record is not None
            all_records[r] = job_record

    return all_records
find_matching_job_record(inputs_manifest: InputsManifest) -> Union[uuid.UUID, None]

Check if a job with same inputs manifest already ran some time before.


inputs_manifest: the manifest incl. inputs

'None' if no such job exists, a (uuid) job-id if the job is currently running or has run in the past
Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
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
def find_matching_job_record(
    self, inputs_manifest: InputsManifest
) -> Union[uuid.UUID, None]:
    """
    Check if a job with same inputs manifest already ran some time before.

    Arguments:
    ---------
        inputs_manifest: the manifest incl. inputs

    Returns:
    -------
        'None' if no such job exists, a (uuid) job-id if the job is currently running or has run in the past
    """
    log = logger.bind(module_type=inputs_manifest.module_type)
    if inputs_manifest.job_hash in self._active_jobs.keys():
        log.debug("job.use_running")
        return self._active_jobs[inputs_manifest.job_hash]

    if inputs_manifest.job_hash in self._finished_jobs.keys():
        job_id = self._finished_jobs[inputs_manifest.job_hash]
        return job_id

    module = self._kiara.module_registry.create_module(manifest=inputs_manifest)
    if not module.characteristics.is_idempotent:
        log.debug(
            "skip.job_matching",
            reason="module is not idempotent",
            module_type=inputs_manifest.module_type,
        )
        return None

    job_record = self.job_matcher.find_existing_job(inputs_manifest=inputs_manifest)
    if job_record is None:
        return None

    self._finished_jobs[inputs_manifest.job_hash] = job_record.job_id
    self._archived_records[job_record.job_id] = job_record
    log.debug(
        "job.found_cached_record",
        job_id=str(job_record.job_id),
        job_hash=inputs_manifest.job_hash,
        module_type=inputs_manifest.module_type,
    )
    return job_record.job_id
prepare_job_config(manifest: Manifest, inputs: Mapping[str, Any]) -> JobConfig
Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
432
433
434
435
436
437
438
439
440
441
442
def prepare_job_config(
    self, manifest: Manifest, inputs: Mapping[str, Any]
) -> JobConfig:

    module = self._kiara.module_registry.create_module(manifest=manifest)

    job_config = JobConfig.create_from_module(
        data_registry=self._kiara.data_registry, module=module, inputs=inputs
    )

    return job_config
execute(manifest: Manifest, inputs: Mapping[str, Any], wait: bool = False, job_metadata: Union[None, Any] = None) -> uuid.UUID
Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
444
445
446
447
448
449
450
451
452
453
def execute(
    self,
    manifest: Manifest,
    inputs: Mapping[str, Any],
    wait: bool = False,
    job_metadata: Union[None, Any] = None,
) -> uuid.UUID:

    job_config = self.prepare_job_config(manifest=manifest, inputs=inputs)
    return self.execute_job(job_config, wait=wait, job_metadata=job_metadata)
execute_job(job_config: JobConfig, wait: bool = False, job_metadata: Union[None, Any] = None) -> uuid.UUID
Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
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
def execute_job(
    self,
    job_config: JobConfig,
    wait: bool = False,
    job_metadata: Union[None, Any] = None,
) -> uuid.UUID:

    log = logger.bind(
        module_type=job_config.module_type,
        module_config=job_config.module_config,
        inputs={k: str(v) for k, v in job_config.inputs.items()},
        job_hash=job_config.job_hash,
    )

    stored_job = self.find_matching_job_record(inputs_manifest=job_config)
    if stored_job is not None:
        log.debug(
            "job.use_cached",
            job_id=str(stored_job),
            module_type=job_config.module_type,
        )
        return stored_job

    if job_metadata is None:
        job_metadata = {}

    is_pipeline_step = job_metadata.get("is_pipeline_step", False)
    dbg_data = {
        "module_type": job_config.module_type,
        "is_pipeline_step": is_pipeline_step,
    }
    if is_pipeline_step:
        dbg_data["step_id"] = job_metadata["step_id"]
    log.debug("job.execute", **dbg_data)

    job_id = self._processor.create_job(
        job_config=job_config, job_metadata=job_metadata
    )
    self._active_jobs[job_config.job_hash] = job_id

    try:
        self._processor.queue_job(job_id=job_id)
    except Exception as e:
        log.error("error.queue_job", job_id=job_id)
        raise e

    if wait:
        self._processor.wait_for(job_id)

    return job_id
get_active_job(job_id: uuid.UUID) -> ActiveJob
Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
def get_active_job(self, job_id: uuid.UUID) -> ActiveJob:

    if job_id in self._active_jobs.keys() or job_id in self._failed_jobs.keys():
        return self._processor.get_job(job_id)
    else:
        if job_id in self._archived_records.keys():
            raise Exception(
                f"Can't retrieve active job with id '{job_id}': job is archived."
            )
        elif job_id in self._processor._failed_jobs.keys():
            job = self._processor.get_job(job_id)
            msg = job.error
            if not msg and job._exception:
                msg = str(job._exception)
                if not msg:
                    msg = repr(job._exception)
            raise FailedJobException(job=job, msg=msg, parent=job._exception)
        else:
            raise Exception(f"Can't retrieve job with id '{job_id}': no such job.")
get_job(job_id: uuid.UUID) -> ActiveJob
Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
526
527
def get_job(self, job_id: uuid.UUID) -> ActiveJob:
    return self._processor.get_job(job_id=job_id)
get_job_status(job_id: uuid.UUID) -> JobStatus
Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
529
530
531
532
533
534
535
536
def get_job_status(self, job_id: uuid.UUID) -> JobStatus:

    if job_id in self._archived_records.keys():
        return JobStatus.SUCCESS
    elif job_id in self._failed_jobs.values():
        return JobStatus.FAILED

    return self._processor.get_job_status(job_id=job_id)
wait_for(*job_id: uuid.UUID)
Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
538
539
540
541
def wait_for(self, *job_id: uuid.UUID):
    not_finished = (j for j in job_id if j not in self._archived_records.keys())
    if not_finished:
        self._processor.wait_for(*not_finished)
retrieve_result(job_id: uuid.UUID) -> ValueMapReadOnly
Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
543
544
545
546
547
548
549
550
551
552
553
554
555
556
def retrieve_result(self, job_id: uuid.UUID) -> ValueMapReadOnly:

    if job_id not in self._archived_records.keys():
        self._processor.wait_for(job_id)

    if job_id in self._archived_records.keys():
        job_record = self._archived_records[job_id]
        results = self._kiara.data_registry.load_values(job_record.outputs)
        return results
    elif job_id in self._failed_jobs.values():
        j = self._processor.get_job(job_id=job_id)
        raise FailedJobException(job=j, parent=j._exception)
    else:
        raise Exception(f"Could not find job with id: {job_id}")
execute_and_retrieve(manifest: Manifest, inputs: Mapping[str, Any]) -> ValueMap
Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/kiara/registries/jobs/__init__.py
558
559
560
561
562
563
564
def execute_and_retrieve(
    self, manifest: Manifest, inputs: Mapping[str, Any]
) -> ValueMap:

    job_id = self.execute(manifest=manifest, inputs=inputs, wait=True)
    results = self.retrieve_result(job_id=job_id)
    return results

Functions