Skip to content
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

Merged
merged 3 commits into from May 17, 2017

Conversation

yrudman
Copy link
Contributor

@yrudman yrudman commented Apr 8, 2017

Part of #14698 - adding former Job specific columns to Task view

We linked miq_server and jobs table in #14385 in order to show server name as Owner column on jobs UI. After merging Job and MiqTask layouts in ManageIQ/manageiq-ui-classic#242 UI we will need to show Owner column (which is server name) on tasks UI

Another 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 task

Column miq_task.miq_server_id already exists but was not in use.

This PR i:

  • make miq_task belongs_to :miq_server
  • assign miq_server_id to task when task status changed to Active
  • data migration jobs.miq_server_id to miq_tasks.miq_server_id

@miq-bot add-label wip, core, refactoring

@miq-bot miq-bot changed the title Link 'miq_servers' and 'miq_tasks' tables (make miq_task belongs_to miq_server) [WIP] Link 'miq_servers' and 'miq_tasks' tables (make miq_task belongs_to miq_server) Apr 8, 2017
@yrudman yrudman force-pushed the link-miq_tasks-and-miq-servers-tables branch 2 times, most recently from 0b84b4b to 604e2a8 Compare April 10, 2017 20:06
@yrudman yrudman force-pushed the link-miq_tasks-and-miq-servers-tables branch 4 times, most recently from febccc8 to 90d1889 Compare April 11, 2017 14:06
@yrudman yrudman changed the title [WIP] Link 'miq_servers' and 'miq_tasks' tables (make miq_task belongs_to miq_server) Link 'miq_servers' and 'miq_tasks' tables (make miq_task belongs_to miq_server) Apr 11, 2017
@miq-bot miq-bot removed the wip label Apr 11, 2017
@yrudman yrudman force-pushed the link-miq_tasks-and-miq-servers-tables branch from 90d1889 to afab251 Compare April 11, 2017 14:32
@yrudman
Copy link
Contributor Author

yrudman commented Apr 11, 2017

@gtanzillo @Fryguy could you review

@@ -295,6 +296,17 @@ def process_cancel
end
end

def attributes_hash_when_state_change(state, attributes = {})
case attributes[:state] = state
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, will change

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)
Copy link
Member

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?

Copy link
Contributor Author

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.

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)
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gtanzillo ok, will change

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)
Copy link
Member

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!

Copy link
Contributor Author

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

Copy link
Contributor Author

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

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?
Copy link
Member

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

Copy link
Contributor Author

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'
Copy link
Member

@Fryguy Fryguy May 9, 2017

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.

Copy link
Contributor Author

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
Copy link
Member

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)

Copy link
Contributor Author

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 }
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok


describe "#attributes_hash_when_state_change" do
let(:miq_task) { FactoryGirl.create(:miq_task_plain) }
before(:each) do
Copy link
Member

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.

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)
Copy link
Member

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)?

Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

forgot to remove

@@ -295,6 +296,17 @@ def process_cancel
end
end

def attributes_hash_when_state_change(state, attributes = {})
Copy link
Member

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

Copy link
Contributor Author

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.

@yrudman yrudman force-pushed the link-miq_tasks-and-miq-servers-tables branch from afab251 to 3036171 Compare May 10, 2017 21:23
@miq-bot
Copy link
Member

miq-bot commented May 10, 2017

Checked commits yrudman/manageiq@2c6d405~...3036171 with ruby 2.2.6, rubocop 0.47.1, and haml-lint 0.20.0
4 files checked, 1 offense detected

db/migrate/20170410130134_copy_server_id_from_jobs_to_miq_tasks.rb

Copy link
Member

@gtanzillo gtanzillo left a 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)
Copy link
Member

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.

@Fryguy Fryguy merged commit dced078 into ManageIQ:master May 17, 2017
@Fryguy Fryguy added this to the Sprint 61 Ending May 22, 2017 milestone May 17, 2017
@yrudman yrudman deleted the link-miq_tasks-and-miq-servers-tables branch May 18, 2017 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants