Created
June 16, 2016 03:37
-
-
Save davidrichards/88c3e55f148c5d27bccbcb42baaef1df to your computer and use it in GitHub Desktop.
Setting up a singleton for bit_analytics in a Rails application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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