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
0
Merge Requests
0
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
Boxiang Sun
gitlab-ce
Commits
a180306d
Commit
a180306d
authored
Jan 06, 2018
by
Matija Čupić
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use token from redis in gcp project billing worker
parent
2885dc06
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
15 deletions
+46
-15
app/workers/check_gcp_project_billing_worker.rb
app/workers/check_gcp_project_billing_worker.rb
+14
-1
spec/workers/check_gcp_project_billing_worker_spec.rb
spec/workers/check_gcp_project_billing_worker_spec.rb
+32
-14
No files found.
app/workers/check_gcp_project_billing_worker.rb
View file @
a180306d
require
'securerandom'
class
CheckGcpProjectBillingWorker
class
CheckGcpProjectBillingWorker
include
ApplicationWorker
include
ApplicationWorker
include
ClusterQueue
include
ClusterQueue
LEASE_TIMEOUT
=
15
.
seconds
.
to_i
LEASE_TIMEOUT
=
15
.
seconds
.
to_i
def
self
.
generate_redis_token_key
SecureRandom
.
uuid
end
def
self
.
redis_shared_state_key_for
(
token
)
def
self
.
redis_shared_state_key_for
(
token
)
"gitlab:gcp:
#{
token
.
hash
}
:billing_enabled"
"gitlab:gcp:
#{
token
.
hash
}
:billing_enabled"
end
end
def
perform
(
token
)
def
perform
(
token_key
)
return
unless
token_key
token
=
get_token
(
token_key
)
return
unless
token
return
unless
token
return
unless
try_obtain_lease_for
(
token
)
return
unless
try_obtain_lease_for
(
token
)
...
@@ -20,6 +29,10 @@ class CheckGcpProjectBillingWorker
...
@@ -20,6 +29,10 @@ class CheckGcpProjectBillingWorker
private
private
def
get_token
(
token_key
)
Gitlab
::
Redis
::
SharedState
.
with
{
|
redis
|
redis
.
get
(
token_key
)
}
end
def
try_obtain_lease_for
(
token
)
def
try_obtain_lease_for
(
token
)
Gitlab
::
ExclusiveLease
Gitlab
::
ExclusiveLease
.
new
(
"check_gcp_project_billing_worker:
#{
token
.
hash
}
"
,
timeout:
LEASE_TIMEOUT
)
.
new
(
"check_gcp_project_billing_worker:
#{
token
.
hash
}
"
,
timeout:
LEASE_TIMEOUT
)
...
...
spec/workers/check_gcp_project_billing_worker_spec.rb
View file @
a180306d
...
@@ -3,7 +3,12 @@ require 'spec_helper'
...
@@ -3,7 +3,12 @@ require 'spec_helper'
describe
CheckGcpProjectBillingWorker
do
describe
CheckGcpProjectBillingWorker
do
describe
'.perform'
do
describe
'.perform'
do
let
(
:token
)
{
'bogustoken'
}
let
(
:token
)
{
'bogustoken'
}
subject
{
described_class
.
new
.
perform
(
token
)
}
subject
{
described_class
.
new
.
perform
(
'token_key'
)
}
context
'when there is a token in redis'
do
before
do
allow_any_instance_of
(
described_class
).
to
receive
(
:get_token
).
and_return
(
token
)
end
context
'when there is no lease'
do
context
'when there is no lease'
do
before
do
before
do
...
@@ -39,4 +44,17 @@ describe CheckGcpProjectBillingWorker do
...
@@ -39,4 +44,17 @@ describe CheckGcpProjectBillingWorker do
end
end
end
end
end
end
context
'when there is no token in redis'
do
before
do
allow_any_instance_of
(
described_class
).
to
receive
(
:get_token
).
and_return
(
nil
)
end
it
'does not call the service'
do
expect
(
CheckGcpProjectBillingService
).
not_to
receive
(
:new
)
subject
end
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