Skip to content

Commit

Permalink
Merge pull request #163 from DigitalNZ/rm/sustainable-resumption
Browse files Browse the repository at this point in the history
SUSTAINABLE RESUMPTION DATA: As Ting, I want to be sure SJ doesnt keep too much info during long harvests, so that harvests run efficiently without breaking things
  • Loading branch information
richardmatthewsdev authored Mar 11, 2021
2 parents b682335 + 10e9944 commit 4263e6d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
7 changes: 7 additions & 0 deletions app/models/abstract_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class AbstractJob

after_save :check_if_job_should_be_resumed, if: :status_changed?

after_save :clear_old_states!

field :start_time, type: DateTime
field :end_time, type: DateTime
field :updated_at, type: DateTime
Expand Down Expand Up @@ -221,4 +223,9 @@ def increment_processed_count!
def check_if_job_should_be_resumed
self.resume! if self.status_change.last == 'resume'
end

def clear_old_states!
return if states.count <= 5
states.first.destroy!
end
end
1 change: 1 addition & 0 deletions spec/factories/state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
limit { 100 }
counter { 1 }
base_urls { [] }
created_at { Time.now }
end
end
20 changes: 20 additions & 0 deletions spec/models/abstract_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -440,4 +440,24 @@
job.update_attributes(status: 'stopped')
end
end

describe '#job_states' do
let(:stateful_job) { create(:harvest_job, :stateful) }
let(:states) { build_list(:state, 5) }
let(:full_job) { create(:harvest_job, states: states) }

it 'can have an embedded history of the job state' do
expect(stateful_job.states.count).not_to eq(0)
expect(stateful_job.states.first).to be_a(State)
end

it 'only embedds 5 states into the job' do
expect(full_job.states.count).to eq 5
full_job.states << build(:state, created_at: 10.minutes.from_now)
full_job.save!
full_job.reload
expect(full_job.states.count).to eq 5
expect(full_job.states.last.created_at.strftime('%H %d %p')).to eq 10.minutes.from_now.strftime('%H %d %p')
end
end
end
9 changes: 0 additions & 9 deletions spec/models/harvest_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,4 @@
expect(full_and_flush_job_with_no_records.full_and_flush_available?).to eq false
end
end

describe '#job_states' do
let(:stateful_job) { create(:harvest_job, :stateful) }

it 'can have an embedded history of the job state' do
expect(stateful_job.states.count).not_to eq(0)
expect(stateful_job.states.first).to be_a(State)
end
end
end

0 comments on commit 4263e6d

Please sign in to comment.