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
Jérome Perrin
gitlab-ce
Commits
9573bb44
Commit
9573bb44
authored
Mar 31, 2017
by
Shinya Maeda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
real_next_run (WIP)
parent
d48658e3
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
102 additions
and
66 deletions
+102
-66
app/models/ci/trigger_schedule.rb
app/models/ci/trigger_schedule.rb
+17
-2
lib/ci/cron_parser.rb
lib/ci/cron_parser.rb
+5
-4
spec/factories/ci/trigger_schedules.rb
spec/factories/ci/trigger_schedules.rb
+5
-37
spec/factories/ci/triggers.rb
spec/factories/ci/triggers.rb
+4
-0
spec/models/ci/trigger_schedule_spec.rb
spec/models/ci/trigger_schedule_spec.rb
+71
-23
No files found.
app/models/ci/trigger_schedule.rb
View file @
9573bb44
...
...
@@ -18,12 +18,27 @@ module Ci
after_create
:schedule_next_run!
def
schedule_next_run!
puts
"cron:
#{
cron
.
inspect
}
| cron_time_zone:
#{
cron_time_zone
.
inspect
}
"
next_time
=
Ci
::
CronParser
.
new
(
cron
,
cron_time_zone
).
next_time_from_now
if
next_time
.
present?
update!
(
next_run_at:
next_time
)
end
end
def
real_next_run
(
worker_cron:
nil
,
worker_time_zone:
nil
)
puts
"worker_cron:
#{
worker_cron
.
inspect
}
| worker_time_zone:
#{
worker_time_zone
.
inspect
}
"
worker_cron
=
Settings
.
cron_jobs
[
'trigger_schedule_worker'
][
'cron'
]
unless
worker_cron
.
present?
worker_time_zone
=
Time
.
zone
.
name
unless
worker_time_zone
.
present?
worker_next_time
=
Ci
::
CronParser
.
new
(
worker_cron
,
worker_time_zone
).
next_time_from_now
puts
"next_run_at:
#{
next_run_at
.
inspect
}
| worker_next_time:
#{
worker_next_time
.
inspect
}
"
if
next_run_at
>
worker_next_time
next_run_at
else
worker_next_time
end
end
private
def
check_cron
...
...
@@ -40,9 +55,9 @@ module Ci
end
def
check_ref
if
!
trigger
.
ref
.
present?
if
!
ref
.
present?
self
.
errors
.
add
(
:ref
,
" is empty"
)
elsif
trigger
.
project
.
repository
.
ref_exists?
(
trigger
.
ref
)
elsif
project
.
repository
.
ref_exists?
(
ref
)
self
.
errors
.
add
(
:ref
,
" does not exist"
)
end
end
...
...
lib/ci/cron_parser.rb
View file @
9573bb44
module
Ci
class
CronParser
VALID_SYNTAX_SAMPLE_TIME_ZONE
=
'UTC'
VALID_SYNTAX_SAMPLE_CRON
=
'* * * * *'
def
initialize
(
cron
,
cron_time_zone
=
'UTC'
)
@cron
=
cron
@cron_time_zone
=
cron_time_zone
...
...
@@ -12,10 +15,8 @@ module Ci
end
def
validation
VALID_SYNTAX_TIME_ZONE
=
'Europe/Istanbul'
VALID_SYNTAX_CRON
=
'* * * * *'
is_valid_cron
=
try_parse_cron
(
@cron
,
VALID_SYNTAX_TIME_ZONE
).
present?
is_valid_cron_time_zone
=
try_parse_cron
(
VALID_SYNTAX_CRON
,
@cron_time_zone
).
present?
is_valid_cron
=
try_parse_cron
(
@cron
,
VALID_SYNTAX_SAMPLE_TIME_ZONE
).
present?
is_valid_cron_time_zone
=
try_parse_cron
(
VALID_SYNTAX_SAMPLE_CRON
,
@cron_time_zone
).
present?
return
is_valid_cron
,
is_valid_cron_time_zone
end
...
...
spec/factories/ci/trigger_schedules.rb
View file @
9573bb44
FactoryGirl
.
define
do
factory
:ci_trigger_schedule
,
class:
Ci
::
TriggerSchedule
do
project
factory: :project
trigger
factory: :ci_trigger
trigger
factory: :ci_trigger
_with_ref
trait
:force_triggable
do
after
(
:create
)
do
|
trigger_schedule
,
evaluator
|
...
...
@@ -11,49 +11,17 @@ FactoryGirl.define do
trait
:cron_nightly_build
do
cron
'0 1 * * *'
cron_time_zone
'Europe/Istanbul'
# next_run_at do # TODO: Use CronParser
# time = Time.now.in_time_zone(cron_time_zone)
# time = time + 1.day if time.hour > 1
# time = time.change(sec: 0, min: 0, hour: 1)
# time
# end
cron_time_zone
Ci
::
CronParser
::
VALID_SYNTAX_SAMPLE_TIME_ZONE
end
trait
:cron_weekly_build
do
cron
'0 1 * * 5'
cron_time_zone
'Europe/Istanbul'
# TODO: next_run_at
cron
'0 1 * * 6'
cron_time_zone
Ci
::
CronParser
::
VALID_SYNTAX_SAMPLE_TIME_ZONE
end
trait
:cron_monthly_build
do
cron
'0 1 22 * *'
cron_time_zone
'Europe/Istanbul'
# TODO: next_run_at
end
trait
:cron_every_5_minutes
do
cron
'*/5 * * * *'
cron_time_zone
'Europe/Istanbul'
# TODO: next_run_at
end
trait
:cron_every_5_hours
do
cron
'* */5 * * *'
cron_time_zone
'Europe/Istanbul'
# TODO: next_run_at
end
trait
:cron_every_5_days
do
cron
'* * */5 * *'
cron_time_zone
'Europe/Istanbul'
# TODO: next_run_at
end
trait
:cron_every_5_months
do
cron
'* * * */5 *'
cron_time_zone
'Europe/Istanbul'
# TODO: next_run_at
cron_time_zone
Ci
::
CronParser
::
VALID_SYNTAX_SAMPLE_TIME_ZONE
end
end
end
spec/factories/ci/triggers.rb
View file @
9573bb44
...
...
@@ -2,6 +2,10 @@ FactoryGirl.define do
factory
:ci_trigger_without_token
,
class:
Ci
::
Trigger
do
factory
:ci_trigger
do
token
{
SecureRandom
.
hex
(
15
)
}
factory
:ci_trigger_with_ref
do
ref
'master'
end
end
end
end
spec/models/ci/trigger_schedule_spec.rb
View file @
9573bb44
...
...
@@ -5,37 +5,85 @@ describe Ci::TriggerSchedule, models: true do
let
(
:project
)
{
create
(
:project
)
}
let
(
:trigger
)
{
create
(
:ci_trigger
,
owner:
user
,
project:
project
,
ref:
'master'
)
}
describe
'associations'
do
it
{
is_expected
.
to
belong_to
(
:project
)
}
it
{
is_expected
.
to
belong_to
(
:trigger
)
}
# it { is_expected.to validate_presence_of :cron }
# it { is_expected.to validate_presence_of :cron_time_zone }
it
{
is_expected
.
to
respond_to
:ref
}
# describe '#schedule_next_run!' do
# let(:trigger_schedule) { create(:ci_trigger_schedule, :cron_nightly_build, next_run_at: nil, trigger: trigger) }
# before do
# trigger_schedule.schedule_next_run!
# end
# it 'updates next_run_at' do
# expect(Ci::TriggerSchedule.last.next_run_at).not_to be_nil
# end
# end
describe
'#real_next_run'
do
subject
{
trigger_schedule
.
real_next_run
(
worker_cron:
worker_cron
,
worker_time_zone:
worker_time_zone
)
}
context
'when next_run_at > worker_next_time'
do
let
(
:worker_cron
)
{
'0 */12 * * *'
}
# each 00:00, 12:00
let
(
:worker_time_zone
)
{
'UTC'
}
let
(
:trigger_schedule
)
{
create
(
:ci_trigger_schedule
,
:cron_weekly_build
,
cron_time_zone:
user_time_zone
,
trigger:
trigger
)
}
context
'when user is in Europe/London(+00:00)'
do
let
(
:user_time_zone
)
{
'Europe/London'
}
it
'returns next_run_at'
do
is_expected
.
to
eq
(
trigger_schedule
.
next_run_at
)
end
end
describe
'validation
'
do
let
(
:trigger_schedule
)
{
create
(
:ci_trigger_schedule
,
:cron_nightly_build
,
trigger:
trigger
)
}
context
'when user is in Asia/Hong_Kong(+08:00)
'
do
let
(
:user_time_zone
)
{
'Asia/Hong_Kong'
}
it
{
expect
(
trigger_schedule
).
to
validate_presence_of
(
:trigger
)
}
it
{
is_expected
.
to
validate_presence_of
(
:cron
)
}
it
{
is_expected
.
to
validate_presence_of
(
:cron_time_zone
)
}
it
'returns next_run_at'
do
is_expected
.
to
eq
(
trigger_schedule
.
next_run_at
)
end
end
it
'#check_cron'
do
subject
.
cron
=
'Hack'
subject
.
valid?
subject
.
errors
[
:screen_name
].
to
include
(
' is invalid syntax'
)
context
'when user is in Canada/Pacific(-08:00)'
do
let
(
:user_time_zone
)
{
'Canada/Pacific'
}
it
'returns next_run_at'
do
is_expected
.
to
eq
(
trigger_schedule
.
next_run_at
)
end
end
end
it
'#check_ref'
do
context
'when worker_next_time > next_run_at'
do
let
(
:worker_cron
)
{
'0 0 */2 * *'
}
# every 2 days
let
(
:worker_time_zone
)
{
'UTC'
}
let
(
:trigger_schedule
)
{
create
(
:ci_trigger_schedule
,
:cron_nightly_build
,
cron_time_zone:
user_time_zone
,
trigger:
trigger
)
}
context
'when user is in Europe/London(+00:00)'
do
let
(
:user_time_zone
)
{
'Europe/London'
}
it
'returns worker_next_time'
do
is_expected
.
to
eq
(
Ci
::
CronParser
.
new
(
worker_cron
,
worker_time_zone
).
next_time_from_now
)
end
end
describe
'#schedule_next_run!
'
do
let
(
:trigger_schedule
)
{
create
(
:ci_trigger_schedule
,
:cron_nightly_build
,
next_run_at:
nil
,
trigger:
trigger
)
}
context
'when user is in Asia/Hong_Kong(+08:00)
'
do
let
(
:user_time_zone
)
{
'Asia/Hong_Kong'
}
before
do
trigger_schedule
.
schedule_next_run!
it
'returns worker_next_time'
do
is_expected
.
to
eq
(
Ci
::
CronParser
.
new
(
worker_cron
,
worker_time_zone
).
next_time_from_now
)
end
end
context
'when user is in Canada/Pacific(-08:00)'
do
let
(
:user_time_zone
)
{
'Canada/Pacific'
}
it
'updates next_run_at'
do
expect
(
Ci
::
TriggerSchedule
.
last
.
next_run_at
).
not_to
be_nil
it
'returns worker_next_time'
do
is_expected
.
to
eq
(
Ci
::
CronParser
.
new
(
worker_cron
,
worker_time_zone
).
next_time_from_now
)
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