Class: Demon::EmailSync

Inherits:
Base
  • Object
show all
Defined in:
lib/demon/email_sync.rb

Constant Summary collapse

HEARTBEAT_KEY =
"email_sync_heartbeat"
HEARTBEAT_INTERVAL =
60.seconds
DEFAULT_MAX_ALLOWED_EMAIL_SYNC_RSS_MEGABYTES =
500

Constants inherited from Base

Base::HOSTNAME

Instance Attribute Summary

Attributes inherited from Base

#index, #parent_pid, #pid, #started, #stop_timeout

Class Method Summary collapse

Methods inherited from Base

#alive?, alive?, #already_running?, demons, #ensure_running, ensure_running, #initialize, kill, #kill, #log, #pid_file, reset_demons, restart, #restart, #run, set_demons, #set_pid, start, #start, stop, #stop, #stop_signal

Constructor Details

This class inherits a constructor from Demon::Base

Class Method Details

.check_email_sync_heartbeatObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/demon/email_sync.rb', line 47

def self.check_email_sync_heartbeat
  if defined?(@@email_sync_next_heartbeat_check) && @@email_sync_next_heartbeat_check &&
       @@email_sync_next_heartbeat_check > Time.now.to_i
    return
  end

  @@email_sync_next_heartbeat_check = (Time.now + HEARTBEAT_INTERVAL).to_i

  should_restart = false

  # Restart process if it does not respond anymore
  last_heartbeat_ago = Time.now.to_i - Discourse.redis.get(HEARTBEAT_KEY).to_i

  if last_heartbeat_ago > HEARTBEAT_INTERVAL.to_i
    Rails.logger.warn(
      "EmailSync heartbeat test failed (last heartbeat was #{last_heartbeat_ago}s ago), restarting",
    )

    should_restart = true
  end

  # Restart process if memory usage is too high
  if !should_restart && (email_sync_rss = max_email_sync_rss) > max_allowed_email_sync_rss
    Rails.logger.warn(
      "EmailSync is consuming too much memory (using: %0.2fM) for '%s', restarting" %
        [(email_sync_rss.to_f / 1.megabyte), HOSTNAME],
    )

    should_restart = true
  end

  restart if should_restart
end

.prefixObject



9
10
11
# File 'lib/demon/email_sync.rb', line 9

def self.prefix
  "email_sync"
end

.test_cleanupObject



42
43
44
# File 'lib/demon/email_sync.rb', line 42

def self.test_cleanup
  @@email_sync_next_heartbeat_check = nil
end