MCPcopy
hub / github.com/celery/celery / Polaroid

Class Polaroid

celery/events/snapshot.py:24–83  ·  view source on GitHub ↗

Record event snapshots.

Source from the content-addressed store, hash-verified

22
23
24class Polaroid:
25 """Record event snapshots."""
26
27 timer = None
28 shutter_signal = Signal(name='shutter_signal', providing_args={'state'})
29 cleanup_signal = Signal(name='cleanup_signal')
30 clear_after = False
31
32 _tref = None
33 _ctref = None
34
35 def __init__(self, state, freq=1.0, maxrate=None,
36 cleanup_freq=3600.0, timer=None, app=None):
37 self.app = app_or_default(app)
38 self.state = state
39 self.freq = freq
40 self.cleanup_freq = cleanup_freq
41 self.timer = timer or self.timer or Timer()
42 self.logger = logger
43 self.maxrate = maxrate and TokenBucket(rate(maxrate))
44
45 def install(self):
46 self._tref = self.timer.call_repeatedly(self.freq, self.capture)
47 self._ctref = self.timer.call_repeatedly(
48 self.cleanup_freq, self.cleanup,
49 )
50
51 def on_shutter(self, state):
52 pass
53
54 def on_cleanup(self):
55 pass
56
57 def cleanup(self):
58 logger.debug('Cleanup: Running...')
59 self.cleanup_signal.send(sender=self.state)
60 self.on_cleanup()
61
62 def shutter(self):
63 if self.maxrate is None or self.maxrate.can_consume():
64 logger.debug('Shutter: %s', self.state)
65 self.shutter_signal.send(sender=self.state)
66 self.on_shutter(self.state)
67
68 def capture(self):
69 self.state.freeze_while(self.shutter, clear_after=self.clear_after)
70
71 def cancel(self):
72 if self._tref:
73 self._tref() # flush all received events.
74 self._tref.cancel()
75 if self._ctref:
76 self._ctref.cancel()
77
78 def __enter__(self):
79 self.install()
80 return self
81

Callers 5

test_constructorMethod · 0.90
test_install_timersMethod · 0.90
test_cleanupMethod · 0.90
test_shutter__captureMethod · 0.90
test_shutter_maxrateMethod · 0.90

Calls 1

SignalClass · 0.90

Tested by 5

test_constructorMethod · 0.72
test_install_timersMethod · 0.72
test_cleanupMethod · 0.72
test_shutter__captureMethod · 0.72
test_shutter_maxrateMethod · 0.72