Skip to content

Commit

Permalink
Reconnect to MongoDB if multiple failures
Browse files Browse the repository at this point in the history
  • Loading branch information
berlic authored Aug 28, 2017
1 parent f013fd0 commit 4b74a44
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/logstash/outputs/mongodb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class LogStash::Outputs::Mongodb < LogStash::Outputs::Base
# "_id" field in the event.
config :generateId, :validate => :boolean, :default => false

# Number of insert attempts before reconnect to MongoDB
config :retries_before_reconnect, :validate => :number, :default => 3, :required => false


# Bulk insert flag, set to true to allow bulk insertion, else it will insert events one by one.
config :bulk, :validate => :boolean, :default => false
Expand Down Expand Up @@ -72,6 +75,7 @@ def register
end # def register

def receive(event)
retries = 0
begin
# Our timestamp object now has a to_bson method, using it here
# {}.merge(other) so we don't taint the event hash innards
Expand Down Expand Up @@ -111,6 +115,11 @@ def receive(event)
# to fix the issue.
else
sleep @retry_delay
retries += 1
if @retries_before_reconnect == retries
register
retries = 0
end
retry
end
end
Expand Down

0 comments on commit 4b74a44

Please sign in to comment.