Skip to content

Commit 217dcac

Browse files
authored
Merge pull request #1490 from Shopify/tdickers/retry-unauthorized-create-on-github
0.45.3: Retry CreateOnGithubJob on GitHub auth 401s
2 parents 3b20110 + 13f0b33 commit 217dcac

6 files changed

Lines changed: 49 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Unreleased
22

3+
# 0.45.3
4+
* Retry CreateOnGithubJob on transient GitHub authentication failures.
5+
* Stabilize PerformTaskJob tests by stubbing the task execution strategy instead of Command#stream!.
6+
37
# 0.45.2
48
* (bugfix) Fix 404 error when removing all permissions from an API client
59

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
shipit-engine (0.45.2)
4+
shipit-engine (0.45.3)
55
active_model_serializers (~> 0.9.3)
66
ansi_stream (~> 0.0.6)
77
autoprefixer-rails (~> 6.4.1)

app/jobs/shipit/create_on_github_job.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ class CreateOnGithubJob < BackgroundJob
77
queue_as :default
88
on_duplicate :drop
99

10+
# Transient Octokit::Unauthorized = GitHub installation-token propagation lag.
11+
# attempts: 14 (~24h) outlasts the 50m token cache (GITHUB_TOKEN_RAILS_CACHE_LIFETIME).
12+
# No token eviction here to avoid a remint storm across workers.
13+
retry_on Octokit::Unauthorized, wait: :polynomially_longer, attempts: 14 do |job, exception|
14+
record = job.arguments.first
15+
Rails.logger.warn(
16+
"[CreateOnGithubJob] Giving up on #{record.class.name} #{record.id} " \
17+
"after GitHub authentication failures: #{exception.class} #{exception.message}"
18+
)
19+
end
20+
1021
# We observe that some objects regularly take longer than the default 10 seconds to create, e.g. deployments
1122
self.timeout = 40
1223
self.lock_timeout = 20

lib/shipit/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module Shipit
4-
VERSION = '0.45.2'
4+
VERSION = '0.45.3'
55
end

test/jobs/perform_task_job_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def success?
107107
end
108108

109109
test "mark deploy as error an unexpected exception is raised" do
110-
Command.any_instance.expects(:stream!).at_least_once.raises(Command::Denied)
110+
Shipit::TaskExecutionStrategy::Default.any_instance.expects(:capture!).at_least_once.raises(Command::Denied)
111111

112112
@job.perform(@deploy)
113113

@@ -116,7 +116,7 @@ def success?
116116
end
117117

118118
test "mark deploy as timedout if a command timeout" do
119-
Command.any_instance.expects(:stream!).at_least_once.raises(Command::TimedOut)
119+
Shipit::TaskExecutionStrategy::Default.any_instance.expects(:capture!).at_least_once.raises(Command::TimedOut)
120120

121121
@job.perform(@deploy)
122122

@@ -129,7 +129,7 @@ def success?
129129
begin
130130
Shipit.timeout_exit_codes = [70].freeze
131131

132-
Command.any_instance.expects(:stream!).at_least_once.raises(Command::Failed.new('Blah', 70))
132+
Shipit::TaskExecutionStrategy::Default.any_instance.expects(:capture!).at_least_once.raises(Command::Failed.new('Blah', 70))
133133

134134
@job.perform(@deploy)
135135

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# frozen_string_literal: true
2+
3+
require 'test_helper'
4+
5+
module Shipit
6+
class CreateOnGithubJobTest < ActiveSupport::TestCase
7+
setup do
8+
@deployment = shipit_commit_deployments(:shipit_pending_fourth)
9+
end
10+
11+
test "#perform retries on GitHub authentication errors" do
12+
CommitDeployment.any_instance.stubs(:create_on_github!).raises(Octokit::Unauthorized)
13+
14+
assert_enqueued_with(job: CreateOnGithubJob) do
15+
CreateOnGithubJob.perform_now(@deployment)
16+
end
17+
end
18+
19+
test "#perform gives up without re-raising after exhausting authentication retries" do
20+
CommitDeployment.any_instance.stubs(:create_on_github!).raises(Octokit::Unauthorized)
21+
Rails.logger.stubs(:warn)
22+
23+
job = CreateOnGithubJob.new(@deployment)
24+
job.exception_executions = { "[Octokit::Unauthorized]" => 13 }
25+
26+
assert_nothing_raised { job.perform_now }
27+
end
28+
end
29+
end

0 commit comments

Comments
 (0)