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
945cdf32
Commit
945cdf32
authored
Jun 29, 2017
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make it possible to schedule bg migrations in bulk
parent
07693b43
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
70 additions
and
8 deletions
+70
-8
app/workers/background_migration_worker.rb
app/workers/background_migration_worker.rb
+16
-2
doc/development/background_migrations.md
doc/development/background_migrations.md
+15
-4
spec/support/sidekiq.rb
spec/support/sidekiq.rb
+7
-1
spec/workers/background_migration_worker_spec.rb
spec/workers/background_migration_worker_spec.rb
+32
-1
No files found.
app/workers/background_migration_worker.rb
View file @
945cdf32
...
...
@@ -2,18 +2,32 @@ class BackgroundMigrationWorker
include
Sidekiq
::
Worker
include
DedicatedSidekiqQueue
#
Schedules a number of jobs in bulk
#
Enqueues a number of jobs in bulk.
#
# The `jobs` argument should be an Array of Arrays, each sub-array must be in
# the form:
#
# [migration-class, [arg1, arg2, ...]]
def
self
.
perform_bulk
(
*
jobs
)
def
self
.
perform_bulk
(
jobs
)
Sidekiq
::
Client
.
push_bulk
(
'class'
=>
self
,
'queue'
=>
sidekiq_options
[
'queue'
],
'args'
=>
jobs
)
end
# Schedules a number of jobs in bulk, with a delay.
#
def
self
.
perform_bulk_in
(
delay
,
jobs
)
now
=
Time
.
now
.
to_f
schedule
=
now
+
delay
.
to_f
raise
ArgumentError
if
schedule
<=
now
Sidekiq
::
Client
.
push_bulk
(
'class'
=>
self
,
'queue'
=>
sidekiq_options
[
'queue'
],
'args'
=>
jobs
,
'at'
=>
schedule
)
end
# Performs the background migration.
#
# See Gitlab::BackgroundMigration.perform for more information.
...
...
doc/development/background_migrations.md
View file @
945cdf32
...
...
@@ -50,14 +50,14 @@ your migration:
BackgroundMigrationWorker
.
perform_async
(
'BackgroundMigrationClassName'
,
[
arg1
,
arg2
,
...
])
```
Usually it's better to
schedul
e jobs in bulk, for this you can use
Usually it's better to
enqueu
e jobs in bulk, for this you can use
`BackgroundMigrationWorker.perform_bulk`
:
```
ruby
BackgroundMigrationWorker
.
perform_bulk
(
[
'BackgroundMigrationClassName'
,
[
1
]],
[
'BackgroundMigrationClassName'
,
[
2
]],
...
[
[
'BackgroundMigrationClassName'
,
[
1
]],
[
'BackgroundMigrationClassName'
,
[
2
]],
...
]
)
```
...
...
@@ -68,6 +68,17 @@ consuming migrations it's best to schedule a background job using an
updates. Removals in turn can be handled by simply defining foreign keys with
cascading deletes.
If you would like to schedule jobs in bulk with a delay, you can use
`BackgroundMigrationWorker.perform_bulk_in`
:
```
ruby
jobs
=
[[
'BackgroundMigrationClassName'
,
[
1
]],
[
'BackgroundMigrationClassName'
,
[
2
]],
...
]
BackgroundMigrationWorker
.
perform_bulk_in
(
5
.
minutes
,
jobs
)
```
## Cleaning Up
Because background migrations can take a long time you can't immediately clean
...
...
spec/support/sidekiq.rb
View file @
945cdf32
require
'sidekiq/testing
/inline
'
require
'sidekiq/testing'
Sidekiq
::
Testing
.
server_middleware
do
|
chain
|
chain
.
add
Gitlab
::
SidekiqStatus
::
ServerMiddleware
end
RSpec
.
configure
do
|
config
|
config
.
after
(
:each
,
:sidekiq
)
do
Sidekiq
::
Worker
.
clear_all
end
end
spec/workers/background_migration_worker_spec.rb
View file @
945cdf32
require
'spec_helper'
describe
BackgroundMigrationWorker
do
describe
BackgroundMigrationWorker
,
:sidekiq
do
describe
'.perform'
do
it
'performs a background migration'
do
expect
(
Gitlab
::
BackgroundMigration
)
...
...
@@ -10,4 +10,35 @@ describe BackgroundMigrationWorker do
described_class
.
new
.
perform
(
'Foo'
,
[
10
,
20
])
end
end
describe
'.perform_bulk'
do
it
'enqueues background migrations in bulk'
do
Sidekiq
::
Testing
.
fake!
do
described_class
.
perform_bulk
([[
'Foo'
,
[
1
]],
[
'Foo'
,
[
2
]]])
expect
(
described_class
.
jobs
.
count
).
to
eq
2
expect
(
described_class
.
jobs
).
to
all
(
include
(
'enqueued_at'
))
end
end
end
describe
'.perform_bulk_in'
do
context
'when delay is valid'
do
it
'correctly schedules background migrations'
do
Sidekiq
::
Testing
.
fake!
do
described_class
.
perform_bulk_in
(
1
.
minute
,
[[
'Foo'
,
[
1
]],
[
'Foo'
,
[
2
]]])
expect
(
described_class
.
jobs
.
count
).
to
eq
2
expect
(
described_class
.
jobs
).
to
all
(
include
(
'at'
))
end
end
end
context
'when delay is invalid'
do
it
'raises an ArgumentError exception'
do
expect
{
described_class
.
perform_bulk_in
(
-
60
,
[[
'Foo'
]])
}
.
to
raise_error
(
ArgumentError
)
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