Skip to content

Commit

Permalink
Merge branch 'master' of git@github.com:olivierdalang/django-toosimpl…
Browse files Browse the repository at this point in the history
…e-q.git into packaging

# Conflicts:
#	docker-compose.yml
  • Loading branch information
olivierdalang committed Sep 13, 2023
2 parents 08972e6 + 2d9eb49 commit 200a5e4
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 4 deletions.
58 changes: 58 additions & 0 deletions django_toosimple_q/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,32 @@ class TaskExecAdmin(ReadOnlyAdmin):
actions = ["action_requeue"]
ordering = ["-created"]
readonly_fields = ["task_", "result"]
fieldsets = [
(
None,
{"fields": ["icon", "task_name", "state", "task_"]},
),
(
"Arguments",
{"fields": ["args", "kwargs"]},
),
(
"Time",
{"fields": ["due_", "created_", "started_", "finished_"]},
),
(
"Retries",
{"fields": ["retries", "retry_delay", "replaced_by"]},
),
(
"Execution",
{"fields": ["worker", "error"]},
),
(
"Output",
{"fields": ["stdout", "stderr", "result"]},
),
]

def arguments_(self, obj):
return format_html(
Expand Down Expand Up @@ -132,6 +158,20 @@ class ScheduleExecAdmin(ReadOnlyAdmin):
list_filter = ["name", ScheduleQueueListFilter, "state"]
actions = ["action_force_run"]
readonly_fields = ["schedule_"]
fieldsets = [
(
None,
{"fields": ["icon", "name", "state", "schedule_"]},
),
(
"Time",
{"fields": ["last_due_", "next_due_"]},
),
(
"Execution",
{"fields": ["last_task_"]},
),
]

def schedule_(self, obj):
if not obj.schedule:
Expand Down Expand Up @@ -188,6 +228,24 @@ class WorkerStatusAdmin(ReadOnlyAdmin):
list_display_links = ["label"]
ordering = ["-started", "label"]
readonly_fields = ["state"]
fieldsets = [
(
None,
{"fields": ["icon", "label"]},
),
(
"Queues",
{"fields": ["included_queues", "excluded_queues"]},
),
(
"Time",
{"fields": ["timeout", "last_tick_", "started_", "stopped_"]},
),
(
"Exit state",
{"fields": ["exit_code", "exit_log"]},
),
]

@admin.display(ordering="last_tick")
def last_tick_(self, obj):
Expand Down
2 changes: 1 addition & 1 deletion django_toosimple_q/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .registry import schedules_registry, tasks_registry

formatter = logging.Formatter(
"[%(asctime)s %(levelname)s] [toosimpleq] %(message)s", "%Y-%m-%d %H:%M:%S"
"[%(asctime)s][%(levelname)s][toosimpleq] %(message)s", "%Y-%m-%d %H:%M:%S"
)

handler = logging.StreamHandler()
Expand Down
2 changes: 1 addition & 1 deletion django_toosimple_q/management/commands/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def do_loop(self) -> bool:
with transaction.atomic():
self.cur_task_exec = self._build_due_tasks_qs().first()
if self.cur_task_exec:
logger.debug(f"{self.cur_task_exec} is due !")
logger.debug(f"Picking up for execution : {self.cur_task_exec}")
self.cur_task_exec.started = now()
self.cur_task_exec.state = TaskExec.States.PROCESSING
self.cur_task_exec.worker = self.worker_status
Expand Down
3 changes: 3 additions & 0 deletions django_toosimple_q/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def icon(self):
return TaskExec.States.icon(self.state)

def execute(self):
logger.info(f"{self} started")
try:
# Get the task from the registry
task = tasks_registry[self.task_name]
Expand All @@ -120,6 +121,7 @@ def execute(self):
stdout, stderr = io.StringIO(), io.StringIO()
with redirect_stderr(stderr), redirect_stdout(stdout):
self.result = task.callable(*self.args, **self.kwargs)
logger.info(f"{self} succeeded")
self.state = TaskExec.States.SUCCEEDED
except Exception:
logger.warning(f"{self} failed !")
Expand Down Expand Up @@ -219,6 +221,7 @@ def execute(self):
did_something = False

if self.next_dues:
logger.info(f"{self} is due ({len(self.next_dues)} occurences)")
self.schedule.execute(self.next_dues)
did_something = True
self.last_due = self.next_dues[-1]
Expand Down
4 changes: 3 additions & 1 deletion django_toosimple_q/tests/demo/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
from ...models import TaskExec


@schedule_task(cron="* * * * * */30", datetime_kwarg="scheduled_time", queue="demo")
@schedule_task(
cron="* * * * * */30", datetime_kwarg="scheduled_time", queue="demo", catch_up=True
)
@register_task(name="say_hi", queue="demo")
def say_hi(scheduled_time):
if scheduled_time is None:
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ services:

worker:
<<: *default-django
command: worker --queue demo
command: worker --queue demo --verbosity 3

postgres:
image: postgres
Expand Down

0 comments on commit 200a5e4

Please sign in to comment.