commit: d13d169922c7efeb6e6c20ef6a271eeda552275f
parent: 29ffe1cad3f473c7b6c0b651d065f8ed9373d37e
Author: Eugen Rochko <eugen@zeonfederated.com>
Date: Wed, 5 Apr 2017 19:46:28 +0200
Merge branch 'krainboltgreene-broadcast-to-worker'
Diffstat:
6 files changed, 38 insertions(+), 19 deletions(-)
diff --git a/app/lib/feed_manager.rb b/app/lib/feed_manager.rb
@@ -34,12 +34,7 @@ class FeedManager
trim(timeline_type, account.id)
end
- broadcast(account.id, event: 'update', payload: inline_render(account, 'api/v1/statuses/show', status))
- end
-
- def broadcast(timeline_id, options = {})
- options[:queued_at] = (Time.now.to_f * 1000.0).to_i
- redis.publish("timeline:#{timeline_id}", Oj.dump(options))
+ PushUpdateWorker.perform_async(account.id, status.id)
end
def trim(type, account_id)
@@ -81,10 +76,6 @@ class FeedManager
end
end
- def inline_render(target_account, template, object)
- Rabl::Renderer.new(template, object, view_path: 'app/views', format: :json, scope: InlineRablScope.new(target_account)).render
- end
-
private
def redis
diff --git a/app/lib/inline_renderer.rb b/app/lib/inline_renderer.rb
@@ -0,0 +1,13 @@
+# frozen_string_literal: true
+
+class InlineRenderer
+ def self.render(status, current_account, template)
+ Rabl::Renderer.new(
+ template,
+ status,
+ view_path: 'app/views',
+ format: :json,
+ scope: InlineRablScope.new(current_account)
+ ).render
+ end
+end
diff --git a/app/services/fan_out_on_write_service.rb b/app/services/fan_out_on_write_service.rb
@@ -50,22 +50,22 @@ class FanOutOnWriteService < BaseService
end
def render_anonymous_payload(status)
- @payload = FeedManager.instance.inline_render(nil, 'api/v1/statuses/show', status)
+ @payload = InlineRenderer.render(status, nil, 'api/v1/statuses/show')
end
def deliver_to_hashtags(status)
Rails.logger.debug "Delivering status #{status.id} to hashtags"
status.tags.pluck(:name).each do |hashtag|
- FeedManager.instance.broadcast("hashtag:#{hashtag}", event: 'update', payload: @payload)
- FeedManager.instance.broadcast("hashtag:#{hashtag}:local", event: 'update', payload: @payload) if status.account.local?
+ Redis.current.publish("hashtag:#{hashtag}", Oj.dump(event: :update, payload: @payload))
+ Redis.current.publish("hashtag:#{hashtag}:local", Oj.dump(event: :update, payload: @payload)) if status.account.local?
end
end
def deliver_to_public(status)
Rails.logger.debug "Delivering status #{status.id} to public timeline"
- FeedManager.instance.broadcast(:public, event: 'update', payload: @payload)
- FeedManager.instance.broadcast('public:local', event: 'update', payload: @payload) if status.account.local?
+ Redis.current.publish('public', Oj.dump(event: 'update', payload: @payload))
+ Redis.current.publish('public:local', Oj.dump(event: 'update', payload: @payload)) if status.account.local?
end
end
diff --git a/app/services/notify_service.rb b/app/services/notify_service.rb
@@ -50,7 +50,7 @@ class NotifyService < BaseService
def create_notification
@notification.save!
return unless @notification.browserable?
- FeedManager.instance.broadcast(@recipient.id, event: 'notification', payload: FeedManager.instance.inline_render(@recipient, 'api/v1/notifications/show', @notification))
+ Redis.current.publish(@recipient.id, Oj.dump(event: :notification, payload: InlineRenderer.render(@notification, @recipient, 'api/v1/notifications/show')))
end
def send_email
diff --git a/app/services/remove_status_service.rb b/app/services/remove_status_service.rb
@@ -65,17 +65,17 @@ class RemoveStatusService < BaseService
redis.zremrangebyscore(FeedManager.instance.key(type, receiver.id), status.id, status.id)
end
- FeedManager.instance.broadcast(receiver.id, event: 'delete', payload: status.id)
+ Redis.current.publish(receiver.id, Oj.dump(event: :delete, payload: status.id))
end
def remove_from_hashtags(status)
status.tags.each do |tag|
- FeedManager.instance.broadcast("hashtag:#{tag.name}", event: 'delete', payload: status.id)
+ Redis.current.publish("hashtag:#{tag.name}", Oj.dump(event: :delete, payload: status.id))
end
end
def remove_from_public(status)
- FeedManager.instance.broadcast(:public, event: 'delete', payload: status.id)
+ Redis.current.publish('public', Oj.dump(event: :delete, payload: status.id))
end
def redis
diff --git a/app/workers/push_update_worker.rb b/app/workers/push_update_worker.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+class PushUpdateWorker
+ include Sidekiq::Worker
+
+ def perform(account_id, status_id)
+ account = Account.find(account_id)
+ status = Status.find(status_id)
+ message = InlineRenderer.render(status, account, 'api/v1/statuses/show')
+
+ Redis.current.publish("timeline:#{account.id}", Oj.dump(event: :update, payload: message, queued_at: (Time.now.to_f * 1000.0).to_i))
+ rescue ActiveRecord::RecordNotFound
+ true
+ end
+end