-
Notifications
You must be signed in to change notification settings - Fork 43
/
mail_fetcher
executable file
·65 lines (56 loc) · 2.31 KB
/
mail_fetcher
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
#!/usr/bin/env ruby
# Check all emails in the gmail inbox.
#
# Any messages successfully processed will be moved out of the inbox. Others are left
# there, so we can review that messages are getting processed properly by going into the
# gmail account and having a look.
#
require File.expand_path("../config/environment", __dir__)
Publisher::Application.mail_fetcher_config.configure
require "fact_check_email_handler"
require "prometheus/client"
require "prometheus/client/push"
require "prometheus/client/registry"
require "redis"
require "redis-lock"
def redis
Redis.new(RedisConfig::REDIS_CONFIG)
end
Rails.logger.info "Running MailFetcher in #{Rails.env} mode - #{Time.zone.now.utc}"
registry = Prometheus::Client.registry
gauge = registry.gauge(
:publisher_fact_check_unprocessed_emails_total,
docstring: "Number of unprocessed fact check emails",
)
handler = FactCheckEmailHandler.new(Publisher::Application.fact_check_config, gauge)
# The lock is created and belongs to this process for as long as the `life`.
# When the block has finished executing, the lock is explicitly released.
# If an exception is raised in the block, the lock is explicitly released.
#
# If we fail to explicitly release the lock before the end of its life
# (eg network cable unplugged), it will be considered stale by other processes
# if its life has passed.
#
# We set the lock to expire after five minutes. After we process each message,
# we update it to expire at five minutes from that point. This should mean that
# we retain the lock for the duration, even if processing takes longer than
# five minutes. The exception to that is if processing a single message takes
# that long.
AUTOMATIC_LOCK_EXPIRY = (5 * 60) # seconds
begin
Redis.new.lock("publisher:fact_check_processing_lock", life: AUTOMATIC_LOCK_EXPIRY) do |lock|
handler.process do
lock.extend_life(AUTOMATIC_LOCK_EXPIRY)
end
end
rescue Redis::Lock::LockNotAcquired => e
Rails.logger.debug("Failed to get lock for fact check processing (#{e.message}). Another process probably got there first.")
rescue StandardError => e
GovukError.notify(e)
raise
end
Rails.logger.info "Finished running MailFetcher in #{Rails.env} mode - #{Time.zone.now.utc}"
Prometheus::Client::Push.new(
job: "publisher-metrics",
gateway: ENV.fetch("PROMETHEUS_PUSHGATEWAY_URL"),
).add(registry)