Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ coverage
config/settings.local.yml
config/settings/*.local.yml
config/environments/*.local.yml

# IDE files
/.idea/*
3 changes: 3 additions & 0 deletions .sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ SECRET_KEY_BASE=
PGPASSWORD=postgres
[email protected]
INDEX_CONCURRENT_PROCESSES=

MONGO_HOST=mongo
MONGO_PORT=27017
10 changes: 10 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ gem 'puma', '>= 6.5.0'

# Базы данных
gem 'pg'
gem 'mongoid'

# Многопоточное выполнение
gem 'parallel'
Expand All @@ -14,6 +15,9 @@ gem 'activerecord-import'
gem 'sprockets-rails'
gem 'slim-rails'

# Serializers
gem 'jbuilder'

# Распаковка архивов
gem 'rubyzip', require: 'zip'

Expand All @@ -29,6 +33,12 @@ gem 'activeadmin'
gem 'activeadmin_addons'
gem 'devise'

# Паджинация
gem 'kaminari-mongoid'

# Брокеры сообщений
gem 'karafka', '~> 2.2'

group :development, :test do
gem 'bundler-audit'
gem 'capybara'
Expand Down
34 changes: 34 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ GEM
benchmark (0.4.0)
bigdecimal (3.1.8)
bindex (0.8.1)
bson (5.0.2)
builder (3.3.0)
bundler-audit (0.9.2)
bundler (>= 1.2.0, < 3)
Expand Down Expand Up @@ -193,6 +194,9 @@ GEM
irb (1.14.0)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
jbuilder (2.13.0)
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
jquery-rails (4.6.0)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
Expand All @@ -210,6 +214,22 @@ GEM
activerecord
kaminari-core (= 1.2.2)
kaminari-core (1.2.2)
kaminari-mongoid (1.0.2)
kaminari-core (~> 1.0)
mongoid
karafka (2.4.17)
base64 (~> 0.2)
karafka-core (>= 2.4.4, < 2.5.0)
karafka-rdkafka (>= 0.17.2)
waterdrop (>= 2.7.3, < 3.0.0)
zeitwerk (~> 2.3)
karafka-core (2.4.9)
karafka-rdkafka (>= 0.17.6, < 0.19.0)
logger (>= 1.6.0)
karafka-rdkafka (0.18.1)
ffi (~> 1.15)
mini_portile2 (~> 2.6)
rake (> 12)
language_server-protocol (3.17.0.3)
logger (1.6.0)
loofah (2.23.1)
Expand All @@ -226,6 +246,12 @@ GEM
mini_mime (1.1.5)
mini_portile2 (2.8.8)
minitest (5.25.1)
mongo (2.21.0)
bson (>= 4.14.1, < 6.0.0)
mongoid (9.0.6)
activemodel (>= 5.1, < 8.1, != 7.0.0)
concurrent-ruby (>= 1.0.5, < 2.0)
mongo (>= 2.18.0, < 3.0.0)
net-imap (0.5.6)
date
net-protocol
Expand Down Expand Up @@ -420,6 +446,10 @@ GEM
useragent (0.16.10)
warden (1.2.9)
rack (>= 2.0.9)
waterdrop (2.8.2)
karafka-core (>= 2.4.3, < 3.0.0)
karafka-rdkafka (>= 0.17.5)
zeitwerk (~> 2.3)
web-console (4.2.1)
actionview (>= 6.0.0)
activemodel (>= 6.0.0)
Expand Down Expand Up @@ -460,6 +490,10 @@ DEPENDENCIES
factory_bot_rails
fasterer
ffaker
jbuilder
kaminari-mongoid
karafka (~> 2.2)
mongoid
parallel
pg
pry-byebug
Expand Down
Empty file added app/consumers/.keep
Empty file.
10 changes: 10 additions & 0 deletions app/controllers/books_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class BooksController < ApplicationController
def index
@books = Documents::Book.all.page(params[:page])

# Продьюсим событие в Кафка, которое показывает, какая страница была открыта
BooksProducer.page_opened(42)

render formats: :json
end
end
19 changes: 19 additions & 0 deletions app/models/documents/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Documents
class Book
include Mongoid::Document
include Mongoid::Timestamps

field :title, type: String
field :series, type: String
field :serno, type: String
field :libid, type: Integer
field :size, type: Integer
field :filename, type: String
field :del, type: Boolean
field :ext, type: String
field :published_at, type: Date
field :insno, type: String
field :folder_id, type: String
field :language_id, type: String
end
end
12 changes: 12 additions & 0 deletions app/producers/books_producer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

require 'karafka'

class BooksProducer
def self.page_opened(page_number)
Karafka.producer.produce_sync(
topic: 'books',
payload: { page_number: page_number }.to_json
)
end
end
1 change: 1 addition & 0 deletions app/views/books/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.array! @books, :id, :title
5 changes: 3 additions & 2 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ default: &default
postgre: &postgre
host: <%= ENV.fetch('POSTGRES_HOST', 'localhost') %>
port: <%= ENV.fetch('POSTGRES_PORT', '5432') %>
username: <%= ENV.fetch('POSTGRES_USER') { 'igorsimdyanov' } %>
password: <%= ENV.fetch('POSTGRES_PASSWORD') { '' } %>
username: <%= ENV.fetch('POSTGRES_USER') { 'postgres' } %>
password: <%= ENV.fetch('POSTGRES_PASSWORD') { 'postgres' } %>

development:
<<: *default
Expand All @@ -17,6 +17,7 @@ development:

test:
<<: *default
<<: *postgre
database: library_test

production:
Expand Down
3 changes: 3 additions & 0 deletions config/initializers/kaminari_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Kaminari.configure do |config|
config.default_per_page = Settings.app.items_per_page
end
25 changes: 25 additions & 0 deletions config/initializers/mongoid.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# rubocop:todo all
Mongoid.configure do
target_version = "9.0"

# Load Mongoid behavior defaults. This automatically sets
# features flags (refer to documentation)
config.load_defaults target_version

# It is recommended to use config/mongoid.yml for most Mongoid-related
# configuration, whenever possible, but if you prefer, you can set
# configuration values here, instead:
#
# config.log_level = :debug
#
# Note that the settings in config/mongoid.yml always take precedence,
# whatever else is set here.
end

# Enable Mongo driver query cache for Rack
# Rails.application.config.middleware.use(Mongo::QueryCache::Middleware)

# Enable Mongo driver query cache for ActiveJob
# ActiveSupport.on_load(:active_job) do
# include Mongo::QueryCache::Middleware::ActiveJob
# end
Loading