Skip to content

Commit 8b5b76a

Browse files
authored
Use real enum for JobState instead of many constant (#25)
When I put in job state constants in #19, I didn't think to check first whether there was a concept of real enums in Python. Turns out there is, and we should use one instead of a whole bunch of raw constants. This is a breaking change, but the job states have existed for so little time that I doubt it'll break anybody, so it's worth doing it in this instance. And a few other small changes: * Add typing to all exported constants. * Fix an "invalid escape sequence" in the tag validation regex stemming from the `\A` in it by making the string a "raw string" with `r`.
1 parent f0f7c95 commit 8b5b76a

File tree

3 files changed

+28
-25
lines changed

3 files changed

+28
-25
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- Use real enum for `JobState` instead of many constant. This is a breaking change, but the job state constants have existed for only a short time. [PR #25](https://github.com/riverqueue/riverqueue-python/pull/25).
13+
1014
## [0.4.0] - 2024-07-05
1115

1216
### Changed

src/riverqueue/__init__.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
# Reexport for more ergonomic use in calling code.
22
from .client import (
3-
JOB_STATE_AVAILABLE as JOB_STATE_AVAILABLE,
4-
JOB_STATE_CANCELLED as JOB_STATE_CANCELLED,
5-
JOB_STATE_COMPLETED as JOB_STATE_COMPLETED,
6-
JOB_STATE_DISCARDED as JOB_STATE_DISCARDED,
7-
JOB_STATE_RETRYABLE as JOB_STATE_RETRYABLE,
8-
JOB_STATE_RUNNING as JOB_STATE_RUNNING,
9-
JOB_STATE_SCHEDULED as JOB_STATE_SCHEDULED,
103
AsyncClient as AsyncClient,
114
JobArgs as JobArgs,
125
JobArgsWithInsertOpts as JobArgsWithInsertOpts,
6+
JobState as JobState,
137
Client as Client,
148
InsertManyParams as InsertManyParams,
159
InsertOpts as InsertOpts,

src/riverqueue/client.py

+23-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from dataclasses import dataclass
22
from datetime import datetime, timezone, timedelta
3+
from enum import Enum
34
import re
45
from typing import (
56
Any,
@@ -18,23 +19,27 @@
1819
from .model import InsertResult
1920
from .fnv import fnv1_hash
2021

21-
JOB_STATE_AVAILABLE = "available"
22-
JOB_STATE_CANCELLED = "cancelled"
23-
JOB_STATE_COMPLETED = "completed"
24-
JOB_STATE_DISCARDED = "discarded"
25-
JOB_STATE_RETRYABLE = "retryable"
26-
JOB_STATE_RUNNING = "running"
27-
JOB_STATE_SCHEDULED = "scheduled"
28-
29-
MAX_ATTEMPTS_DEFAULT = 25
30-
PRIORITY_DEFAULT = 1
31-
QUEUE_DEFAULT = "default"
32-
UNIQUE_STATES_DEFAULT = [
33-
JOB_STATE_AVAILABLE,
34-
JOB_STATE_COMPLETED,
35-
JOB_STATE_RUNNING,
36-
JOB_STATE_RETRYABLE,
37-
JOB_STATE_SCHEDULED,
22+
23+
class JobState(str, Enum):
24+
AVAILABLE = "available"
25+
CANCELLED = "cancelled"
26+
COMPLETED = "completed"
27+
DISCARDED = "discarded"
28+
PENDING = "pending"
29+
RETRYABLE = "retryable"
30+
RUNNING = "running"
31+
SCHEDULED = "scheduled"
32+
33+
34+
MAX_ATTEMPTS_DEFAULT: int = 25
35+
PRIORITY_DEFAULT: int = 1
36+
QUEUE_DEFAULT: str = "default"
37+
UNIQUE_STATES_DEFAULT: list[str] = [
38+
JobState.AVAILABLE,
39+
JobState.COMPLETED,
40+
JobState.RUNNING,
41+
JobState.RETRYABLE,
42+
JobState.SCHEDULED,
3843
]
3944

4045

@@ -351,7 +356,7 @@ def _uint64_to_int64(uint64):
351356
return (uint64 + (1 << 63)) % (1 << 64) - (1 << 63)
352357

353358

354-
tag_re = re.compile("\A[\w][\w\-]+[\w]\Z")
359+
tag_re = re.compile(r"\A[\w][\w\-]+[\w]\Z")
355360

356361

357362
def _validate_tags(tags: list[str]) -> list[str]:

0 commit comments

Comments
 (0)