Skip to content

Auto commit timer is not periodic #23

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

Merged
merged 1 commit into from
May 28, 2013
Merged
Show file tree
Hide file tree
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
25 changes: 18 additions & 7 deletions kafka/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,20 @@ def __init__(self, client, group, topic, auto_commit=False, auto_commit_every_n=
self.client._load_metadata_for_topics(topic)
self.offsets = {}

# Set up the auto-commit timer
if auto_commit is True:
if auto_commit_every_t is not None:
self.commit_timer = ReentrantTimer(auto_commit_every_t, self.commit)
self.commit_timer.start()

# Variables for handling offset commits
self.commit_lock = Lock()
self.commit_timer = None
self.count_since_commit = 0
self.auto_commit = auto_commit
self.auto_commit_every_n = auto_commit_every_n
self.auto_commit_every_t = auto_commit_every_t


# Set up the auto-commit timer
if auto_commit is True and auto_commit_every_t is not None:
self.commit_timer = ReentrantTimer(auto_commit_every_t,
self._timed_commit)
self.commit_timer.start()

def get_or_init_offset_callback(resp):
if resp.error == ErrorMapping.NO_ERROR:
return resp.offset
Expand Down Expand Up @@ -101,6 +103,15 @@ def seek(self, offset, whence):
else:
raise ValueError("Unexpected value for `whence`, %d" % whence)

def _timed_commit(self):
"""
Commit offsets as part of timer
"""
self.commit()

# Once the commit is done, start the timer again
self.commit_timer.start()

def commit(self, partitions=[]):
"""
Commit offsets for this consumer
Expand Down
11 changes: 5 additions & 6 deletions kafka/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,12 @@ def __init__(self, t, fn):
self.fn = fn

def start(self):
if self.timer is None:
self.timer = Timer(self.t / 1000., self.fn)
self.timer.start()
else:
if self.timer is not None:
self.timer.cancel()
self.timer = Timer(self.t / 1000., self.fn)
self.timer.start()

self.timer = Timer(self.t / 1000., self.fn)
self.timer.start()

def stop(self):
self.timer.cancel()
self.timer = None