Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move parts to SignalInfoManager model #146

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion huey_monitor/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import logging
import uuid
import os
import threading
import socket

from bx_django_utils.models.timetracking import TimetrackingBaseModel
from django.db import models
Expand All @@ -13,13 +16,18 @@


try:
from functools import cached_property # new in Python 3.8
from functools import lru_cache, cached_property # new in Python 3.8
except ImportError:
from django.utils.functional import cached_property


logger = logging.getLogger(__name__)

lru_cache(maxsize=None)
def get_hostname():
return socket.gethostname()



class TaskManager(models.Manager):
def set_parent_task(self, main_task_id, sub_task_id):
Expand Down Expand Up @@ -255,6 +263,16 @@ class SignalInfoModel(models.Model):
help_text=_('(will be set automatically)')
)

def __init__(self, *args, **kwargs):
if not 'hostname' in kwargs:
kwargs['hostname'] = get_hostname()
if not 'pid' in kwargs:
kwargs['pid'] = os.getpid()
if not 'thread' in kwargs:
kwargs['thread'] = threading.current_thread().name

super().__init__(*args, **kwargs)

def admin_link(self):
url = reverse('admin:huey_monitor_signalinfomodel_change', args=[self.pk])
return url
Expand Down
15 changes: 0 additions & 15 deletions huey_monitor/tasks.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import logging
import os
import socket
import sys
import threading
import traceback
import uuid
from functools import lru_cache

from django.db import transaction
from django.db.models import Sum
Expand All @@ -18,9 +14,6 @@
logger = logging.getLogger(__name__)


@lru_cache(maxsize=None)
def get_hostname():
return socket.gethostname()


def update_task_instance(instance, last_signal, task_finished):
Expand Down Expand Up @@ -58,10 +51,6 @@ def store_signals(signal, task, exc=None):
logger.info('Store Task %s signal %r (finished: %s)', task_id, signal, task_finished)

signal_kwargs = {
# TODO: move parts into huey_monitor.models.SignalInfoManager
'hostname': get_hostname(),
'pid': os.getpid(),
'thread': threading.current_thread().name,
'signal_name': signal,
}

Expand Down Expand Up @@ -111,10 +100,6 @@ def startup_handler():
for task_model_instance in qs:
logger.warning('Mark "executing" task %s to "unknown"', task_model_instance.pk)
last_signal = SignalInfoModel.objects.create(
# TODO: move parts into huey_monitor.models.SignalInfoManager
hostname=get_hostname(),
pid=os.getpid(),
thread=threading.current_thread().name,
task_id=task_model_instance.pk,
signal_name='unknown',
progress_count=task_model_instance.progress_count,
Expand Down
Loading