Commit 160c6664 by Chif Gergő

Implement basic channels feature for testing

parent 37eea7f4
Pipeline #1074 passed with stage
in 2 minutes 26 seconds
......@@ -7,6 +7,9 @@ from image.models import Disk
from interface_openstack.implementation.vm.instance import (
OSVirtualMachineManager
)
from channels.layers import get_channel_layer
from asgiref.sync import async_to_sync
import logging
logger = logging.getLogger(__name__)
......@@ -251,8 +254,11 @@ class Instance(BaseMachineDescriptor):
def update_status(self):
remote = self.get_remote_instance()
self.status = remote.status
self.save()
if self.status is not remote.status:
self.status = remote.status
channels = get_channel_layer()
async_to_sync(channels.group_send)(str(self.id), {"type": "status.status_changed"})
self.save()
@classmethod
def generate_password(self):
......
# import json
# from asgiref.sync import async_to_sync
from asgiref.sync import async_to_sync
from channels.generic.websocket import WebsocketConsumer
from recircle.instance.models import Instance
import logging
logger = logging.getLogger(__name__)
class StatusConsumer(WebsocketConsumer):
def connect(self):
pass
instances = Instance.objects.all()
for inst in instances:
async_to_sync(self.channel_layer.group_add)(
str(inst.id),
self.channel_name
)
def disconnect(self):
pass
instances = Instance.objects.all()
for inst in instances:
async_to_sync(self.channel_layer.group_discard)(
str(inst.id),
self.channel_name
)
def receive(self, text_message):
pass
def status_changed(self, event):
logger.info("Event received")
self.send(text_data=event["status"])
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment