Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dont commit on shutdown if task hasnt finished #5

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions kq/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,20 @@ def __init__(self,
auto_offset_reset='latest',
)

self._task_finished = False

def __del__(self):
"""Commit the Kafka consumer offsets and close the consumer."""
"""Commit the Kafka consumer offsets and close the consumer.
... But only when the task has finished. This is to keep
ourselves consistent with Kafka's at-least once policy.
"""
if hasattr(self, '_consumer'):
try:
self._logger.info('Committing offsets ...')
self._consumer.commit()
except Exception as e: # pragma: no cover
self._logger.warning('Failed to commit offsets: {}'.format(e))
if self._task_finished:
try:
self._logger.info('Committing offsets ...')
self._consumer.commit()
except Exception as e: # pragma: no cover
self._logger.warning('Failed to commit offsets: {}'.format(e))
try:
self._logger.info('Closing consumer ...')
self._consumer.close()
Expand Down Expand Up @@ -258,7 +264,9 @@ def start(self):
try:
for record in self._consumer:
self._consume_record(record)
self._task_finished = True
self._consumer.commit()
self._task_finished = False # For next task
except KeyboardInterrupt: # pragma: no cover
self._logger.info('Stopping {} ...'.format(self))
self._pool.terminate() # TODO not sure if necessary