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

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

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

"""LVAlert client.""" 

import json 

 

from celery.utils.log import get_task_logger 

 

from ..lvalert.signals import lvalert_received 

from .. import app 

from .core import DispatchHandler 

from . import gracedb 

 

log = get_task_logger(__name__) 

 

 

class _LVAlertDispatchHandler(DispatchHandler): 

 

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

try: 

lvalert_nodes = app.conf['lvalert_nodes'] 

except KeyError: 

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

lvalert_nodes.update(keys) 

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

 

def process_args(self, node, payload): 

# Determine GraceDB service URL 

alert = json.loads(payload) 

try: 

try: 

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

except KeyError: 

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

except KeyError: 

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

alert) 

return None, None, None 

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

service = base + api 

 

if service != gracedb.client._service_url: 

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

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

service, gracedb.client._service_url) 

return None, None, None 

 

return super().process_args(node, alert) 

 

 

handler = _LVAlertDispatchHandler() 

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

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

be automatically called whenever a matching LVAlert message is received. 

 

Parameters 

---------- 

\*keys 

List of LVAlert message types to accept 

\*\*kwargs 

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

 

Examples 

-------- 

Declare a new handler like this:: 

 

@lvalert.handler('cbc_gstlal', 

'cbc_spiir', 

'cbc_pycbc', 

'cbc_mbtaonline') 

def handle_cbc(alert_content): 

# do work here... 

""" 

 

 

@lvalert_received.connect 

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

handler.dispatch(node, payload)