This commit is contained in:
Administrator
2022-05-08 19:59:06 +00:00
parent 210d87efdf
commit f81029b211
7 changed files with 194 additions and 18 deletions

View File

@@ -1,27 +1,37 @@
from subprocess import call
from django.db.models import Q
from Main.models import Solution
from SprintLib.redis import lock, get_redis
from SprintLib.utils import LoopWorker
class Command(LoopWorker):
help = "starts docker cleaner"
@lock("docker")
def go(self):
for solution in Solution.objects.filter(~Q(result="Testing") | ~Q(result="In queue"), docker_instances__isnull=False):
for instance in sorted(solution.docker_instances, key=lambda x: x['type']):
if instance['type'] == 'network':
call(f"docker network rm {instance['name']}", shell=True)
elif instance['type'] == 'image':
call(f"docker image rm --force {instance['name']}", shell=True)
elif instance['type'] == 'container':
call(f"docker rm --force {instance['name']}", shell=True)
else:
raise ValueError(f"Unknown docker type {instance['type']}")
solution.docker_instances = None
solution.save()
containers, images, networks = list(), list(), list()
with get_redis() as r:
while r.llen("containers") != 0:
value = r.rpop("containers").decode("utf-8")
return_code = call(f"docker rm --force {value}", shell=True)
if return_code != 0:
containers.append(value)
while r.llen("images") != 0:
value = r.rpop("images").decode("utf-8")
return_code = call(f"docker image rm --force {value}", shell=True)
if return_code != 0:
images.append(value)
while r.llen("networks") != 0:
value = r.rpop("networks").decode("utf-8")
return_code = call(f"docker network rm {value}", shell=True)
if return_code != 0:
networks.append(value)
if containers:
r.lpush("containers", *containers)
if images:
r.lpush("images", *images)
if networks:
r.lpush("networks", *networks)
def handle(self, *args, **options):
call('docker image rm $(docker images -q mathwave/sprint-repo)', shell=True)

View File

@@ -0,0 +1,29 @@
from SprintLib.queue import MessagingSupport
from SprintLib.redis import lock, get_redis
class Command(MessagingSupport):
help = "Starts redis worker"
queue_name = "redis"
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.handlers = {
"docker": self.docker_handler
}
@lock("docker")
def docker_handler(self, payload):
key = payload["key"]
value = payload["value"]
with get_redis() as r:
r.lpush(key, value)
def process(self, payload: dict):
action = payload.get("action")
if action is None:
raise ValueError("No action field in message")
handler = self.handlers.get(action)
if handler is None:
raise ValueError(f"No handler for action: {action}")
handler(payload)