Skip to content

Commit

Permalink
Remove Faye + use simple pooling
Browse files Browse the repository at this point in the history
  • Loading branch information
andersoncardoso committed Sep 11, 2016
1 parent ef227e9 commit 89c0f63
Show file tree
Hide file tree
Showing 20 changed files with 21 additions and 118 deletions.
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ gem 'rgeo-geojson' # geojson encode/decode
gem 'activerecord-postgis-adapter' # postgis
gem 'pg_search' # postgres fulltext search
gem 'thin' # server to run faye
gem 'faye' # realtime pub/sub

gem 'coffee-rails', '~> 4.0.1' # coffeescript
gem 'uglifier', '>= 1.3.0' # minify
Expand Down Expand Up @@ -72,7 +71,7 @@ group :development, :test do
gem 'rspec-rails', '~> 2.0' # BDD
gem 'pry' # better shell sessions and debug tool
gem 'pry-rails' # use pry as rails console
gem 'konacha' # js tests with mocha + chai
# gem 'konacha' # js tests with mocha + chai
gem 'guard-rspec', require: false # launch specs when files are modified
gem 'i18n-tasks' # check for translations
end
Expand All @@ -85,6 +84,7 @@ group :test do
gem 'database_cleaner' # improved database cleaning for tests
gem 'simplecov', '~> 0.9.0', require: false # coverage report
gem 'coveralls', require: false # use coveralls with travisCI
gem 'konacha' # js tests with mocha + chai
end

group :production, :staging do
Expand Down
22 changes: 0 additions & 22 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ GEM
compass-rails (2.0.0)
compass (>= 0.12.2)
connection_pool (2.0.0)
cookiejar (0.3.2)
coveralls (0.7.1)
multi_json (~> 1.3)
rest-client
Expand All @@ -112,14 +111,6 @@ GEM
thread
thread_safe
ejs (1.1.1)
em-http-request (1.1.2)
addressable (>= 2.3.4)
cookiejar
em-socksify (>= 0.3)
eventmachine (>= 1.0.3)
http_parser.rb (>= 0.6.0)
em-socksify (0.3.0)
eventmachine (>= 1.0.0.beta.4)
erubis (2.7.0)
event_bus (1.0.0)
eventmachine (1.0.3)
Expand All @@ -131,17 +122,6 @@ GEM
railties (>= 3.0.0)
faraday (0.9.0)
multipart-post (>= 1.2, < 3)
faye (1.0.3)
cookiejar (>= 0.3.0)
em-http-request (>= 0.3.0)
eventmachine (>= 0.12.0)
faye-websocket (>= 0.7.0)
multi_json (>= 1.0.0)
rack (>= 1.0.0)
websocket-driver (>= 0.3.0)
faye-websocket (0.7.5)
eventmachine (>= 0.12.0)
websocket-driver (>= 0.3.5)
ffi (1.9.6)
figaro (1.0.0)
thor (~> 0.14)
Expand All @@ -165,7 +145,6 @@ GEM
hike (1.2.3)
hitimes (1.2.2)
http_accept_language (2.0.2)
http_parser.rb (0.6.0)
i18n (0.6.11)
i18n-tasks (0.7.7)
activesupport
Expand Down Expand Up @@ -422,7 +401,6 @@ DEPENDENCIES
ejs
event_bus
factory_girl_rails
faye
figaro
font-awesome-sass
foreman
Expand Down
1 change: 0 additions & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
web: bundle exec puma -C config/puma.rb
faye: rackup faye.ru -s thin -E production
worker: bundle exec sidekiq -C config/sidekiq.yml
1 change: 0 additions & 1 deletion Procfile.dev
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
web: bundle exec rails s
faye: rackup faye.ru -s thin -E production
worker: bundle exec sidekiq -C config/sidekiq.yml
5 changes: 0 additions & 5 deletions app/assets/javascripts/application.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,11 @@
window.__testing__ ?= {}

onReady = ->
try
App.faye = new Faye.Client(fayeUrl)
catch e
console?.error e
App.mediator.publish('components:start')
$('.central-pane, .side-pane').stick_in_parent()

$(document).ready onReady


# add private properties to be tested
window.__testing__?.application =
onReady: onReady
2 changes: 1 addition & 1 deletion app/assets/javascripts/base.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ componentChanged = (evt, component) ->
mediator.subscribe 'component:changed', componentChanged

log = () ->

getDate = (date) ->
months = ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December']
Expand Down
11 changes: 8 additions & 3 deletions app/assets/javascripts/components/notifications.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ App.components.notifications = ->
initialize: ->
@toggleCounter()
App.mediator.subscribe "modal:afterOpen", @onModalOpen.bind(this)
App.faye.subscribe "/notifications/#{@attr.userId}", @onNotified.bind(this)
if @attr.userId
setInterval @checkNotifications.bind(this), 30000

onModalOpen: (evt, data) ->
if data.identifier is @attr.modalId
@markAsRead()

onNotified: (data) ->
@setCounter data.count
checkNotifications: () ->
$.ajax
type: 'GET'
url: "/notifications/count",
dataType: "json"
success: (data) => @setCounter(data.count)

markAsRead: ->
ids = @unreadIds()
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/notifications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ def read
render json: {ok: true}
end

def count
count = Notification.unread.where(user_id: current_user.id).count
render json: {count: count}
end
end
7 changes: 2 additions & 5 deletions app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ class Notification < ActiveRecord::Base

validates :user, :activity, presence: true

scope :unread, ->{ where(status: "unread") }

def self.build_notifications(activity)
_id = activity.is_a?(PublicActivity::Activity) ? activity.id : activity
NotificationWorker.perform_async(_id)
end

def rt_notify
count = Notification.where(user_id: user_id, status: "unread").count
FayeClient::publish "/notifications/#{user_id}", {count: count}
end
end
5 changes: 1 addition & 4 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@
window.I18n.pluralize = function(key, count) {
return count === 1 ? key.one : key.other.replace('%{count}', count)
};

window.fayeUrl = "<%= ENV['FAYE_URL'] %>";
</script>

<%= javascript_include_tag "application", ENV['FAYE_JS'] %>
<%= javascript_include_tag "application" %>
</body>
</html>
5 changes: 1 addition & 4 deletions app/views/layouts/landingpage.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@
window.I18n.pluralize = function(key, count) {
return count === 1 ? key.one : key.other.replace('%{count}', count)
};

window.fayeUrl = "<%= ENV['FAYE_URL'] %>";
</script>

<%= javascript_include_tag "application", ENV['FAYE_JS'] %>
<%= javascript_include_tag "application" %>
</body>
</html>
1 change: 0 additions & 1 deletion app/workers/notification_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ def perform(activity_id)
users_to_receive_notification(activity).each do |user_id|
if user_id != activity.owner_id
notification = Notification.create activity: activity, user_id: user_id
notification.rt_notify
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion bootstrap.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ then
# Install dependencies
echo "-- SETUP DEPS"
sudo apt-get install -y curl vim build-essential libtool checkinstall python-pip python-dev gcc make sqlite3 libcurl4-openssl-dev ruby-dev libssl-dev openssl libreadline-dev git
sudo apt-get install -y redis-server libpq-dev nodejs libxslt1.1 imagemagick libjson0-dev libxml2-dev libproj-dev python2.7-dev swig libgeos-3.4.2 libgeos-dev gdal-bin libgdal1-1.10.1-grass libgdal1-dev
sudo apt-get install -y redis-server libpq-dev nodejs phantomjs libxslt1.1 imagemagick libjson0-dev libxml2-dev libproj-dev python2.7-dev swig libgeos-3.4.2 libgeos-dev gdal-bin libgdal1-1.10.1-grass libgdal1-dev
sudo apt-get install --reinstall -y language-pack-en language-pack-pt
git config --global color.ui true

Expand Down
9 changes: 0 additions & 9 deletions config/application.yml.sample
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
SECRET_TOKEN: "b82bbcb5a636bb565472f407d98a339b2d08d3c60cafdc7cc56f5fc625650d5c8f13e90fd84eb89c2ddf8ebddc031db0c098a8766869000cc56b119874462dcf"
FIXED_SALT: "long and complicated salt goes here"
FAYE_TOKEN: "token for faye authentication"

development:
PORT: "3000"
Expand All @@ -9,8 +8,6 @@ development:
FACEBOOK_SECRET: ""
GOOGLE_KEY: ""
GOOGLE_SERCRET: ""
FAYE_URL: "http://localhost:9292/faye"
FAYE_JS: "http://localhost:9292/faye.js"

test:
FIXED_SALT: "test-salt"
Expand All @@ -19,8 +16,6 @@ test:
FACEBOOK_SECRET: ""
GOOGLE_KEY: ""
GOOGLE_SECRET: ""
FAYE_URL: "http://localhost:9292/faye"
FAYE_JS: "http://localhost:9292/faye.js"

staging:
SECRET_TOKEN: ""
Expand All @@ -32,8 +27,6 @@ staging:
MAILGUN_API_KEY: ""
MAILGUN_DOMAIN: ""
MEPPIT_HOST: ""
FAYE_URL: ""
FAYE_JS: ""

production:
SECRET_TOKEN: ""
Expand All @@ -45,5 +38,3 @@ production:
MAILGUN_API_KEY: ""
MAILGUN_DOMAIN: ""
MEPPIT_HOST: ""
FAYE_URL: ""
FAYE_JS: ""
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

get "notifications" => "notifications#notifications", as: :notifications
post "notifications/read" => "notifications#read", as: :read_notifications
get "notifications/count" => "notifications#count", as: :count_notifications

concern :contributable do
get "contributors" => "contributings#contributors"
Expand Down
19 changes: 0 additions & 19 deletions faye.ru

This file was deleted.

14 changes: 0 additions & 14 deletions lib/faye_client.rb

This file was deleted.

9 changes: 0 additions & 9 deletions spec/javascripts/spec_helper.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,4 @@ chai.config.includeStack = true; # Show stack trace on failing assertion.

window._base = __testing__.base
window.I18n = {}
# Create a fake Faye Client
window.fayeUrl = ''
window.Faye = {
Client: () ->
{
subscribe: ->
publish: ->
}
}
window.DEBUG = false
9 changes: 0 additions & 9 deletions spec/models/notification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,4 @@
Notification.build_notifications activity
end
end

describe "#rt_notify" do
let!(:notification) { Notification.create user: user, activity: activity }

it "publishes message to FayeClient" do
expect(FayeClient).to receive(:publish).with "/notifications/#{user.id}", {count: 1}
notification.rt_notify
end
end
end
8 changes: 0 additions & 8 deletions spec/workers/notification_worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,5 @@
worker.perform activity.id
expect(Notification.count).to eq 1
end

it "calls rt_notify on the created notification" do
notification = Notification.create activity: activity, user: user
allow(Notification).to receive(:create).and_return notification
expect(notification).to receive(:rt_notify)
worker.perform activity.id
end
end

end

0 comments on commit 89c0f63

Please sign in to comment.