Skip to content

Commit

Permalink
增加Program模型
Browse files Browse the repository at this point in the history
  • Loading branch information
cone387 committed Oct 20, 2023
1 parent d6112df commit a09c18c
Show file tree
Hide file tree
Showing 5 changed files with 266 additions and 67 deletions.
3 changes: 2 additions & 1 deletion django_common_task_system/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,12 +382,13 @@ class ConsumerAdmin(admin.ModelAdmin):
'program_state',
'admin_consume_url',
'consume_status', 'program_status',
'program_source',
'action', 'create_time')
form = forms.ConsumerForm
fieldsets = (
(None, {
'fields': (
('machine', 'program_type')
('machine', 'program_type', 'program_source')
),
}),
("容器配置", {
Expand Down
19 changes: 9 additions & 10 deletions django_common_task_system/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(self, key):

class ConsumerProgram(Program):
state_class = ConsumerState
state_key = MapKey('consumers')
state_key = MapKey('consumer-programs')

def __init__(self, model: Consumer, container=None):
self.model = model
Expand All @@ -54,7 +54,7 @@ def load_from_container(cls, container: Container):
consume_kwargs=kwargs,
create_time=datetime.strptime(container.attrs['Created'].split('.')[0], "%Y-%m-%dT%H:%M:%S"),
)
consumer.program = cls(model=consumer, container=container)
# consumer.program = cls(model=consumer, container=container)
consumer.save()

@classmethod
Expand Down Expand Up @@ -90,7 +90,7 @@ def _run(self):
settings_file = '/mnt/task-system-client-settings.py'
command = f'common-task-system-client --subscription-url="{model.consume_url}" --settings="{settings_file}"'
try:
container = docker_client.containers.create(
self.container = docker_client.containers.create(
setting.image, command=command, name=setting.name,
volumes=[f"{model.settings_file}:{settings_file}"],
detach=True
Expand All @@ -106,30 +106,29 @@ def _run(self):
pass
else:
raise RuntimeError('pull image failed: %s' % setting.image)
container = docker_client.containers.create(setting.image,
self.container = docker_client.containers.create(setting.image,
command=command,
name=setting.name, detach=True)
container.start()
container = docker_client.containers.get(container.short_id)
self.container = container
self.container.start()
self.container = docker_client.containers.get(self.container.short_id)

def run(self):
model = self.model
model.program = self
# model.program = self
try:
self._run()
model.consume_status = ConsumeStatus.RUNNING
except Exception as _:
model.consume_status = ConsumeStatus.FAILED
model.startup_log = traceback.format_exc()
model.save()
model.save(commit=False)

def stop(self, destroy=False):
if isinstance(self.container, Container):
self.container.stop()
# self.container.remove()
self.model.consume_status = ConsumeStatus.STOPPED
self.model.save()
self.model.save(commit=False)

def read_log(self, page=0, page_size=1000):
if self.model.consume_status == ConsumeStatus.RUNNING.value:
Expand Down
7 changes: 5 additions & 2 deletions django_common_task_system/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ def __init__(self, attrs=None):


class ConsumerForm(forms.ModelForm):
program_source = forms.CharField(initial=ProgramSource.ADMIN.value, widget=forms.HiddenInput())
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 @@ -397,11 +398,13 @@ def __init__(self, *args, **kwargs):
path = reverse('schedule-get', kwargs={'code': obj.code})
consume_url_choices.append((path, obj.name))
self.fields['consume_url'].choices = consume_url_choices
ip_choices = [(models.Machine.localhost_ip, '127.0.0.1(本机)')]
local = models.Machine.objects.local
ip_choices = [(local.localhost_ip, '%s(%s)' % (local.localhost_ip, local.hostname))]
for machine in models.Machine.objects.all():
ip_choices.append((machine.intranet_ip, "%s(%s)内网" % (machine.intranet_ip, machine.hostname)))
ip_choices.append((machine.internet_ip, "%s(%s)外网" % (machine.internet_ip, machine.hostname)))
self.fields['consume_host'].choices = ip_choices
self.initial['machine'] = models.Machine.objects.local
self.initial['consume_port'] = os.environ['DJANGO_SERVER_ADDRESS'].split(':')[-1]

@staticmethod
Expand Down Expand Up @@ -433,7 +436,7 @@ def clean(self):
raise forms.ValidationError('command is required for mysql consume')
consumer.program_type = cleaned_data['program_type']
consumer.machine = cleaned_data['machine']
consumer.program_source = ProgramSource.ADMIN
consumer.program_source = cleaned_data['program_source']
if consumer.program_type == ProgramType.DOCKER:
consumer.program_setting = {
'image': cleaned_data['container_image'],
Expand Down
Loading

0 comments on commit a09c18c

Please sign in to comment.