1 | #!/usr/bin/env python |
||
2 | # coding: utf-8 |
||
3 | from __future__ import unicode_literals |
||
4 | from multiprocessing import Pool, TimeoutError |
||
5 | # django |
||
6 | from django.core.management.base import BaseCommand |
||
7 | from django.conf import settings |
||
8 | from logging import getLogger |
||
9 | from django.db.models import Q |
||
10 | # trigger happy |
||
11 | from django_th.models import TriggerService |
||
12 | from django_th.read import Read |
||
13 | |||
14 | # create logger |
||
15 | logger = getLogger('django_th.trigger_happy') |
||
16 | |||
17 | |||
18 | View Code Duplication | class Command(BaseCommand): |
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
19 | |||
20 | help = 'Trigger all the services '\ |
||
21 | 'and put them in cache' |
||
22 | |||
23 | def handle(self, *args, **options): |
||
24 | """ |
||
25 | get all the triggers that need to be handled |
||
26 | """ |
||
27 | from django.db import connection |
||
28 | connection.close() |
||
29 | failed_tries = settings.DJANGO_TH.get('failed_tries', 10) |
||
30 | trigger = TriggerService.objects.filter( |
||
31 | Q(provider_failed__lte=failed_tries) | |
||
32 | Q(consumer_failed__lte=failed_tries), |
||
33 | status=True, |
||
34 | user__is_active=True, |
||
35 | provider__name__status=True, |
||
36 | consumer__name__status=True, |
||
37 | ).select_related('consumer__name', 'provider__name') |
||
38 | try: |
||
39 | with Pool(processes=settings.DJANGO_TH.get('processes')) as pool: |
||
40 | r = Read() |
||
41 | result = pool.map_async(r.reading, trigger) |
||
42 | result.get(timeout=60) |
||
43 | except TimeoutError as e: |
||
44 | logger.warning(e) |
||
45 |