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
45f52235
Commit
45f52235
authored
May 02, 2018
by
Matija Čupić
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove ListGcpProjectsWorker
parent
7a1ce56f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
0 additions
and
208 deletions
+0
-208
app/workers/list_gcp_projects_worker.rb
app/workers/list_gcp_projects_worker.rb
+0
-92
spec/workers/list_gcp_projects_worker_spec.rb
spec/workers/list_gcp_projects_worker_spec.rb
+0
-116
No files found.
app/workers/list_gcp_projects_worker.rb
deleted
100644 → 0
View file @
7a1ce56f
require
'securerandom'
class
ListGcpProjectsWorker
include
ApplicationWorker
include
ClusterQueue
LEASE_TIMEOUT
=
3
.
seconds
.
to_i
SESSION_KEY_TIMEOUT
=
5
.
minutes
PROJECT_TIMEOUT
=
1
.
hour
BILLING_CHANGED_LABELS
=
{
state_transition:
nil
}.
freeze
def
self
.
get_session_token
(
token_key
)
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
redis
.
get
(
get_redis_session_key
(
token_key
))
end
end
def
self
.
store_session_token
(
token
)
generate_token_key
.
tap
do
|
token_key
|
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
redis
.
set
(
get_redis_session_key
(
token_key
),
token
,
ex:
SESSION_KEY_TIMEOUT
)
end
end
end
def
self
.
read_projects
(
token
)
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
value
=
redis
.
get
(
redis_shared_state_key_for
(
token
))
value
?
JSON
.
parse
(
value
)
:
[]
end
end
def
perform
(
token_key
)
return
unless
token_key
token
=
self
.
class
.
get_session_token
(
token_key
)
return
unless
token
return
unless
try_obtain_lease_for
(
token
)
billing_enabled_projects
=
ListGcpProjectsService
.
new
.
execute
(
token
)
update_billing_change_counter
(
!
self
.
class
.
read_projects
(
token
).
empty?
,
!
billing_enabled_projects
.
empty?
)
self
.
class
.
store_projects
(
token
,
billing_enabled_projects
.
to_json
)
end
private
def
self
.
generate_token_key
SecureRandom
.
uuid
end
def
self
.
get_redis_session_key
(
token_key
)
"gitlab:gcp:session:
#{
token_key
}
"
end
def
self
.
redis_shared_state_key_for
(
token
)
"gitlab:gcp:
#{
Digest
::
SHA1
.
hexdigest
(
token
)
}
:projects"
end
def
self
.
store_projects
(
token
,
value
)
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
redis
.
set
(
redis_shared_state_key_for
(
token
),
value
,
ex:
PROJECT_TIMEOUT
)
end
end
def
try_obtain_lease_for
(
token
)
Gitlab
::
ExclusiveLease
.
new
(
"list_gcp_projects_worker:
#{
token
.
hash
}
"
,
timeout:
LEASE_TIMEOUT
)
.
try_obtain
end
def
billing_changed_counter
@billing_changed_counter
||=
Gitlab
::
Metrics
.
counter
(
:gcp_billing_change_count
,
"Counts the number of times a GCP project changed billing_enabled state from false to true"
,
BILLING_CHANGED_LABELS
)
end
def
state_transition
(
previous_state
,
current_state
)
if
previous_state
.
nil?
&&
!
current_state
'no_billing'
elsif
previous_state
.
nil?
&&
current_state
'with_billing'
elsif
!
previous_state
&&
current_state
'billing_configured'
end
end
def
update_billing_change_counter
(
previous_state
,
current_state
)
billing_changed_counter
.
increment
(
state_transition:
state_transition
(
previous_state
,
current_state
))
end
end
spec/workers/list_gcp_projects_worker_spec.rb
deleted
100644 → 0
View file @
7a1ce56f
require
'spec_helper'
describe
ListGcpProjectsWorker
do
describe
'.perform'
do
let
(
:token
)
{
'bogustoken'
}
subject
{
described_class
.
new
.
perform
(
'token_key'
)
}
before
do
allow
(
described_class
).
to
receive
(
:read_projects
).
and_return
([
double
])
allow_any_instance_of
(
described_class
).
to
receive
(
:update_billing_change_counter
)
end
context
'when there is a token in redis'
do
before
do
allow
(
described_class
).
to
receive
(
:get_session_token
).
and_return
(
token
)
end
context
'when there is no lease'
do
before
do
allow_any_instance_of
(
described_class
).
to
receive
(
:try_obtain_lease_for
).
and_return
(
'randomuuid'
)
end
it
'calls the service'
do
expect
(
ListGcpProjectsService
).
to
receive_message_chain
(
:new
,
:execute
).
and_return
([
double
])
subject
end
it
'stores projects in redis'
do
expect
(
ListGcpProjectsService
).
to
receive_message_chain
(
:new
,
:execute
).
and_return
([
double
])
expect
(
described_class
).
to
receive
(
:store_projects
).
with
(
token
,
anything
)
subject
end
end
context
'when there is a lease'
do
before
do
allow_any_instance_of
(
described_class
).
to
receive
(
:try_obtain_lease_for
).
and_return
(
false
)
end
it
'does not call the service'
do
expect
(
ListGcpProjectsService
).
not_to
receive
(
:new
)
subject
end
end
end
context
'when there is no token in redis'
do
before
do
allow
(
described_class
).
to
receive
(
:get_session_token
).
and_return
(
nil
)
end
it
'does not call the service'
do
expect
(
ListGcpProjectsService
).
not_to
receive
(
:new
)
subject
end
end
end
describe
'billing change counter'
do
subject
{
described_class
.
new
.
perform
(
'token_key'
)
}
before
do
allow
(
described_class
).
to
receive
(
:get_session_token
).
and_return
(
'bogustoken'
)
allow_any_instance_of
(
described_class
).
to
receive
(
:try_obtain_lease_for
).
and_return
(
'randomuuid'
)
allow
(
described_class
).
to
receive
(
:store_projects
)
end
context
'when there are no projects in redis'
do
before
do
expect
(
described_class
).
to
receive
(
:read_projects
).
and_return
([])
end
context
'when the service does not return projects'
do
before
do
expect
(
ListGcpProjectsService
).
to
receive_message_chain
(
:new
,
:execute
).
and_return
([])
end
it
'increments the billing change counter'
do
expect_any_instance_of
(
described_class
).
to
receive_message_chain
(
:billing_changed_counter
,
:increment
)
subject
end
end
context
'when the service returns projects'
do
before
do
expect
(
ListGcpProjectsService
).
to
receive_message_chain
(
:new
,
:execute
).
and_return
([
double
])
end
it
'increments the billing change counter'
do
expect_any_instance_of
(
described_class
).
to
receive_message_chain
(
:billing_changed_counter
,
:increment
)
subject
end
end
end
context
'when there are projects in redis'
do
before
do
expect
(
described_class
).
to
receive
(
:read_projects
).
and_return
([
double
])
expect
(
ListGcpProjectsService
).
to
receive_message_chain
(
:new
,
:execute
).
and_return
([
double
])
end
it
'increments the billing change counter'
do
expect_any_instance_of
(
described_class
).
to
receive_message_chain
(
:billing_changed_counter
,
:increment
)
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