samco-job-lifecycle 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 samcoai
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,42 @@
1
+ Metadata-Version: 2.4
2
+ Name: samco-job-lifecycle
3
+ Version: 0.1.0
4
+ Summary: A small, dependency-free state machine for tracking job lifecycles
5
+ Author: samcoai
6
+ License: MIT
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: Programming Language :: Python :: 3 :: Only
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.9
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Dynamic: license-file
15
+
16
+ # samco-job-lifecycle
17
+
18
+ A small, dependency-free Python library for tracking the state of a job. It validates transitions, records UTC timestamps, and keeps an in-memory event history. It does not run jobs or store them in a database.
19
+
20
+ ## Install
21
+
22
+ ```sh
23
+ pip install samco-job-lifecycle
24
+ ```
25
+
26
+ ## Example
27
+
28
+ ```python
29
+ from samco_job_lifecycle import Job, JobStatus
30
+
31
+ job = Job("report-42")
32
+ job.start()
33
+ job.succeed()
34
+
35
+ assert job.status is JobStatus.SUCCEEDED
36
+ assert job.is_terminal
37
+ print(job.events)
38
+ ```
39
+
40
+ Valid paths are `pending -> running -> succeeded|failed|cancelled` and `pending -> cancelled`. Terminal jobs cannot transition again. Invalid changes raise `InvalidTransition`.
41
+
42
+ Requires Python 3.9 or newer.
@@ -0,0 +1,27 @@
1
+ # samco-job-lifecycle
2
+
3
+ A small, dependency-free Python library for tracking the state of a job. It validates transitions, records UTC timestamps, and keeps an in-memory event history. It does not run jobs or store them in a database.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ pip install samco-job-lifecycle
9
+ ```
10
+
11
+ ## Example
12
+
13
+ ```python
14
+ from samco_job_lifecycle import Job, JobStatus
15
+
16
+ job = Job("report-42")
17
+ job.start()
18
+ job.succeed()
19
+
20
+ assert job.status is JobStatus.SUCCEEDED
21
+ assert job.is_terminal
22
+ print(job.events)
23
+ ```
24
+
25
+ Valid paths are `pending -> running -> succeeded|failed|cancelled` and `pending -> cancelled`. Terminal jobs cannot transition again. Invalid changes raise `InvalidTransition`.
26
+
27
+ Requires Python 3.9 or newer.
@@ -0,0 +1,21 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "samco-job-lifecycle"
7
+ version = "0.1.0"
8
+ description = "A small, dependency-free state machine for tracking job lifecycles"
9
+ readme = "README.md"
10
+ requires-python = ">=3.9"
11
+ license = { text = "MIT" }
12
+ authors = [{ name = "samcoai" }]
13
+ classifiers = [
14
+ "Programming Language :: Python :: 3",
15
+ "Programming Language :: Python :: 3 :: Only",
16
+ "License :: OSI Approved :: MIT License",
17
+ "Operating System :: OS Independent",
18
+ ]
19
+
20
+ [tool.setuptools.packages.find]
21
+ where = ["src"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,5 @@
1
+ """Track jobs through a small, explicit lifecycle."""
2
+
3
+ from .job import Job, JobEvent, JobStatus, InvalidTransition
4
+
5
+ __all__ = ["Job", "JobEvent", "JobStatus", "InvalidTransition"]
@@ -0,0 +1,85 @@
1
+ """In-memory job lifecycle primitives."""
2
+
3
+ from dataclasses import dataclass, field
4
+ from datetime import datetime, timezone
5
+ from enum import Enum
6
+ from typing import List, Optional, Tuple
7
+
8
+
9
+ class JobStatus(str, Enum):
10
+ PENDING = "pending"
11
+ RUNNING = "running"
12
+ SUCCEEDED = "succeeded"
13
+ FAILED = "failed"
14
+ CANCELLED = "cancelled"
15
+
16
+
17
+ class InvalidTransition(ValueError):
18
+ """Raised when a requested state change is not allowed."""
19
+
20
+
21
+ @dataclass(frozen=True)
22
+ class JobEvent:
23
+ status: JobStatus
24
+ occurred_at: datetime
25
+ reason: Optional[str] = None
26
+
27
+
28
+ _ALLOWED = {
29
+ JobStatus.PENDING: frozenset((JobStatus.RUNNING, JobStatus.CANCELLED)),
30
+ JobStatus.RUNNING: frozenset((JobStatus.SUCCEEDED, JobStatus.FAILED, JobStatus.CANCELLED)),
31
+ JobStatus.SUCCEEDED: frozenset(),
32
+ JobStatus.FAILED: frozenset(),
33
+ JobStatus.CANCELLED: frozenset(),
34
+ }
35
+
36
+
37
+ @dataclass
38
+ class Job:
39
+ """A single in-memory job with validated state changes and an event history.
40
+
41
+ This class does not execute work or persist data. Its methods mutate the job.
42
+ """
43
+
44
+ id: str
45
+ status: JobStatus = field(default=JobStatus.PENDING, init=False)
46
+ _events: List[JobEvent] = field(default_factory=list, init=False, repr=False)
47
+
48
+ def __post_init__(self) -> None:
49
+ if not isinstance(self.id, str) or not self.id.strip():
50
+ raise ValueError("job id must be a non-empty string")
51
+ self._events.append(JobEvent(self.status, datetime.now(timezone.utc)))
52
+
53
+ @property
54
+ def events(self) -> Tuple[JobEvent, ...]:
55
+ """Return an immutable view of the recorded state changes."""
56
+ return tuple(self._events)
57
+
58
+ @property
59
+ def is_terminal(self) -> bool:
60
+ return not _ALLOWED[self.status]
61
+
62
+ def transition(self, status: JobStatus, *, reason: Optional[str] = None) -> JobEvent:
63
+ """Change status and record a UTC timestamp; reject invalid transitions."""
64
+ if not isinstance(status, JobStatus):
65
+ raise TypeError("status must be a JobStatus")
66
+ if reason is not None and not isinstance(reason, str):
67
+ raise TypeError("reason must be a string or None")
68
+ if status not in _ALLOWED[self.status]:
69
+ raise InvalidTransition(f"cannot transition from {self.status.value} to {status.value}")
70
+ event = JobEvent(status, datetime.now(timezone.utc), reason)
71
+ self.status = status
72
+ self._events.append(event)
73
+ return event
74
+
75
+ def start(self) -> JobEvent:
76
+ return self.transition(JobStatus.RUNNING)
77
+
78
+ def succeed(self) -> JobEvent:
79
+ return self.transition(JobStatus.SUCCEEDED)
80
+
81
+ def fail(self, reason: Optional[str] = None) -> JobEvent:
82
+ return self.transition(JobStatus.FAILED, reason=reason)
83
+
84
+ def cancel(self, reason: Optional[str] = None) -> JobEvent:
85
+ return self.transition(JobStatus.CANCELLED, reason=reason)
@@ -0,0 +1,42 @@
1
+ Metadata-Version: 2.4
2
+ Name: samco-job-lifecycle
3
+ Version: 0.1.0
4
+ Summary: A small, dependency-free state machine for tracking job lifecycles
5
+ Author: samcoai
6
+ License: MIT
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: Programming Language :: Python :: 3 :: Only
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.9
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Dynamic: license-file
15
+
16
+ # samco-job-lifecycle
17
+
18
+ A small, dependency-free Python library for tracking the state of a job. It validates transitions, records UTC timestamps, and keeps an in-memory event history. It does not run jobs or store them in a database.
19
+
20
+ ## Install
21
+
22
+ ```sh
23
+ pip install samco-job-lifecycle
24
+ ```
25
+
26
+ ## Example
27
+
28
+ ```python
29
+ from samco_job_lifecycle import Job, JobStatus
30
+
31
+ job = Job("report-42")
32
+ job.start()
33
+ job.succeed()
34
+
35
+ assert job.status is JobStatus.SUCCEEDED
36
+ assert job.is_terminal
37
+ print(job.events)
38
+ ```
39
+
40
+ Valid paths are `pending -> running -> succeeded|failed|cancelled` and `pending -> cancelled`. Terminal jobs cannot transition again. Invalid changes raise `InvalidTransition`.
41
+
42
+ Requires Python 3.9 or newer.
@@ -0,0 +1,10 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ src/samco_job_lifecycle/__init__.py
5
+ src/samco_job_lifecycle/job.py
6
+ src/samco_job_lifecycle.egg-info/PKG-INFO
7
+ src/samco_job_lifecycle.egg-info/SOURCES.txt
8
+ src/samco_job_lifecycle.egg-info/dependency_links.txt
9
+ src/samco_job_lifecycle.egg-info/top_level.txt
10
+ tests/test_job.py
@@ -0,0 +1 @@
1
+ samco_job_lifecycle
@@ -0,0 +1,41 @@
1
+ import unittest
2
+
3
+ from samco_job_lifecycle import InvalidTransition, Job, JobStatus
4
+
5
+
6
+ class JobTests(unittest.TestCase):
7
+ def test_success_path_records_ordered_utc_events(self):
8
+ job = Job("a")
9
+ job.start()
10
+ job.succeed()
11
+ self.assertEqual([event.status for event in job.events], [
12
+ JobStatus.PENDING, JobStatus.RUNNING, JobStatus.SUCCEEDED,
13
+ ])
14
+ self.assertTrue(job.is_terminal)
15
+ self.assertTrue(all(event.occurred_at.utcoffset().total_seconds() == 0 for event in job.events))
16
+
17
+ def test_failure_records_reason(self):
18
+ job = Job("a")
19
+ job.start()
20
+ job.fail("network")
21
+ self.assertEqual(job.events[-1].reason, "network")
22
+
23
+ def test_cancel_pending_and_reject_terminal_transition(self):
24
+ job = Job("a")
25
+ job.cancel()
26
+ with self.assertRaises(InvalidTransition):
27
+ job.start()
28
+ self.assertEqual(len(job.events), 2)
29
+
30
+ def test_reject_invalid_input(self):
31
+ with self.assertRaises(ValueError):
32
+ Job(" ")
33
+ job = Job("a")
34
+ with self.assertRaises(InvalidTransition):
35
+ job.succeed()
36
+ with self.assertRaises(TypeError):
37
+ job.transition("running")
38
+
39
+
40
+ if __name__ == "__main__":
41
+ unittest.main()