8000 Add shutdown routine to clean up properly · dotlambda/fluent-logger-python@c1e5b29 · GitHub
[go: up one dir, main page]

Skip to content

Commit c1e5b29

Browse files
committed
Add shutdown routine to clean up properly
1 parent 3909c43 commit c1e5b29

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

README.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ fluent, with tag 'app.follow' and the attributes 'from' and 'to'.
9393
'to': 'userB'
9494
})
9595
96+
If you want to shutdown the client, call `close()` method.
97+
98+
.. code:: python
99+
100+
sender.close()
101+
96102
Handler for buffer overflow
97103
~~~~~~~~~~~~~~~~~~~~~~~~~~~
98104

@@ -112,6 +118,8 @@ You can inject your own custom proc to handle buffer overflow in the event of co
112118
113119
You should handle any exception in handler. fluent-logger ignores exceptions from ``buffer_overflow_handler``.
114120

121+
This handler is also called when pending events exist during `close()`.
122+
115123
Python logging.Handler interface
116124
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
117125

fluent/sender.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ def setup(tag, **kwargs):
2727
def get_global_sender():
2828
return _global_sender
2929

30+
def close():
31+
get_global_sender().close()
3032

3133
class FluentSender(object):
3234
def __init__(self,
@@ -71,6 +73,20 @@ def emit_with_time(self, label, timestamp, data):
7173
"traceback": traceback.format_exc()})
7274
self._send(bytes_)
7375

76+
def close(self):
77+
self.lock.acquire()
78+
try:
79+
if self.pendings:
80+
try:
81+
self._send_data(self.pendings)
82+
except Exception:
83+
self._call_buffer_overflow_handler(self.pendings)
84+
finally:
85+
self.lock.release()
86+
87+
self._close()
88+
self.pendings = None
89+
7490
def _make_packet(self, label, timestamp, data):
7591
if label:
7692
tag = '.'.join((self.tag, label))
@@ -95,11 +111,7 @@ def _send_internal(self, bytes_):
95111
bytes_ = self.pendings
96112

97113
try:
98-
# reconnect if possible
99-
self._reconnect()
100-
101-
# send message
102-
self.socket.sendall(bytes_)
114+
self._send_data(bytes_)
103115

104116
# send finished
105117
self.pendings = None
@@ -113,6 +125,12 @@ def _send_internal(self, bytes_):
113125
else:
114126
self.pendings = bytes_
115127

128+
def _send_data(self, bytes_):
129+
# reconnect if possible
130+
self._reconnect()
131+
# send message
132+
self.socket.sendall(bytes_)
133+
116134
def _reconnect(self):
117135
if not self.socket:
118136
3816 if self.host.startswith('unix://'):

0 commit comments

Comments
 (0)
0