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
Link 'miq_servers' and 'miq_tasks' tables (make miq_task belongs_to miq_server) #14701
Link 'miq_servers' and 'miq_tasks' tables (make miq_task belongs_to miq_server) #14701
Conversation
0b84b4b
to
604e2a8
Compare
febccc8
to
90d1889
Compare
90d1889
to
afab251
Compare
@gtanzillo @Fryguy could you review |
app/models/miq_task.rb
Outdated
@@ -295,6 +296,17 @@ def process_cancel | |||
end | |||
end | |||
|
|||
def attributes_hash_when_state_change(state, attributes = {}) | |||
case attributes[:state] = state |
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.
I'm not a fan of mixing an assignment and a case at the same time. These can just be two separate lines and it would be clear.
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.
ok, will change
app/models/miq_task.rb
Outdated
attributes[:started_on] = Time.now.utc if started_on.nil? | ||
attributes[:miq_server_id] = MiqServer.my_server.try(:id) | ||
when STATE_FINISHED | ||
attributes[:miq_server_id] = MiqServer.my_server.try(: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.
What do you do in the else
case? Is there even an else case?
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.
started_on
is new column for MiqTask
and idea here is to record timestamp when task become active for the first time. I think going forward there may be more states (in addition to active
which could be added to case.
I think we do not need else
case here at this time.
app/models/miq_task.rb
Outdated
case attributes[:state] = state | ||
when STATE_ACTIVE | ||
attributes[:started_on] = Time.now.utc if started_on.nil? | ||
attributes[:miq_server_id] = MiqServer.my_server.try(: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.
I think this can just be attributes[:miq_server] = MiqServer.my_server
. Also, it can be moved out of the case statement.
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.
@gtanzillo ok, will change
app/models/miq_task.rb
Outdated
attributes = {:state => state, :status => status, :message => message} | ||
attributes[:started_on] = Time.now.utc if state == STATE_ACTIVE && started_on.nil? | ||
attributes = {:status => status, :message => message} | ||
attributes = attributes_hash_when_state_change(state, attributes) |
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.
Why build up a hash like this for update? You can just do
self.status = status
self.message = message
...
save!
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.
@Fryguy agreed, it does look artificial and reasons for that: attempt to avoid repeating the same code
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.
It looks like it would be more simple if I would follow you advice and remove new method
app/models/miq_task.rb
Outdated
def attributes_hash_when_state_change(state, attributes = {}) | ||
case attributes[:state] = state | ||
when STATE_ACTIVE | ||
attributes[:started_on] = Time.now.utc if started_on.nil? |
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 you go with just settings methods, then you do self.started_on ||= Time.now.utc
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.
ok
end | ||
|
||
class MiqTask < ActiveRecord::Base | ||
has_one :job, :class_name => 'CopyServerIdFromJobsToMiqTasks::MiqTask' |
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.
There's no need to link these together because you don't actually use the relationship below. Same goes for Job#miq_task belongs_to up above.
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.
ok
it "copies data from 'jobs.miq_server_id' to 'miq_tasks.miq_server_id'" do | ||
task = task_stub.create!(:name => task_name) | ||
migration_stub(:Job).create!(:miq_server_id => server_id, :miq_task_id => task.id) | ||
migrate |
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.
Same comments from the other PR
- Blank lines around migrate
- create a let for the migration_stub(:Job)
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.
ok, will do
describe CopyServerIdFromJobsToMiqTasks do | ||
let(:task_name) { "Hello Test Task" } | ||
let(:task_stub) { migration_stub(:MiqTask) } | ||
let(:server_id) { 1111 } |
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 hardcode a server id. You can generate one by using ArRegion.anonymous_class_with_ar_region.rails_sequence_start
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.
ok
spec/models/miq_task_spec.rb
Outdated
|
||
describe "#attributes_hash_when_state_change" do | ||
let(:miq_task) { FactoryGirl.create(:miq_task_plain) } | ||
before(:each) 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.
Prefer before
over before(:each)
as each is the default.
spec/models/miq_task_spec.rb
Outdated
describe "#attributes_hash_when_state_change" do | ||
let(:miq_task) { FactoryGirl.create(:miq_task_plain) } | ||
before(:each) do | ||
@miq_task = FactoryGirl.create(:miq_task_plain) |
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.
Why are you creating two miq_tasks (one via a let, and one via an instance var)?
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.
It looks like @miq_task
is not used
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.
forgot to remove
app/models/miq_task.rb
Outdated
@@ -295,6 +296,17 @@ def process_cancel | |||
end | |||
end | |||
|
|||
def attributes_hash_when_state_change(state, attributes = {}) |
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.
Should this be a private method? You can still run specs on it, just mark the specs as describe "#attributes_hash_when_state_change (private)" 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.
as discussed above - this method does look confusing, I will add direct update to appropriate methods.
…ted_on attribute when task become active
afab251
to
3036171
Compare
Checked commits yrudman/manageiq@2c6d405~...3036171 with ruby 2.2.6, rubocop 0.47.1, and haml-lint 0.20.0 db/migrate/20170410130134_copy_server_id_from_jobs_to_miq_tasks.rb
|
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.
👍 LGTM
def up | ||
say_with_time("Copying miq_server_id from jobs table to miq_tasks") do | ||
Job.where.not(:miq_task_id => nil).find_each do |job| | ||
MiqTask.find(job.miq_task_id).update_attributes!(:miq_server_id => job.miq_server_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.
This might an expensive N+1 during a migration if there are a lot of jobs x tasks. .includes
would solve the N+1 on the .find
at least.
Part of #14698 - adding former
Job
specific columns to Task viewWe linked
miq_server
andjobs
table in #14385 in order to show server name asOwner
column on jobs UI. After mergingJob
andMiqTask
layouts in ManageIQ/manageiq-ui-classic#242 UI we will need to showOwner
column (which is server name) on tasks UIAnother benefit of having
miq_server
linked to task (in addition of providing the same UI for job management screens) : in multy-server environment it would be clear on which server we need to check logs if something wrong with taskColumn
miq_task.miq_server_id
already exists but was not in use.This PR i:
belongs_to :miq_server
miq_server_id
to task when task status changed toActive
jobs.miq_server_id
tomiq_tasks.miq_server_id
@miq-bot add-label wip, core, refactoring