-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
211 additions
and
6 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,11 @@ | ||
module Photos | ||
class DumpViewsJob | ||
include Sidekiq::Worker | ||
|
||
def perform | ||
RedisMutex.with_lock('counters:photo', block: 5.minutes, expire: 30.minutes) do | ||
::Counters::DumpService.call!(model_klass: ::Photo) | ||
end | ||
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,10 @@ | ||
module Countable | ||
def inc_counter | ||
return unless persisted? | ||
|
||
RedisClassy. | ||
redis. | ||
incr("counters:#{self.class.to_s.underscore}:#{id}"). | ||
to_i | ||
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
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,36 @@ | ||
module Counters | ||
class DumpService | ||
include ::Interactor | ||
|
||
delegate :model_klass, to: :context | ||
|
||
def call | ||
# batches? | ||
redis. | ||
scan_each(match: key_for('*')). | ||
each do |key| | ||
model = model_klass.where(id: key.gsub(/[^\d]+/, '').to_i).first | ||
dump_counter(model) if model | ||
end | ||
end | ||
|
||
private | ||
|
||
delegate :redis, to: RedisClassy | ||
|
||
def dump_counter(model) | ||
key = key_for(model.id) | ||
|
||
value = redis.multi do | ||
redis.get(key) | ||
redis.del(key) | ||
end.first | ||
|
||
model.update_column(:views, model.views + value.to_i) if value.present? | ||
end | ||
|
||
def key_for(id) | ||
"counters:#{model_klass.to_s.underscore}:#{id}" | ||
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
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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
yandex_refresh: | ||
cron: '0 1 * * *' | ||
class: Yandex::RefreshTokensJob | ||
|
||
dump_photo_views: | ||
cron: '0 2 * * *' | ||
class: Photos::DumpViewsJob |
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 AddViewsToPhotos < ActiveRecord::Migration[5.2] | ||
def change | ||
add_column :photos, :views, :bigint, default: 0, null: false | ||
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
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,12 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Photos::DumpViewsJob do | ||
before do | ||
allow(Counters::DumpService).to receive(:call!) | ||
described_class.perform_async | ||
end | ||
|
||
it do | ||
expect(Counters::DumpService).to have_received(:call!).with(model_klass: Photo) | ||
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,42 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Counters::DumpService do | ||
before { RedisClassy.flushdb } | ||
after { RedisClassy.flushdb } | ||
|
||
let(:redis) { RedisClassy.redis } | ||
|
||
subject { described_class.call!(model_klass: Photo) } | ||
|
||
let!(:photo_without_views) { create :photo, :fake, local_filename: 'test', views: 0 } | ||
let!(:photo_with_views) { create :photo, :fake, local_filename: 'test', views: 1_000 } | ||
let!(:wrong_photo) { create :photo, :fake, local_filename: 'test', views: 2_000 } | ||
|
||
context 'when no counters in redis' do | ||
it do | ||
expect { subject }. | ||
to change { photo_without_views.reload.views }.by(0). | ||
and change { photo_with_views.reload.views }.by(0). | ||
and change { wrong_photo.reload.views }.by(0) | ||
end | ||
end | ||
|
||
context 'when information presents' do | ||
before do | ||
redis.set("counters:photo:#{photo_without_views.id}", 100) | ||
redis.set("counters:photo:#{photo_with_views.id}", 10) | ||
redis.set("counters:photo:#{photo_with_views.id * 2}", 1_000) | ||
end | ||
|
||
context 'when regular call' do | ||
it do | ||
expect { subject }. | ||
to change { photo_without_views.reload.views }.by(100). | ||
and change { photo_with_views.reload.views }.by(10). | ||
and change { wrong_photo.reload.views }.by(0) | ||
|
||
expect(redis.keys).to match_array(["counters:photo:#{photo_with_views.id * 2}"]) | ||
end | ||
end | ||
end | ||
end |
Empty file.
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,44 @@ | ||
RSpec.shared_context 'model with counter' do |factory| | ||
describe '#inc_counter' do | ||
before { RedisClassy.flushdb } | ||
after { RedisClassy.flushdb } | ||
|
||
let(:redis) { RedisClassy.redis } | ||
|
||
subject { model.inc_counter } | ||
|
||
context 'when persisted' do | ||
let(:model) { create :photo, :fake, local_filename: 'test' } | ||
|
||
context 'and first view' do | ||
it do | ||
expect { subject }. | ||
to change { redis.get("counters:#{model.class.to_s.underscore}:#{model.id}") }.from(nil).to('1') | ||
|
||
is_expected.to eq(1) | ||
end | ||
end | ||
|
||
context 'and second view' do | ||
before { model.inc_counter } | ||
|
||
it do | ||
expect { subject }. | ||
to change { redis.get("counters:#{model.class.to_s.underscore}:#{model.id}") }.from('1').to('2') | ||
|
||
is_expected.to eq(2) | ||
end | ||
end | ||
end | ||
|
||
context 'when not persisted' do | ||
let(:model) { build factory } | ||
|
||
it do | ||
expect { subject }.not_to(change { RedisClassy.redis.keys('counters:*') }) | ||
|
||
is_expected.to be_nil | ||
end | ||
end | ||
end | ||
end |