Skip to content

Commit

Permalink
Allow cancelling of overdue edition
Browse files Browse the repository at this point in the history
If something goes wrong within the scheduled publishing worker, be it a state transition
or a downstream error coming from publishing-api, it will return without changing the edition
state (remains scheduled) and will retry.

Nonetheless, if the worker cannot fix itself, we want to allow the users to cancel the current
schedule and re-schedule.

Due to a validation restriction on the edition model, we were blocking this. The publication
time would in this case be in the past which was not allowed on the draft state. We are now allowing
it if the previous state has been scheduled.
  • Loading branch information
lauraghiorghisor-tw committed Apr 21, 2024
1 parent 1a4c0e6 commit 8ecae31
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/models/travel_advice_edition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def parts_valid?
end

def validate_scheduled_publication_time
errors.add(:scheduled_publication_time, "can't be in the past") if scheduled_publication_time && scheduled_publication_time <= Time.zone.now
errors.add(:scheduled_publication_time, "can't be in the past") if scheduled_publication_time && scheduled_publication_time <= Time.zone.now && state_was != "scheduled"
end

def extract_part_errors
Expand Down
11 changes: 11 additions & 0 deletions spec/controllers/admin/schedulings_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,17 @@
expect(flash[:alert]).to include("We had some problems cancelling")
expect(response).to redirect_to edit_admin_edition_path(@edition)
end

it "cancels an overdue scheduled edition (failed to publish)" do
scheduled_publication_time = 1.hour.from_now
edition = create(:scheduled_travel_advice_edition, country_slug: "albania", scheduled_publication_time:)

travel_to 2.hours.from_now
delete :destroy, params: { edition_id: edition.id }

expect(edition.reload.scheduled_publication_time).to be_nil
expect(edition.reload.state).to eq("draft")
end
end

def generate_scheduling_params(date_time)
Expand Down
12 changes: 11 additions & 1 deletion spec/models/travel_advice_edition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -477,14 +477,24 @@

context "cancel schedule for publication" do
let(:user) { create(:user) }
let(:country) { Country.find_by_slug("afghanistan") }
let!(:country) { Country.find_by_slug("afghanistan") }

it "sends a cancel schedule action" do
scheduled = create(:scheduled_travel_advice_edition, country_slug: country.slug)
scheduled.cancel_schedule_for_publication(user)

expect(scheduled.actions.first.request_type).to eq "cancel_schedule"
end

it "allows cancelling a failed scheduled edition (failed to publish)" do
scheduled_overdue = create(:scheduled_travel_advice_edition, country_slug: country.slug)
travel_to(2.hours.from_now)

scheduled_overdue.cancel_schedule_for_publication(user)

expect(scheduled_overdue.reload.scheduled_publication_time).to be_nil
expect(scheduled_overdue.reload.state).to eq "draft"
end
end
end

Expand Down

0 comments on commit 8ecae31

Please sign in to comment.