Skip to content

Commit

Permalink
adds a worker to pump messages into segmentio
Browse files Browse the repository at this point in the history
  • Loading branch information
rauhryan committed Aug 5, 2014
1 parent f9cebe8 commit 4bbbd41
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Gemfile
Expand Up @@ -35,9 +35,14 @@ gem 'sprockets'
gem 'sass', '~> 3.3.9'
gem 'sprockets-helpers'
gem 'execjs', "~> 1.4"
gem 'sucker_punch', '~> 1.0'
gem 'eventmachine'

gem 'sucker_punch', '~> 1.0'
gem 'redis'
gem 'celluloid'
gem 'celluloid-redis'
gem 'analytics-ruby', '~> 2.0.0'

gem 'wkhtmltopdf-heroku', '~> 1.0'
gem 'pdfkit'

Expand Down
12 changes: 12 additions & 0 deletions Gemfile.lock
Expand Up @@ -27,13 +27,20 @@ GEM
thread_safe (~> 0.1)
tzinfo (~> 1.1)
addressable (2.3.5)
analytics-ruby (2.0.5)
better_errors (1.1.0)
coderay (>= 1.0.0)
erubis (>= 2.6.6)
binding_of_caller (0.7.2)
debug_inspector (>= 0.0.1)
celluloid (0.15.2)
timers (~> 1.1.0)
celluloid-io (0.15.0)
celluloid (>= 0.15.0)
nio4r (>= 0.5.0)
celluloid-redis (0.0.2)
celluloid-io (>= 0.13.0.pre)
redis
closure-compiler (1.1.10)
coderay (1.0.9)
coffee-script (2.3.0)
Expand Down Expand Up @@ -96,6 +103,7 @@ GEM
multipart-post (1.2.0)
netrc (0.7.7)
newrelic_rpm (3.9.0.229)
nio4r (1.0.0)
octokit (2.7.0)
sawyer (~> 0.5.2)
pdfkit (0.6.2)
Expand Down Expand Up @@ -187,7 +195,10 @@ PLATFORMS
DEPENDENCIES
activesupport (~> 4.1.4)
addressable
analytics-ruby (~> 2.0.0)
better_errors
celluloid
celluloid-redis
closure-compiler
coffee-script
connection_pool
Expand Down Expand Up @@ -218,6 +229,7 @@ DEPENDENCIES
rake
raygun4ruby
rdiscount
redis
sass (~> 3.3.9)
shotgun
sinatra (~> 1.4.0)
Expand Down
53 changes: 53 additions & 0 deletions lib/workers/worker.rb
@@ -0,0 +1,53 @@
require 'dotenv'
Dotenv.load

$: << File.expand_path('../', __FILE__)

require 'celluloid'
require 'redis'
require 'celluloid/redis'
require 'multi_json'
require 'hashie'


trap(:INT) {puts "Shutting down"; $redis.unsubscribe; exit }

$redis = Redis.connect driver: :celluloid
require 'segment/analytics'

Analytics = Segment::Analytics.new({
write_key: ENV["SEGMENTIO_KEY"],
on_error: Proc.new { |status, msg| print msg }
})

begin
puts ENV['REDIS_URL']

$redis.psubscribe 'pubsub.*' do |on|
on.psubscribe do

end
on.pmessage do |match, channel, msg|
message = MultiJson.load msg
hashie = Hashie::Mash.new message
Analytics.identify(
user_id: hashie.meta.user.id,
traits: hashie.meta.user.to_hash
)
Analytics.track(
user_id: hashie.meta.user.id,
event: hashie.meta.action,
properties: hashie.to_hash
)
Analytics.logger.info "Tracked: #{hashie.meta.action}"

end
end

rescue Redis::BaseConnectionError => error
puts "#{error}, retrying in 1s"
sleep 1
retry
end


0 comments on commit 4bbbd41

Please sign in to comment.