Class: Octospy::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/octospy/worker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repositories, &block) ⇒ Worker

Returns a new instance of Worker.



5
6
7
8
9
10
# File 'lib/octospy/worker.rb', line 5

def initialize(repositories, &block)
  @repositories = repositories
  @block = block
  @last_event_id = nil
  thread_loop
end

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



3
4
5
# File 'lib/octospy/worker.rb', line 3

def thread
  @thread
end

Instance Method Details

#api_requestable?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/octospy/worker.rb', line 37

def api_requestable?
  limit = Octokit.rate_limit
  if !limit.limit.zero? && limit.remaining.zero?
    notify "ヾ(;´Д`)ノ #{limit}"
    false
  else
    true
  end
# No rate limit for white listed users
rescue Octokit::NotFound
  true
end

#notify_recent_envetsObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/octospy/worker.rb', line 86

def notify_recent_envets
  events = repository_events
  return if events.count.zero?

  last_event = events.sort_by(&:id).last
  debug 'last_event', "      repo: \#{last_event.repo.name},\n      event_type: \#{last_event.type},\n      \#{@last_event_id.nil? ?\n        \"while_ago: \#{while_ago}, created_at: \#{last_event.created_at}\" :\n        \"last_id: \#{@last_event_id}, current_id: \#{last_event.id}\"}\n    MSG\n\n  # ascending by event.id\n  events.sort_by(&:id).each { |event|\n    next if skipping?(event)\n\n    parsed_event = Octospy.parse(event)\n\n    unless parsed_event\n      debug 'could_not_parse', <<-MSG.compact\n          repo: \#{event.repo.name},\n          event: \#{event.type}\n        MSG\n      next\n    end\n\n    @last_event_id = event.id.to_i\n    parsed_event.each { |p| notify p[:message] }\n  }\nend\n".compact

#repository_eventsObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/octospy/worker.rb', line 50

def repository_events
  @repositories.each_with_object([]) do |repo, arr|
    break unless api_requestable?

    sleep Octospy.api_request_interval
    events = ::Octokit.repository_events(repo.to_s)
    arr.concat events

    debug_attrs = "      repo: \#{repo},\n      limit: \#{Octokit.rate_limit.remaining}/\#{Octokit.rate_limit.limit},\n      reset: \#{Octokit.rate_limit.resets_at.strftime('%H:%M:%S')} *after \#{Octokit.rate_limit.resets_in}sec,\n    MSG\n\n    if !events.nil? && !events.empty?\n      debug_attrs << ' ' + <<-MSG.compact\n        first: \#{events.first.type},\n        last: \#{events.last.type}\n      MSG\n    end\n\n    debug 'get_event', debug_attrs\n  end\nend\n".compact

#skipping?(event) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
83
84
# File 'lib/octospy/worker.rb', line 75

def skipping?(event)
  case
  when event.nil?,
       @last_event_id.nil? && while_ago >= event.created_at,
      !@last_event_id.nil? && @last_event_id >= event.id.to_i
    true
  else
    false
  end
end

#thread_loopObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/octospy/worker.rb', line 16

def thread_loop
  debug 'thread_start', "      api_request_interval(A): \#{Octospy.api_request_interval},\n      repoisitory_count(R): \#{@repositories.count},\n      worker_interval(W): \#{Octospy.worker_interval},\n      work_interval(A*R+W): \#{work_interval}\n    MSG\n\n  @thread = Thread.start { loop { work } }\nend\n".compact

#workObject



27
28
29
30
31
32
33
34
35
# File 'lib/octospy/worker.rb', line 27

def work
  notify_recent_envets
  debug 'sleep', work_interval
  sleep work_interval
rescue => e
  error e.message
  debug 'sleep', work_interval
  sleep worker_interval
end

#work_intervalObject



12
13
14
# File 'lib/octospy/worker.rb', line 12

def work_interval
  (Octospy.api_request_interval * @repositories.count) + Octospy.worker_interval
end