Skip to content

Commit

Permalink
将consumer中machine相关信息独立为model
Browse files Browse the repository at this point in the history
  • Loading branch information
18895379450 committed Oct 17, 2023
1 parent 5b19e1c commit a6fe53c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 31 deletions.
19 changes: 10 additions & 9 deletions django_common_task_system/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,8 @@ def get_readonly_fields(self, request, obj=None):


class ConsumerAdmin(admin.ModelAdmin):
list_display = ('consumer_id', 'group', 'machine', 'program_state',
list_display = ('consumer_id', 'machine',
'program_state',
'admin_consume_url',
'consume_status', 'program_status',
'action', 'create_time')
Expand Down Expand Up @@ -779,18 +780,18 @@ def get_queryset(self, request):
},
position=4
)
groups = set()
machines = set()
for client in models.Consumer.objects.all():
client: models.Consumer
machines.add(client.machine_ip)
groups.add(client.group)
# groups = set()
# machines = models.Machine.objects.all().count()
# for client in models.Consumer.objects.all():
# client: models.Consumer
# machines.add(client.machine_ip)
# groups.add(client.group)

model.objects['client'] = model(
name="客户端数量",
state={
"机器数量": len(machines),
"分组数量": len(groups),
"机器数量": models.Machine.objects.all().count(),
# "分组数量": len(groups),
"客户端数量": models.Consumer.objects.count()
},
position=5
Expand Down
5 changes: 3 additions & 2 deletions django_common_task_system/consumer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Optional
from django_common_task_system.choices import ConsumeStatus, ContainerStatus
from django_common_task_system.models import Consumer
from django_common_task_system.models import Consumer, Machine
from docker.errors import APIError
from threading import Thread
from docker.models.containers import Container
Expand Down Expand Up @@ -63,7 +63,8 @@ def load_from_container(cls, container: Container):
@classmethod
def load_consumer_from_cache(cls, cache: dict):
program = cache.pop('program')
consumer = Consumer(**cache)
machine = Machine(**cache.pop('machine'))
consumer = Consumer(machine=machine, **cache)
if program:
container_cache = program.get('container', {})
if container_cache:
Expand Down
6 changes: 3 additions & 3 deletions django_common_task_system/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,6 @@ def __init__(self, attrs=None):


class ConsumerForm(forms.ModelForm):
machine = forms.ChoiceField(label='机器')
consume_url = forms.ChoiceField(label='订阅地址', required=False)
consume_scheme = forms.ChoiceField(label='订阅Scheme', choices={x: x for x in ['http', 'https']}.items())
consume_host = forms.ChoiceField(label='订阅Host')
Expand Down Expand Up @@ -407,7 +406,7 @@ def __init__(self, *args, **kwargs):
('127.0.0.1', '127.0.0.1')
)
self.fields['consume_host'].choices = ip_choices
self.fields['machine'].choices = self.get_machine_choices()
# self.fields['machine'].choices = self.get_machine_choices()
self.initial['consume_port'] = os.environ['DJANGO_SERVER_ADDRESS'].split(':')[-1]

@staticmethod
Expand Down Expand Up @@ -451,7 +450,7 @@ def clean(self):
if self.errors:
return cleaned_data
consumer: models.Consumer = self.instance
consumer.machine_ip, consumer.machine_name = cleaned_data['machine'].split('-', 1)
# consumer.machine_ip, consumer.machine_name = cleaned_data['machine'].split('-', 1)
consumer.consume_url = cleaned_data.get('custom_consume_url') or urljoin(
"%s://%s:%s" % (cleaned_data.get('consume_scheme'),
cleaned_data.get('consume_host'),
Expand All @@ -464,6 +463,7 @@ def clean(self):
if not consumer.consume_kwargs.get('command'):
raise forms.ValidationError('command is required for mysql consume')
consumer.program_type = cleaned_data['program_type']
consumer.machine = cleaned_data['machine']
if consumer.program_type == ProgramType.DOCKER:
consumer.program_setting = {
'image': cleaned_data['container_image'],
Expand Down
56 changes: 39 additions & 17 deletions django_common_task_system/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,17 @@ class Query:
order_by = []
select_related = False

def using(self, alias):
return self

def all(self):
return self

def __init__(self, seq, model):
super().__init__(seq)
self.query = self.Query
self.model = model
self._prefetch_related_lookups = True
self.verbose_name = model._meta.verbose_name
self.verbose_name_plural = model._meta.verbose_name_plural

Expand Down Expand Up @@ -386,29 +393,37 @@ def delete(self, consumer: 'Consumer'):
cache_agent.hdel(self.cache_key, consumer.consumer_id)


# class Machine(models.Model):
# name = models.CharField(max_length=100, verbose_name='机器名', primary_key=True)
# ip = models.GenericIPAddressField(max_length=100, verbose_name='机器IP', default='127.0.0.1')
# group = models.CharField(max_length=100, verbose_name='分组', default='默认')
#
# manager = CustomManager()
#
# class Meta:
# managed = False
# verbose_name = verbose_name_plural = '机器管理'
#
# def __str__(self):
# return self.name
class MachineManager(CustomManager):

def all(self):
return QuerySet([
Machine(name='本机', ip='127.0.0.1', group='默认'),
], Machine)


class Machine(models.Model):
name = models.CharField(max_length=100, verbose_name='机器名', primary_key=True)
ip = models.GenericIPAddressField(max_length=100, verbose_name='机器IP', default='127.0.0.1')
group = models.CharField(max_length=100, verbose_name='分组', default='默认')

objects = MachineManager()

class Meta:
managed = False
verbose_name = verbose_name_plural = '机器管理'

def __str__(self):
return "%s(%s)" % (self.name, self.ip)


class Consumer(models.Model):
# 两种运行模式: 容器模式,进程模式
program: ContainerProgram = None

machine = models.ForeignKey(Machine, on_delete=models.DO_NOTHING, db_constraint=False, verbose_name='机器')
consumer_id = models.IntegerField(verbose_name='客户端ID', primary_key=True)
machine_name = models.CharField(max_length=100, verbose_name='机器名', default='本机')
machine_ip = models.GenericIPAddressField(max_length=100, verbose_name='机器IP', default='127.0.0.1')
group = models.CharField(max_length=100, verbose_name='分组', default='默认')
# machine_name = models.CharField(max_length=100, verbose_name='机器名', default='本机')
# machine_ip = models.GenericIPAddressField(max_length=100, verbose_name='机器IP', default='127.0.0.1')
# group = models.CharField(max_length=100, verbose_name='分组', default='默认')
consume_url = models.CharField(max_length=200, verbose_name='订阅地址')
consume_kwargs = models.JSONField(verbose_name='订阅参数', default=dict)
program_type = models.CharField(max_length=100, verbose_name='运行引擎', choices=ProgramType.choices,
Expand Down Expand Up @@ -611,8 +626,15 @@ class Meta:
ordering = ('position',)


class MachineSerializer(serializers.ModelSerializer):
class Meta:
fields = '__all__'
model = Machine


class ConsumerSerializer(serializers.ModelSerializer):
program = serializers.SerializerMethodField()
machine = MachineSerializer()

@staticmethod
def get_program(obj: Consumer):
Expand Down

0 comments on commit a6fe53c

Please sign in to comment.