From 8a6f4066ae45d268a85caff0cd3c93da030c2c23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20Ko=CC=88tte?= Date: Mon, 1 Feb 2016 17:08:11 +0100 Subject: [PATCH] refactor methods to calculate next build time --- lib/travis/api/v3/models/cron.rb | 60 ++++++-------------------------- 1 file changed, 11 insertions(+), 49 deletions(-) diff --git a/lib/travis/api/v3/models/cron.rb b/lib/travis/api/v3/models/cron.rb index 1300cbfa..7441e80c 100644 --- a/lib/travis/api/v3/models/cron.rb +++ b/lib/travis/api/v3/models/cron.rb @@ -5,80 +5,42 @@ module Travis::API::V3 def next_enqueuing - if (disable_by_build) && (last_non_cron_build_date > last_planned_time) - return after_next_planned_time - elsif last_cron_build_date >= last_planned_time - return next_planned_time + if (disable_by_build) && (last_non_cron_build_date > plannedTime(-1)) + return plannedTime(1) + elsif last_cron_build_date >= plannedTime(-1) + return plannedTime(0) else return Time.now end end - def next_planned_time + def plannedTime(in_builds = 0) # 0 equals next build, 1 after next build, -1 last build, ... now = DateTime.now created = DateTime.new(created_at.year, created_at.month, created_at.day, created_at.hour) case interval when 'daily' build_today = DateTime.new(now.year, now.month, now.day, created_at.hour) if now > build_today - return build_today + 1 + return build_today + 1 + in_builds else - return build_today + return build_today + in_builds end when 'weekly' build_today = DateTime.new(now.year, now.month, now.day, created_at.hour) in_days = (created_at.wday - now.wday) % 7 next_time = build_today + in_days if now > next_time - return build_today + 7 + return build_today + 7 * (1 + in_builds) else - return next_time + return next_time + 7 * in_builds end when 'monthly' month_since_creation = (now.year*12+now.month) - (created_at.year*12+created_at.month) this_month = created >> month_since_creation if now > this_month - return created >> (month_since_creation+1) + return created >> (month_since_creation + 1 + in_builds) else - return this_month - end - end - end - - def last_planned_time - now = DateTime.now - created = DateTime.new(created_at.year, created_at.month, created_at.day, created_at.hour) - case interval - when 'daily' - return next_planned_time - 1 - when 'weekly' - return next_planned_time - 7 - when 'monthly' - month_since_creation = (now.year*12+now.month) - (created_at.year*12+created_at.month) - this_month = created >> month_since_creation - if now > this_month - return this_month - else - return created >> (month_since_creation-1) - end - end - end - - def after_next_planned_time - now = DateTime.now - created = DateTime.new(created_at.year, created_at.month, created_at.day, created_at.hour) - case interval - when 'daily' - return next_planned_time + 1 - when 'weekly' - return next_planned_time + 7 - when 'monthly' - month_since_creation = (now.year*12+now.month) - (created_at.year*12+created_at.month) - this_month = created >> month_since_creation - if now > this_month - return created >> (month_since_creation+2) - else - return created >> (month_since_creation+1) + return created >> (month_since_creation + in_builds) end end end