Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1"""LVAlert client.""" 

2import json 

3 

4from celery.utils.log import get_task_logger 

5 

6from ..lvalert.signals import lvalert_received 

7from .. import app 

8from .core import DispatchHandler 

9from . import gracedb 

10 

11log = get_task_logger(__name__) 

12 

13 

14class _LVAlertDispatchHandler(DispatchHandler): 

15 

16 def __call__(self, *keys, **kwargs): 

17 try: 

18 lvalert_nodes = app.conf['lvalert_nodes'] 

19 except KeyError: 

20 lvalert_nodes = app.conf['lvalert_nodes'] = set() 

21 lvalert_nodes.update(keys) 

22 return super().__call__(*keys, **kwargs) 

23 

24 def process_args(self, node, payload): 

25 # Determine GraceDB service URL 

26 alert = json.loads(payload) 

27 try: 

28 try: 

29 self_link = alert['object']['links']['self'] 

30 except KeyError: 

31 self_link = alert['object']['self'] 

32 except KeyError: 

33 log.exception('LVAlert message does not contain an API URL: %r', 

34 alert) 

35 return None, None, None 

36 base, api, _ = self_link.partition('/api/') 

37 service = base + api 

38 

39 if service != gracedb.client.url: 

40 log.warning('ignoring LVAlert message because it is intended for ' 

41 'GraceDB server %s, but we are set up for server %s', 

42 service, gracedb.client.url) 

43 return None, None, None 

44 

45 return super().process_args(node, alert) 

46 

47 

48handler = _LVAlertDispatchHandler() 

49r"""Function decorator to register a handler callback for specified LVAlert 

50message types. The decorated function is turned into a Celery task, which will 

51be automatically called whenever a matching LVAlert message is received. 

52 

53Parameters 

54---------- 

55\*keys 

56 List of LVAlert message types to accept 

57\*\*kwargs 

58 Additional keyword arguments for :meth:`celery.Celery.task`. 

59 

60Examples 

61-------- 

62Declare a new handler like this:: 

63 

64 @lvalert.handler('cbc_gstlal', 

65 'cbc_spiir', 

66 'cbc_pycbc', 

67 'cbc_mbtaonline') 

68 def handle_cbc(alert_content): 

69 # do work here... 

70""" 

71 

72 

73@lvalert_received.connect 

74def _on_lvalert_received(node, payload, **kwargs): 

75 handler.dispatch(node, payload)