Skip to content

Commit 986c914

Browse files
committed
Rename Job model to JobProcess
1 parent 0e08b9e commit 986c914

File tree

7 files changed

+122
-52
lines changed

7 files changed

+122
-52
lines changed

plain-worker/plain/worker/admin.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from plain.http import ResponseRedirect
1212
from plain.runtime import settings
1313

14-
from .models import Job, JobRequest, JobResult
14+
from .models import JobProcess, JobRequest, JobResult
1515

1616

1717
def _td_format(td_object):
@@ -87,14 +87,14 @@ class WaitingJobsCard(Card):
8787
title = "Waiting Jobs"
8888

8989
def get_number(self):
90-
return Job.query.waiting().count()
90+
return JobProcess.query.waiting().count()
9191

9292

9393
class RunningJobsCard(Card):
9494
title = "Running Jobs"
9595

9696
def get_number(self):
97-
return Job.query.running().count()
97+
return JobProcess.query.running().count()
9898

9999

100100
@register_viewset
@@ -117,11 +117,12 @@ class DetailView(AdminModelDetailView):
117117

118118

119119
@register_viewset
120-
class JobViewset(AdminViewset):
120+
class JobProcessViewset(AdminViewset):
121121
class ListView(AdminModelListView):
122122
nav_section = "Worker"
123123
nav_icon = "gear"
124-
model = Job
124+
model = JobProcess
125+
title = "Job processes"
125126
fields = [
126127
"id",
127128
"job_class",
@@ -138,10 +139,10 @@ class ListView(AdminModelListView):
138139

139140
def perform_action(self, action: str, target_ids: list):
140141
if action == "Delete":
141-
Job.query.filter(id__in=target_ids).delete()
142+
JobProcess.query.filter(id__in=target_ids).delete()
142143

143144
class DetailView(AdminModelDetailView):
144-
model = Job
145+
model = JobProcess
145146

146147

147148
@register_viewset
@@ -162,7 +163,7 @@ class ListView(AdminModelListView):
162163
]
163164
search_fields = [
164165
"uuid",
165-
"job_uuid",
166+
"job_process_uuid",
166167
"job_request_uuid",
167168
"job_class",
168169
]

plain-worker/plain/worker/cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from plain.runtime import settings
99
from plain.utils import timezone
1010

11-
from .models import Job, JobRequest, JobResult
11+
from .models import JobProcess, JobRequest, JobResult
1212
from .registry import jobs_registry
1313
from .scheduling import load_schedule
1414
from .workers import Worker
@@ -100,7 +100,7 @@ def clear_completed():
100100
def stats():
101101
"""Stats across all queues."""
102102
pending = JobRequest.query.count()
103-
processing = Job.query.count()
103+
processing = JobProcess.query.count()
104104

105105
successful = JobResult.query.successful().count()
106106
errored = JobResult.query.errored().count()
@@ -124,7 +124,7 @@ def purge_processing():
124124
deleted = JobRequest.query.all().delete()[0]
125125
click.echo(f"Deleted {deleted} job requests")
126126

127-
deleted = Job.query.all().delete()[0]
127+
deleted = JobProcess.query.all().delete()[0]
128128
click.echo(f"Deleted {deleted} jobs")
129129

130130

plain-worker/plain/worker/jobs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ def run_in_worker(
166166
return self._in_progress(unique_key)
167167

168168
def _in_progress(self, unique_key):
169-
"""Get all JobRequests and Jobs that are currently in progress, regardless of queue."""
170-
from .models import Job, JobRequest
169+
"""Get all JobRequests and JobProcess that are currently in progress, regardless of queue."""
170+
from .models import JobProcess, JobRequest
171171

172172
job_class_name = jobs_registry.get_job_class_name(self.__class__)
173173

@@ -176,7 +176,7 @@ def _in_progress(self, unique_key):
176176
unique_key=unique_key,
177177
)
178178

179-
jobs = Job.query.filter(
179+
jobs = JobProcess.query.filter(
180180
job_class=job_class_name,
181181
unique_key=unique_key,
182182
)

plain-worker/plain/worker/middleware.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ def __init__(self, run_job):
77

88
def __call__(self, job):
99
with app_logger.include_context(
10-
job_request_uuid=str(job.job_request_uuid), job_uuid=str(job.uuid)
10+
job_request_uuid=str(job.job_request_uuid), job_process_uuid=str(job.uuid)
1111
):
1212
return self.run_job(job)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Generated by Plain 0.63.0 on 2025-09-15 21:29
2+
3+
from plain import models
4+
from plain.models import migrations
5+
6+
7+
class Migration(migrations.Migration):
8+
dependencies = [
9+
("plainworker", "0002_job_span_id_job_trace_id_jobrequest_span_id_and_more"),
10+
]
11+
12+
operations = [
13+
migrations.RenameModel(
14+
old_name="Job",
15+
new_name="JobProcess",
16+
),
17+
migrations.RemoveIndex(
18+
model_name="jobresult",
19+
name="plainworker_job_uui_8307d1_idx",
20+
),
21+
migrations.RenameField(
22+
model_name="jobresult",
23+
old_name="job_uuid",
24+
new_name="job_process_uuid",
25+
),
26+
migrations.RenameIndex(
27+
model_name="jobprocess",
28+
new_name="plainworker_created_0d3928_idx",
29+
old_name="plainworker_created_a02317_idx",
30+
),
31+
migrations.RenameIndex(
32+
model_name="jobprocess",
33+
new_name="plainworker_queue_2550ba_idx",
34+
old_name="plainworker_queue_077806_idx",
35+
),
36+
migrations.RenameIndex(
37+
model_name="jobprocess",
38+
new_name="plainworker_unique__9dc0bb_idx",
39+
old_name="plainworker_unique__04d87b_idx",
40+
),
41+
migrations.RenameIndex(
42+
model_name="jobprocess",
43+
new_name="plainworker_started_b80ec5_idx",
44+
old_name="plainworker_started_143df5_idx",
45+
),
46+
migrations.RenameIndex(
47+
model_name="jobprocess",
48+
new_name="plainworker_job_cla_fe2b70_idx",
49+
old_name="plainworker_job_cla_884b46_idx",
50+
),
51+
migrations.RenameIndex(
52+
model_name="jobprocess",
53+
new_name="plainworker_job_req_357898_idx",
54+
old_name="plainworker_job_req_db2681_idx",
55+
),
56+
migrations.RenameIndex(
57+
model_name="jobprocess",
58+
new_name="plainworker_trace_i_da2cfa_idx",
59+
old_name="plainworker_trace_i_d2b645_idx",
60+
),
61+
migrations.AddIndex(
62+
model_name="jobresult",
63+
index=models.Index(
64+
fields=["job_process_uuid"], name="plainworker_job_pro_ceabfb_idx"
65+
),
66+
),
67+
]

plain-worker/plain/worker/models.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ class Meta:
8989
def __str__(self):
9090
return f"{self.job_class} [{self.uuid}]"
9191

92-
def convert_to_job(self):
92+
def convert_to_job_process(self):
9393
"""
9494
JobRequests are the pending jobs that are waiting to be executed.
95-
We immediately convert them to JobResults when they are picked up.
95+
We immediately convert them to JobProcess when they are picked up.
9696
"""
9797
with transaction.atomic():
98-
result = Job.query.create(
98+
result = JobProcess.query.create(
9999
job_request_uuid=self.uuid,
100100
job_class=self.job_class,
101101
parameters=self.parameters,
@@ -140,7 +140,7 @@ def mark_lost_jobs(self):
140140

141141

142142
@models.register_model
143-
class Job(models.Model):
143+
class JobProcess(models.Model):
144144
"""
145145
All active jobs are stored in this table.
146146
"""
@@ -240,15 +240,15 @@ def run(self):
240240

241241
def convert_to_result(self, *, status, error=""):
242242
"""
243-
Convert this Job to a JobResult.
243+
Convert this JobProcess to a JobResult.
244244
"""
245245
with transaction.atomic():
246246
result = JobResult.query.create(
247247
ended_at=timezone.now(),
248248
error=error,
249249
status=status,
250-
# From the Job
251-
job_uuid=self.uuid,
250+
# From the JobProcess
251+
job_process_uuid=self.uuid,
252252
started_at=self.started_at,
253253
# From the JobRequest
254254
job_request_uuid=self.job_request_uuid,
@@ -264,7 +264,7 @@ def convert_to_result(self, *, status, error=""):
264264
span_id=self.span_id,
265265
)
266266

267-
# Delete the Job now
267+
# Delete the JobProcess now
268268
self.delete()
269269

270270
return result
@@ -359,7 +359,7 @@ class JobResult(models.Model):
359359
created_at = models.DateTimeField(auto_now_add=True)
360360

361361
# From the Job
362-
job_uuid = models.UUIDField()
362+
job_process_uuid = models.UUIDField()
363363
started_at = models.DateTimeField(required=False, allow_null=True)
364364
ended_at = models.DateTimeField(required=False, allow_null=True)
365365
error = models.TextField(required=False)
@@ -391,7 +391,7 @@ class Meta:
391391
ordering = ["-created_at"]
392392
indexes = [
393393
models.Index(fields=["created_at"]),
394-
models.Index(fields=["job_uuid"]),
394+
models.Index(fields=["job_process_uuid"]),
395395
models.Index(fields=["started_at"]),
396396
models.Index(fields=["ended_at"]),
397397
models.Index(fields=["status"]),

plain-worker/plain/worker/workers.py

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from plain.utils import timezone
1414
from plain.utils.module_loading import import_string
1515

16-
from .models import Job, JobRequest, JobResult, JobResultStatuses
16+
from .models import JobProcess, JobRequest, JobResult, JobResultStatuses
1717
from .registry import jobs_registry
1818

1919
logger = logging.getLogger("plain.worker")
@@ -114,16 +114,18 @@ def run(self):
114114
job_request.queue,
115115
)
116116

117-
job = job_request.convert_to_job()
117+
job = job_request.convert_to_job_process()
118118

119-
job_uuid = str(job.uuid) # Make a str copy
119+
job_process_uuid = str(job.uuid) # Make a str copy
120120

121121
# Release these now
122122
del job_request
123123
del job
124124

125-
future = self.executor.submit(process_job, job_uuid)
126-
future.add_done_callback(partial(future_finished_callback, job_uuid))
125+
future = self.executor.submit(process_job, job_process_uuid)
126+
future.add_done_callback(
127+
partial(future_finished_callback, job_process_uuid)
128+
)
127129

128130
# Do a quick sleep regardless to see if it
129131
# gives processes a chance to start up
@@ -213,7 +215,7 @@ def log_stats(self):
213215
num_proccesses = 0
214216

215217
jobs_requested = JobRequest.query.filter(queue__in=self.queues).count()
216-
jobs_processing = Job.query.filter(queue__in=self.queues).count()
218+
jobs_processing = JobProcess.query.filter(queue__in=self.queues).count()
217219

218220
logger.info(
219221
'Job worker stats worker_processes=%s worker_queues="%s" jobs_requested=%s jobs_processing=%s worker_max_processes=%s worker_max_jobs_per_process=%s',
@@ -228,52 +230,52 @@ def log_stats(self):
228230
def rescue_job_results(self):
229231
"""Find any lost or failed jobs on this worker's queues and handle them."""
230232
# TODO return results and log them if there are any?
231-
Job.query.filter(queue__in=self.queues).mark_lost_jobs()
233+
JobProcess.query.filter(queue__in=self.queues).mark_lost_jobs()
232234
JobResult.query.filter(queue__in=self.queues).retry_failed_jobs()
233235

234236

235-
def future_finished_callback(job_uuid: str, future: Future):
237+
def future_finished_callback(job_process_uuid: str, future: Future):
236238
if future.cancelled():
237-
logger.warning("Job cancelled job_uuid=%s", job_uuid)
239+
logger.warning("Job cancelled job_process_uuid=%s", job_process_uuid)
238240
try:
239-
job = Job.query.get(uuid=job_uuid)
241+
job = JobProcess.query.get(uuid=job_process_uuid)
240242
job.convert_to_result(status=JobResultStatuses.CANCELLED)
241-
except Job.DoesNotExist:
243+
except JobProcess.DoesNotExist:
242244
# Job may have already been cleaned up
243245
pass
244246
elif exception := future.exception():
245247
# Process pool may have been killed...
246248
logger.warning(
247-
"Job failed job_uuid=%s",
248-
job_uuid,
249+
"Job failed job_process_uuid=%s",
250+
job_process_uuid,
249251
exc_info=exception,
250252
)
251253
try:
252-
job = Job.query.get(uuid=job_uuid)
254+
job = JobProcess.query.get(uuid=job_process_uuid)
253255
job.convert_to_result(status=JobResultStatuses.CANCELLED)
254-
except Job.DoesNotExist:
256+
except JobProcess.DoesNotExist:
255257
# Job may have already been cleaned up
256258
pass
257259
else:
258-
logger.debug("Job finished job_uuid=%s", job_uuid)
260+
logger.debug("Job finished job_process_uuid=%s", job_process_uuid)
259261

260262

261-
def process_job(job_uuid):
263+
def process_job(job_process_uuid):
262264
try:
263265
worker_pid = os.getpid()
264266

265267
request_started.send(sender=None)
266268

267-
job = Job.query.get(uuid=job_uuid)
269+
job_process = JobProcess.query.get(uuid=job_process_uuid)
268270

269271
logger.info(
270272
'Executing job worker_pid=%s job_class=%s job_request_uuid=%s job_priority=%s job_source="%s" job_queue="%s"',
271273
worker_pid,
272-
job.job_class,
273-
job.job_request_uuid,
274-
job.priority,
275-
job.source,
276-
job.queue,
274+
job_process.job_class,
275+
job_process.job_request_uuid,
276+
job_process.priority,
277+
job_process.source,
278+
job_process.queue,
277279
)
278280

279281
def middleware_chain(job):
@@ -284,19 +286,19 @@ def middleware_chain(job):
284286
middleware_instance = middleware_class(middleware_chain)
285287
middleware_chain = middleware_instance
286288

287-
job_result = middleware_chain(job)
289+
job_result = middleware_chain(job_process)
288290

289291
# Release it now
290-
del job
292+
del job_process
291293

292294
duration = job_result.ended_at - job_result.started_at
293295
duration = duration.total_seconds()
294296

295297
logger.info(
296-
'Completed job worker_pid=%s job_class=%s job_uuid=%s job_request_uuid=%s job_result_uuid=%s job_priority=%s job_source="%s" job_queue="%s" job_duration=%s',
298+
'Completed job worker_pid=%s job_class=%s job_process_uuid=%s job_request_uuid=%s job_result_uuid=%s job_priority=%s job_source="%s" job_queue="%s" job_duration=%s',
297299
worker_pid,
298300
job_result.job_class,
299-
job_result.job_uuid,
301+
job_result.job_process_uuid,
300302
job_result.job_request_uuid,
301303
job_result.uuid,
302304
job_result.priority,

0 commit comments

Comments
 (0)