Skip to content

Instantly share code, notes, and snippets.

@davidrichards
Created June 16, 2016 03:37
Show Gist options
  • Save davidrichards/88c3e55f148c5d27bccbcb42baaef1df to your computer and use it in GitHub Desktop.
Save davidrichards/88c3e55f148c5d27bccbcb42baaef1df to your computer and use it in GitHub Desktop.
Setting up a singleton for bit_analytics in a Rails application
require "singleton"
class Cohort
include Singleton
def self.instance
@@instance ||= new.db
end
def host_url
@host_url ||= ENV["COHORT_HOST_URL"] || "localhost"
end
def port
@port ||= ENV["COHORT_PORT"] || 6379
end
def db
@db ||= BitAnalytics
.new({ :host => host_url, :port => port })
.tap do |e|
e.send(:define_singleton_method, :state) { "Active" }
e.mark_event("system:start", Time.now.utc.to_i)
end
rescue Object => exception
@db = OpenStruct.new({ :state => "Inactive", :error => exception })
end
def reload!
@db = nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment