-
Notifications
You must be signed in to change notification settings - Fork 624
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move order submit to order_processor and remove Ordering service #2147
Conversation
dd774e3
to
86d2746
Compare
|
||
AMQPQueue.enqueue(:matching, action: 'submit', order: order.to_matching_attributes) | ||
end | ||
rescue e |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there was an issue on order submit wee need probably to reject it
2120670
to
073ca61
Compare
app/api/v2/helpers.rb
Outdated
order | ||
rescue => e | ||
message = create_order_errors.fetch(e.class, 'market.order.create_error') | ||
report_exception_to_screen(e) | ||
error!({ errors: [message] }, 422) | ||
end | ||
|
||
def submit(order) | ||
ActiveRecord::Base.transaction do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't need transaction here
app/api/v2/helpers.rb
Outdated
{ persistent: false } | ||
end | ||
|
||
def cancel(order) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to move trading related method into separate Helper
order = Order.find_by_id(payload.dig('order', 'id')) | ||
cancel(order) if order | ||
when 'submit' | ||
order = Order.find_by_id(payload.dig('order', 'id')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Use strict find_by_id!
- In case if order doesn't exist publish message to trade_errors queue
Optional: Remove duplicated
order = Order.find_by_id(payload.dig('order', 'id'))
order = Order.find_by_id(payload.dig('order', 'id')) | ||
cancel(order) if order | ||
when 'submit' | ||
order = Order.find_by_id(payload.dig('order', 'id')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lock order directly after finding
end | ||
end | ||
|
||
private | ||
|
||
def submit(order) | ||
order.with_lock do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use active_record transaction here and lock in process
app/trading/matching/executor.rb
Outdated
raise_error(3003, 'Ask state isn\'t equal to «wait».') unless @ask.state == Order::WAIT | ||
raise_error(3004, 'Bid state isn\'t equal to «wait».') unless @bid.state == Order::WAIT | ||
raise_error(3003, "Ask state isn\'t equal to «wait» (#{ask.state}).") unless @ask.state == Order::WAIT | ||
raise_error(3004, "Bid state isn\'t equal to «wait» (#{ask.state}).") unless @bid.state == Order::WAIT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bid.state
def cancel(order) | ||
Ordering.new(order).cancel! | ||
rescue StandardError => e | ||
order.with_lock do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use active_record transaction here and lock in process
app/api/v2/helpers.rb
Outdated
@@ -65,6 +65,10 @@ def build_order(attrs) | |||
origin_volume: attrs[:volume] | |||
end | |||
|
|||
def check_balance(order) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrong business logic for bid order and spec
c415437
to
c8cd1ef
Compare
app/api/v2/ordering.rb
Outdated
@@ -0,0 +1,61 @@ | |||
module API | |||
module V2 | |||
module Ordering |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OrderHelpers
def process(payload) | ||
order = Order.find_by_id!(payload.dig('order', 'id')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you nee to lock order here right after finding
@@ -3,21 +3,56 @@ | |||
|
|||
module Worker | |||
class OrderProcessor | |||
def initialize | |||
Order.where(state: ::Order::PENDING).each do |order| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
better to use find_each
here
end | ||
|
||
private | ||
|
||
def submit(order) | ||
ActiveRecord::Base.transaction do | ||
order.with_lock do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't need with_lock here if you lock order here right after finding
Ordering.new(order).cancel! | ||
rescue StandardError => e | ||
ActiveRecord::Base.transaction do | ||
order.with_lock do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't need with_lock here if you lock order here right after finding
aea8f01
to
94e0cb5
Compare
- Add new state `Pending` for orders; - Add fast check_balance before publishing message; - Publish message for submitting order from API; - Delete Ordering module;
pending
&reject
for orders;