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
1 change: 1 addition & 0 deletions check-manager/.tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby 3.3.6
14 changes: 14 additions & 0 deletions check-manager/app/contexts/checks/create_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Checks::CreateService
include Callable
extend Dry::Initializer

param :message

def call
attachment_id = message.dig(:payload, :attachment_id)

raise Error, 'Attachment id can not be blank'

Check.find_or_create_by(attachment_id: attachment_id)
end
end
15 changes: 15 additions & 0 deletions check-manager/app/contexts/checks/save_message_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Checks::SaveMessageService
include Callable
extend Dry::Initializer

param :attachment_id
param :message

def call
check = Check.find_or_create_by(attachment_id: attachment_id)
check.with_lock do
check.messages << message
check.save
end
end
end
59 changes: 59 additions & 0 deletions check-manager/app/contexts/checks/tesseract_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
class Checks::TesseractService
include Callable
extend Dry::Initializer

param :message

def call
file_path = message.dig(:payload, :file_path)
file = download_file(file_path)
response = send_to_tesseract(file)

raise Error, 'Tesseract problem' unless response.is_a?(Net::HTTPSuccess)

Checks::SaveMessageService.call(message.dig(:payload, :attachment_id), response.body)
end

private


def service

end

def download_file(url)
uri = URI.parse(url)

Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вообще предполагалаось, что вы все-таки будет опираться на события RabbitMQ, а не воспользуетесь REST-овым обращением через HTTP. Но наверное это я запутал вас в задании.

request = Net::HTTP::Get.new(uri)

http.request(request) do |response|
raise Error, "Failed to download file: #{response.code} #{response.message}" unless response.is_a?(Net::HTTPSuccess)

temp_file = Tempfile.new(["downloaded_file_#{Time.current.to_i}", File.extname(uri.path)])
temp_file.binmode

response.read_body do |chunk|
temp_file.write(chunk)
end

temp_file.close
return temp_file
end
end
end

def send_to_tesseract(file)
uri = URI.parse(ENV['TESSERACT_URL'])

file_content = File.binread(file.path)

request = Net::HTTP::Post.new(uri)
request['Content-Type'] = 'application/x-www-form-urlencoded'
request.body = file_content

Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
http.request(request)
end
end
end
12 changes: 12 additions & 0 deletions check-manager/app/contexts/pays/analyse_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,22 @@ class Pays::AnalyseService
param :message

def call
return unless available_send?
next_message = Pays::BuildMessageService.call(
message: message,
check_messages: check.messages,
routing_key: 'pay_analyse')

RabbitMessages::Send.call(next_message)
end

private

def available_send?
check&.messages&.size >= ENV['MESSAGES_SIZE']
end

def check
@check ||= Check.find_by(attachment_id: message.dig(:payload, :attachment_id))
end
end
3 changes: 2 additions & 1 deletion check-manager/app/contexts/pays/build_message_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Pays::BuildMessageService

option :message
option :routing_key
option :check_messages

def call
{
Expand All @@ -13,7 +14,7 @@ def call
full_path: message.dig(:payload, :full_path),
attachment_id: message.dig(:payload, :attachment_id),
created_at: message.dig(:payload, :created_at),
message: message.dig(:payload, :message)
message: check_messages
},
created_at: Time.now.utc.to_i,
routing_key: routing_key
Expand Down
14 changes: 14 additions & 0 deletions check-manager/app/contexts/pays/save_message_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Pays::SaveMessageService
include Callable
extend Dry::Initializer

param :message

def call
attachment_id = message.dig(:payload, :attachment_id)

raise Error, 'Attachment id can not be blank'

Checks::SaveMessageService.call(attachment_id, message.dig(:payload, :message))
end
end
2 changes: 2 additions & 0 deletions check-manager/app/models/check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Check < ApplicationRecord
end
8 changes: 4 additions & 4 deletions check-manager/app/workers/inbox_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class InboxListener
include RabbitMessages::Logging

ACTION_HANDLERS = {
attachment_registered: Checks::RecognizeService
attachment_registered: [Checks::CreateService, Checks::TesseractService, Checks::RecognizeService]
}.freeze
QUEUE_NAME = Settings.sneakers.inbox_queue
PG_EXCEPTION = [
Expand Down Expand Up @@ -42,10 +42,10 @@ def process_message
parsed_message,
RabbitMessage::INCOME_MESSAGE,
action)
handler = ACTION_HANDLERS[action&.to_sym]
raise Error, "Action #{action} is not supported" unless handler
handlers = ACTION_HANDLERS[action&.to_sym]
raise Error, "Action #{action} is not supported" unless handlers

handler.call(parsed_message)
handlers.each { |handler| handler.call(message) }

rabbit_message.update!(success: true)
end
Expand Down
2 changes: 1 addition & 1 deletion check-manager/app/workers/recognizer_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class RecognizerListener
include RabbitMessages::Logging

ACTION_HANDLERS = {
recognized_result: Pays::AnalyseService
recognized_result: [Pays::SaveMessageService, Pays::AnalyseService]
}.freeze
QUEUE_NAME = Settings.sneakers.recognizer_queue
PG_EXCEPTION = [
Expand Down
10 changes: 10 additions & 0 deletions check-manager/db/migrate/20251118181614_create_checks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateChecks < ActiveRecord::Migration[8.0]
def change
create_table :checks do |t|
t.text :messages, array: true, default: []
t.bigint :attachment_id, index: true

t.timestamps
end
end
end
10 changes: 9 additions & 1 deletion check-manager/db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions check-manager/spec/factories/checks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryBot.define do
factory :check do

end
end
5 changes: 5 additions & 0 deletions check-manager/spec/models/check_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Check, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end