-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.rb
More file actions
68 lines (57 loc) · 1.86 KB
/
Copy pathworker.rb
File metadata and controls
68 lines (57 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# frozen_string_literal: true
require './app'
# Fetches and caches Hacker News items
class Worker
class FetchFailed < StandardError; end
def self.interval
value = Integer ENV.fetch('WORKER_INTERVAL', '300')
raise ArgumentError, 'WORKER_INTERVAL must be positive' unless value.positive?
value
end
def self.max_consecutive_failures
Integer ENV.fetch('WORKER_MAX_FAILURES', '5')
end
def self.run
App.logger.info 'Fetching top stories...'
fetched = Item.top_stories
if fetched.nil?
App.logger.error 'Skipped finish: top stories fetch failed'
raise FetchFailed, 'top story IDs unavailable'
elsif fetched.empty?
App.logger.error 'Done fetching top stories: none persisted'
raise FetchFailed, 'no top stories persisted'
else
App.logger.info "Done fetching top stories (#{fetched.size})"
end
# Refresh what HN says changed, then re-check a slice of the cache the feed
# may have missed, then pull in comments we still do not have. Discovering a
# new reply depends on its parent's kids list being current, which is why
# both refresh passes run before backfill.
Item.sync_updates
Item.reconcile
story_ids = fetched.pluck :id
added = Item.backfill story_ids
App.logger.info "Backfilled #{added} new comments"
Item.prune story_ids
end
def self.start
failures = 0
loop do
begin
run
failures = 0
rescue Interrupt, SignalException
raise
rescue StandardError => e
failures += 1
App.logger.error "Worker run failed (#{e.class}): #{e}\n#{Array(e.backtrace).join("\n")}"
if failures >= max_consecutive_failures
App.logger.fatal "Worker exiting after #{failures} consecutive failures"
raise
end
end
sleep interval
end
end
end
Worker.start if $PROGRAM_NAME == __FILE__