Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 9 additions & 21 deletions lib/fbe/conclude.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require_relative 'fb'
require_relative 'if_absent'
require_relative 'octo'
require_relative 'over'

# Creates an instance of {Fbe::Conclude} and evals it with the block provided.
#
Expand Down Expand Up @@ -195,29 +196,16 @@ def consider(&)
# end
# end
def roll(&)
if @lifetime_aware && @options.lifetime && Time.now - @epoch > @options.lifetime * 0.9
@loog.debug("We ran out of lifetime (#{@epoch.ago} already), can't even start")
return
end
if @timeout_aware && @options.timeout && Time.now - @kickoff > @options.timeout * 0.9
@loog.debug("We've spent more than #{@kickoff.ago}, won't even start")
return
end
return 0 if Fbe.over?(
global: @global, options: @options, loog: @loog, epoch: @epoch, kickoff: @kickoff,
quota_aware: @quota_aware, lifetime_aware: @lifetime_aware, timeout_aware: @timeout_aware
)
passed = 0
oct = Fbe.octo(loog: @loog, options: @options, global: @global)
@fb.query(@query).each do |a|
if @quota_aware && oct.off_quota?(threshold: 100)
@loog.info('We ran out of GitHub quota, must stop here')
break
end
if @lifetime_aware && @options.lifetime && Time.now - @epoch > @options.lifetime * 0.9
@loog.debug("We ran out of lifetime (#{@epoch.ago} already), must stop here")
break
end
if @timeout_aware && @options.timeout && Time.now - @kickoff > @options.timeout * 0.9
@loog.debug("We've spent more than #{@kickoff.ago}, must stop here")
break
end
break if Fbe.over?(
global: @global, options: @options, loog: @loog, epoch: @epoch, kickoff: @kickoff,
quota_aware: @quota_aware, lifetime_aware: @lifetime_aware, timeout_aware: @timeout_aware
)
@fb.txn do |fbt|
n = yield fbt, a
@loog.info("#{n.what}: #{n.details}") unless n.nil?
Expand Down
31 changes: 11 additions & 20 deletions lib/fbe/iterate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require_relative 'fb'
require_relative 'if_absent'
require_relative 'octo'
require_relative 'over'
require_relative 'overwrite'
require_relative 'unmask_repos'

Expand Down Expand Up @@ -267,29 +268,19 @@ def over
starts = before.dup
values = {}
loop do
if @quota_aware && oct.off_quota?(threshold: 100)
@loog.info("We are off GitHub quota, time to stop after #{started.ago}")
break
end
if @lifetime_aware && @options.lifetime && Time.now - @epoch > @options.lifetime * 0.9
@loog.debug("We ran out of lifetime (#{@epoch.ago} already), must stop here")
break
end
if @timeout_aware && @options.timeout && Time.now - @kickoff > @options.timeout * 0.9
@loog.debug("We've spent more than #{@kickoff.ago}, must stop here")
if Fbe.over?(
global: @global, options: @options, loog: @loog, epoch: @epoch, kickoff: @kickoff,
quota_aware: @quota_aware, lifetime_aware: @lifetime_aware, timeout_aware: @timeout_aware
)
@loog.info("Time to stop after #{started.ago}")
break
end
repos.each do |repo|
if @quota_aware && oct.off_quota?(threshold: 100)
@loog.info("We are off GitHub quota, we must skip #{repo}")
break
end
if @lifetime_aware && @options.lifetime && Time.now - @epoch > @options.lifetime * 0.9
@loog.info("We are working for #{@epoch.ago} already, won't check repository ##{repo}")
next
end
if @timeout_aware && @options.timeout && Time.now - @kickoff > @options.timeout * 0.9
@loog.debug("We've spent more than #{@kickoff.ago}, won't check repository ##{repo}")
if Fbe.over?(
global: @global, options: @options, loog: @loog, epoch: @epoch, kickoff: @kickoff,
quota_aware: @quota_aware, lifetime_aware: @lifetime_aware, timeout_aware: @timeout_aware
)
@loog.info("Won't check repository ##{repo}")
break
end
next if restarted.include?(repo)
Expand Down
38 changes: 38 additions & 0 deletions lib/fbe/over.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

# SPDX-FileCopyrightText: Copyright (c) 2024-2025 Zerocracy
# SPDX-License-Identifier: MIT

require_relative '../fbe'
require_relative 'octo'

# Check GitHub API quota, lifetime, and timeout.
#
# @param [Hash] global Hash of global options
# @param [Judges::Options] options The options available globally
# @param [Loog] loog Logging facility
# @param [Time] epoch When the entire update started
# @param [Time] kickoff When the particular judge started
# @param [Boolean] quota_aware Enable or disable check of GitHub API quota
# @param [Boolean] lifetime_aware Enable or disable check of lifetime limitations
# @param [Boolean] timeout_aware Enable or disable check of timeout limitations
# @return [Boolean] check result
def Fbe.over?(
global: $global, options: $options, loog: $loog,
epoch: $epoch || Time.now, kickoff: $kickoff || Time.now,
quota_aware: true, lifetime_aware: true, timeout_aware: true
)
if quota_aware && Fbe.octo(loog:, options:, global:).off_quota?(threshold: 100)
loog.info('We are off GitHub quota, time to stop')
return true
end
if lifetime_aware && options.lifetime && Time.now - epoch > options.lifetime * 0.9
loog.info("We ran out of lifetime (#{epoch.ago} already), must stop here")
return true
end
if timeout_aware && options.timeout && Time.now - kickoff > options.timeout * 0.9
loog.info("We've spent more than #{kickoff.ago}, must stop here")
return true
end
false
end
63 changes: 63 additions & 0 deletions test/fbe/test_over.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# frozen_string_literal: true

# SPDX-FileCopyrightText: Copyright (c) 2024-2025 Zerocracy
# SPDX-License-Identifier: MIT

require 'judges/options'
require 'loog'
require_relative '../../lib/fbe/octo'
require_relative '../../lib/fbe/over'
require_relative '../test__helper'

# Test.
class TestOver < Fbe::Test
def test_simple
refute(Fbe.over?(global: {}, options: Judges::Options.new({ 'testing' => true }), loog: Loog::NULL))
end

def test_check_off_quota_enabled
global = {}
options = Judges::Options.new({ 'testing' => true })
loog = Loog::NULL
Fbe.octo(loog:, options:, global:).stub(:off_quota?, true) do
assert(Fbe.over?(global:, options:, loog:, quota_aware: true))
end
end

def test_check_off_quota_disabled
global = {}
options = Judges::Options.new({ 'testing' => true })
loog = Loog::NULL
Fbe.octo(loog:, options:, global:).stub(:off_quota?, true) do
refute(Fbe.over?(global:, options:, loog:, quota_aware: false))
end
end

def test_check_lifetime_enabled
global = {}
options = Judges::Options.new({ 'testing' => true, 'lifetime' => 100 })
loog = Loog::NULL
assert(Fbe.over?(global:, options:, loog:, epoch: Time.now - 120, lifetime_aware: true))
end

def test_check_lifetime_disabled
global = {}
options = Judges::Options.new({ 'testing' => true, 'lifetime' => 100 })
loog = Loog::NULL
refute(Fbe.over?(global:, options:, loog:, epoch: Time.now - 120, lifetime_aware: false))
end

def test_check_timeout_enabled
global = {}
options = Judges::Options.new({ 'testing' => true, 'timeout' => 100 })
loog = Loog::NULL
assert(Fbe.over?(global:, options:, loog:, kickoff: Time.now - 120, timeout_aware: true))
end

def test_check_timeout_disabled
global = {}
options = Judges::Options.new({ 'testing' => true, 'timeout' => 100 })
loog = Loog::NULL
refute(Fbe.over?(global:, options:, loog:, kickoff: Time.now - 120, timeout_aware: false))
end
end