refactor cron model and create service
This commit is contained in:
parent
9734e7133f
commit
31f6b5793a
|
@ -3,61 +3,72 @@ module Travis::API::V3
|
||||||
|
|
||||||
belongs_to :branch
|
belongs_to :branch
|
||||||
|
|
||||||
|
LastBuild = -1
|
||||||
|
ThisBuild = 0
|
||||||
|
NextBuild = 1
|
||||||
|
|
||||||
def next_enqueuing
|
def next_enqueuing
|
||||||
if (disable_by_build) && (last_non_cron_build_date > planned_time(-1))
|
if disable_by_build && last_non_cron_build_date > planned_time(LastBuild)
|
||||||
return planned_time(1)
|
planned_time(NextBuild)
|
||||||
elsif last_cron_build_date >= planned_time(-1)
|
elsif last_cron_build_date >= planned_time(LastBuild)
|
||||||
return planned_time(0)
|
planned_time(ThisBuild)
|
||||||
else
|
else
|
||||||
return Time.now
|
Time.now
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def planned_time(in_builds = 0) # 0 equals next build, 1 after next build, -1 last build, ...
|
def planned_time(in_builds = ThisBuild)
|
||||||
case interval
|
case interval
|
||||||
when 'daily'
|
when 'daily'
|
||||||
return planned_time_daily(in_builds)
|
planned_time_daily(in_builds)
|
||||||
when 'weekly'
|
when 'weekly'
|
||||||
return planned_time_weekly(in_builds)
|
planned_time_weekly(in_builds)
|
||||||
when 'monthly'
|
when 'monthly'
|
||||||
return planned_time_monthly(in_builds)
|
planned_time_monthly(in_builds)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def planned_time_daily(in_builds = 0)
|
def planned_time_daily(in_builds)
|
||||||
now = DateTime.now
|
now = DateTime.now
|
||||||
build_today = DateTime.new(now.year, now.month, now.day, created_at.hour)
|
build_today = DateTime.new(now.year, now.month, now.day, created_at.hour)
|
||||||
return build_today + 1 + in_builds if (now > build_today)
|
return build_today + 1 + in_builds if (now > build_today)
|
||||||
return build_today + in_builds
|
build_today + in_builds
|
||||||
end
|
end
|
||||||
|
|
||||||
def planned_time_weekly(in_builds = 0)
|
def planned_time_weekly(in_builds)
|
||||||
now = DateTime.now
|
now = DateTime.now
|
||||||
build_today = DateTime.new(now.year, now.month, now.day, created_at.hour)
|
build_today = DateTime.new(now.year, now.month, now.day, created_at.hour)
|
||||||
next_time = build_today + ((created_at.wday - now.wday) % 7)
|
next_time = build_today + ((created_at.wday - now.wday) % 7)
|
||||||
return build_today + 7 * (1 + in_builds) if (now > next_time)
|
return build_today + 7 * (1 + in_builds) if (now > next_time)
|
||||||
return next_time + 7 * in_builds
|
next_time + 7 * in_builds
|
||||||
end
|
end
|
||||||
|
|
||||||
def planned_time_monthly(in_builds = 0)
|
def planned_time_monthly(in_builds)
|
||||||
now = DateTime.now
|
now = DateTime.now
|
||||||
created = DateTime.new(created_at.year, created_at.month, created_at.day, created_at.hour)
|
created = DateTime.new(created_at.year, created_at.month, created_at.day, created_at.hour)
|
||||||
month_since_creation = (now.year*12+now.month) - (created_at.year*12+created_at.month)
|
month_since_creation = (now.year * 12 + now.month) - (created_at.year * 12 + created_at.month)
|
||||||
this_month = created >> month_since_creation
|
this_month = created >> month_since_creation
|
||||||
return created >> (month_since_creation + 1 + in_builds) if (now > this_month)
|
return created >> (month_since_creation + 1 + in_builds) if (now > this_month)
|
||||||
return created >> (month_since_creation + in_builds)
|
created >> (month_since_creation + in_builds)
|
||||||
end
|
end
|
||||||
|
|
||||||
def last_cron_build_date
|
def last_cron_build_date
|
||||||
last_cron_build = Models::Build.where(:repository_id => branch.repository.id, :branch => branch.name, :event_type => 'cron').order("id DESC").first
|
last_cron_build = Models::Build.where(
|
||||||
|
:repository_id => branch.repository.id,
|
||||||
|
:branch => branch.name,
|
||||||
|
:event_type => 'cron'
|
||||||
|
).order("id DESC").first
|
||||||
return last_cron_build.created_at unless last_cron_build.nil?
|
return last_cron_build.created_at unless last_cron_build.nil?
|
||||||
return Time.at(0)
|
Time.at(0)
|
||||||
end
|
end
|
||||||
|
|
||||||
def last_non_cron_build_date
|
def last_non_cron_build_date
|
||||||
last_build = Models::Build.where(:repository_id => branch.repository.id, :branch => branch.name).where(['event_type NOT IN (?)', ['cron']]).order("id DESC").first
|
last_build = Models::Build.where(
|
||||||
|
:repository_id => branch.repository.id,
|
||||||
|
:branch => branch.name
|
||||||
|
).where(['event_type NOT IN (?)', ['cron']]).order("id DESC").first
|
||||||
return last_build.created_at unless last_build.nil?
|
return last_build.created_at unless last_build.nil?
|
||||||
return Time.at(0)
|
Time.at(0)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -9,13 +9,8 @@ module Travis::API::V3
|
||||||
raise NotFound unless branch = find(:branch, repository)
|
raise NotFound unless branch = find(:branch, repository)
|
||||||
raise Error.new('Invalid value for interval. Interval must be "daily", "weekly" or "monthly"!', status: 422) unless ["daily", "weekly", "monthly"].include?(params["interval"])
|
raise Error.new('Invalid value for interval. Interval must be "daily", "weekly" or "monthly"!', status: 422) unless ["daily", "weekly", "monthly"].include?(params["interval"])
|
||||||
access_control.permissions(repository).create_cron!
|
access_control.permissions(repository).create_cron!
|
||||||
|
access_control.permissions(branch.cron).delete! if branch.cron
|
||||||
if branch.cron
|
|
||||||
access_control.permissions(branch.cron).delete!
|
|
||||||
end
|
|
||||||
|
|
||||||
query.create(branch, params["interval"], params["disable_by_build"] ? params["disable_by_build"] : false)
|
query.create(branch, params["interval"], params["disable_by_build"] ? params["disable_by_build"] : false)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user