Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from sad16/15-background-jobs
background jobs
- Loading branch information
Showing
46 changed files
with
581 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
$(document).on('turbolinks:load', function() { | ||
onAjaxSuccessSubscribeNotification($('.subscribe-notification-link')); | ||
onAjaxSuccessUnsubscribeNotification($(`.unsubscribe-notification-link`)); | ||
}); | ||
|
||
function onAjaxSuccessSubscribeNotification(elem) { | ||
$(elem) | ||
.on('ajax:success', function(response) { | ||
successSubscribeNotification(response, this); | ||
}) | ||
.on('ajax:error', function(response) { | ||
errorSubscribeNotification(response, this); | ||
}) | ||
} | ||
|
||
function successSubscribeNotification(response, elem) { | ||
var notification = response.detail[0]['notification']; | ||
var notificationBlock = $(elem).closest('.notification'); | ||
insertUnsubscribeNotificationLink(notification, notificationBlock); | ||
onAjaxSuccessUnsubscribeNotification(notificationBlock.find('.unsubscribe-notification-link')); | ||
} | ||
|
||
function insertUnsubscribeNotificationLink(notification, notificationBlock) { | ||
var unsubscribeNotificationLink = `<p><a class="unsubscribe-notification-link" rel="nofollow" data-method="delete" data-remote="true" href="/notifications/${notification.id}">Unsubscribe</a></p>` | ||
notificationBlock.html(unsubscribeNotificationLink); | ||
} | ||
|
||
function onAjaxSuccessUnsubscribeNotification(elem) { | ||
$(elem).on('ajax:success', function(response) { | ||
successUnsubscribeNotification(response, this); | ||
}); | ||
} | ||
|
||
function successUnsubscribeNotification(response, elem) { | ||
var notification = response.detail[0]['notification']; | ||
var notificationBlock = $(elem).closest('.notification'); | ||
insertSubscribeNotificationLink(notification, notificationBlock); | ||
onAjaxSuccessSubscribeNotification(notificationBlock.find('.subscribe-notification-link')); | ||
} | ||
|
||
function insertSubscribeNotificationLink(notification, notificationBlock) { | ||
var subscribeNotificationLink = `<p><a class="subscribe-notification-link" rel="nofollow" data-method="post" data-remote="true" href="/questions/${notification.question_id}/notifications">Subscribe</a></p>` | ||
notificationBlock.html(subscribeNotificationLink); | ||
} | ||
|
||
function errorSubscribeNotification(response, elem) { | ||
var errors = response.detail[0]['errors']; | ||
var notificationBlock = $(elem).closest('.notification'); | ||
notificationBlock.append(errors); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
class NotificationsController < ApplicationController | ||
before_action :authenticate_user!, only: [:create, :destroy] | ||
|
||
def create | ||
question = Question.find(params[:question_id]) | ||
notification = Notification.new(user: current_user, question: question) | ||
|
||
if notification.save | ||
render json: notification | ||
else | ||
render json: { errors: notification.errors.full_messages }, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def destroy | ||
notification = Notification.find(params[:id]) | ||
|
||
authorize notification | ||
|
||
notification.destroy | ||
|
||
render json: notification | ||
end | ||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class DailyDigestJob < ApplicationJob | ||
queue_as :default | ||
|
||
def perform | ||
Services::SendDailyDigest.new.call | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class NotificationsJob < ApplicationJob | ||
queue_as :default | ||
|
||
def perform(answer) | ||
Services::SendNotifications.new.call(answer) | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class DailyDigestMailer < ApplicationMailer | ||
def digest(user) | ||
@questions_data = Services::DailyDigestData.new.call | ||
mail to: user.email | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class NotificationMailer < ApplicationMailer | ||
def new_answer(user, answer) | ||
@answer = answer | ||
mail to: user.email | ||
end | ||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class Notification < ApplicationRecord | ||
belongs_to :user | ||
belongs_to :question | ||
|
||
validates :user_id, uniqueness: { scope: [:question_id] } | ||
end |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class NotificationPolicy < ApplicationPolicy | ||
def destroy? | ||
owner_of? | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class NotificationSerializer < ApplicationSerializer | ||
attributes :id, :question_id, :user_id | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module Services | ||
class DailyDigestData < ApplicationService | ||
CACHE_KEY = 'daily_digest_data'.freeze | ||
|
||
def call | ||
Rails.cache.fetch(CACHE_KEY, expires_in: cache_expires_in) do | ||
questions.map { |q| { id: q.id, title: q.title } } | ||
end | ||
end | ||
|
||
private | ||
|
||
def questions | ||
Question.where('created_at >= ? AND created_at <= ?', yesterday.beginning_of_day, yesterday.end_of_day) | ||
end | ||
|
||
def yesterday | ||
@yesterday ||= Date.yesterday | ||
end | ||
|
||
def cache_expires_in | ||
Date.today.end_of_day | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module Services | ||
class SendDailyDigest < ApplicationService | ||
BATCH_SIZE = 500 | ||
|
||
def call | ||
User.find_each(batch_size: BATCH_SIZE) { |u| deliver_mail(u) } if questions_data.present? | ||
end | ||
|
||
private | ||
|
||
def deliver_mail(user) | ||
DailyDigestMailer.digest(user).deliver_later | ||
end | ||
|
||
def questions_data | ||
Services::DailyDigestData.new.call | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Services | ||
class SendNotifications < ApplicationService | ||
BATCH_SIZE = 500 | ||
|
||
def call(answer) | ||
answer.question.subscribed_users.find_each(batch_size: BATCH_SIZE) { |u| deliver_mail(u, answer) } | ||
end | ||
|
||
private | ||
|
||
def deliver_mail(user, answer) | ||
NotificationMailer.new_answer(user, answer).deliver_later | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
p Hi, it is daily digest! | ||
p Yesterday questions: | ||
- @questions_data.each do |q| | ||
= link_to q[:title], question_url(id: q[:id]) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
p Hi | ||
p New answer by question: | ||
p = @answer.question.title | ||
p Answer | ||
p = @answer.body |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.