File: consumers.py

package info (click to toggle)
celery-progress 0.1.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 156 kB
  • sloc: python: 272; javascript: 168; makefile: 6
file content (41 lines) | stat: -rw-r--r-- 1,173 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from channels.generic.websocket import AsyncWebsocketConsumer
import json

from celery.result import AsyncResult
from celery_progress.backend import Progress


class ProgressConsumer(AsyncWebsocketConsumer):
    async def connect(self):
        self.task_id = self.scope['url_route']['kwargs']['task_id']

        await self.channel_layer.group_add(
            self.task_id,
            self.channel_name
        )

        await self.accept()

    async def disconnect(self, close_code):
        await self.channel_layer.group_discard(
            self.task_id,
            self.channel_name
        )

    async def receive(self, text_data):
        text_data_json = json.loads(text_data)
        task_type = text_data_json['type']

        if task_type == 'check_task_completion':
            await self.channel_layer.group_send(
                self.task_id,
                {
                    'type': 'update_task_progress',
                    'data': Progress(AsyncResult(self.task_id)).get_info()
                }
            )

    async def update_task_progress(self, event):
        data = event['data']

        await self.send(text_data=json.dumps(data))