Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
99043d24
Commit
99043d24
authored
Dec 16, 2017
by
Matija Čupić
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add lease to CheckGcpProjectBillingWorker
parent
78f85f3f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
9 deletions
+38
-9
app/workers/check_gcp_project_billing_worker.rb
app/workers/check_gcp_project_billing_worker.rb
+11
-0
spec/workers/check_gcp_project_billing_worker_spec.rb
spec/workers/check_gcp_project_billing_worker_spec.rb
+27
-9
No files found.
app/workers/check_gcp_project_billing_worker.rb
View file @
99043d24
class
CheckGcpProjectBillingWorker
include
ApplicationWorker
LEASE_TIMEOUT
=
1
.
minute
.
to_i
def
self
.
redis_shared_state_key_for
(
token
)
"gitlab:gcp:
#{
token
}
:billing_enabled"
end
def
perform
(
token
)
return
unless
token
return
unless
try_obtain_lease_for
(
token
)
billing_enabled
=
CheckGcpProjectBillingService
.
new
.
execute
(
token
)
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
redis
.
set
(
self
.
class
.
redis_shared_state_key_for
(
token
),
billing_enabled
)
end
end
private
def
try_obtain_lease_for
(
token
)
Gitlab
::
ExclusiveLease
.
new
(
"check_gcp_project_billing_worker:
#{
token
}
"
,
timeout:
LEASE_TIMEOUT
)
.
try_obtain
end
end
spec/workers/check_gcp_project_billing_worker_spec.rb
View file @
99043d24
...
...
@@ -5,20 +5,38 @@ describe CheckGcpProjectBillingWorker do
let
(
:token
)
{
'bogustoken'
}
subject
{
described_class
.
new
.
perform
(
token
)
}
it
'calls the service'
do
expect
(
CheckGcpProjectBillingService
).
to
receive_message_chain
(
:new
,
:execute
)
context
'when there is no lease'
do
before
do
allow_any_instance_of
(
CheckGcpProjectBillingWorker
).
to
receive
(
:try_obtain_lease_for
).
and_return
(
'randomuuid'
)
end
subject
it
'calls the service'
do
expect
(
CheckGcpProjectBillingService
).
to
receive_message_chain
(
:new
,
:execute
)
subject
end
it
'stores billing status in redis'
do
redis_double
=
double
expect
(
CheckGcpProjectBillingService
).
to
receive_message_chain
(
:new
,
:execute
).
and_return
(
true
)
expect
(
Gitlab
::
Redis
::
SharedState
).
to
receive
(
:with
).
and_yield
(
redis_double
)
expect
(
redis_double
).
to
receive
(
:set
).
with
(
CheckGcpProjectBillingWorker
.
redis_shared_state_key_for
(
token
),
anything
)
subject
end
end
it
'stores billing status in redis'
do
redis_double
=
double
context
'when there is a lease'
do
before
do
allow_any_instance_of
(
CheckGcpProjectBillingWorker
).
to
receive
(
:try_obtain_lease_for
).
and_return
(
false
)
end
expect
(
CheckGcpProjectBillingService
).
to
receive_message_chain
(
:new
,
:execute
).
and_return
(
true
)
expect
(
Gitlab
::
Redis
::
SharedState
).
to
receive
(
:with
).
and_yield
(
redis_double
)
expect
(
redis_double
).
to
receive
(
:set
).
with
(
CheckGcpProjectBillingWorker
.
redis_shared_state_key_for
(
token
),
anything
)
it
'does not call the service'
do
expect
(
CheckGcpProjectBillingService
).
not_to
receive
(
:new
)
subject
subject
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment