Module: Appsignal::CheckIn

Defined in:
lib/appsignal/check_in.rb,
lib/appsignal/check_in/cron.rb,
lib/appsignal/check_in/event.rb,
lib/appsignal/check_in/scheduler.rb

Defined Under Namespace

Classes: Cron, Event, Scheduler

Constant Summary collapse

HEARTBEAT_CONTINUOUS_INTERVAL_SECONDS =
30
NEW_SCHEDULER_MUTEX =
Mutex.new

Class Method Summary collapse

Class Method Details

.continuous_heartbeatsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



10
11
12
# File 'lib/appsignal/check_in.rb', line 10

def continuous_heartbeats
  @continuous_heartbeats ||= []
end

.cron(identifier) { ... } ⇒ void

This method returns an undefined value.

Track cron check-ins.

Track the execution of scheduled processes by sending a cron check-in.

To track the duration of a piece of code, pass a block to cron to report both when the process starts, and when it finishes.

If an exception is raised within the block, the finish event will not be reported, triggering a notification about the missing cron check-in. The exception will bubble outside of the cron check-in block.

Examples:

Send a cron check-in

Appsignal::CheckIn.cron("send_invoices")

Send a cron check-in with duration

Appsignal::CheckIn.cron("send_invoices") do
  # your code
end

Parameters:

  • identifier (String)

    identifier of the cron check-in to report.

Yields:

  • the block to monitor.

See Also:

Since:

  • 3.13.0



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/appsignal/check_in.rb', line 43

def cron(identifier)
  cron = Appsignal::CheckIn::Cron.new(:identifier => identifier)
  output = nil

  if block_given?
    cron.start
    output = yield
  end

  cron.finish
  output
end

.heartbeat(identifier, continuous: false) { ... } ⇒ void

This method returns an undefined value.

Track heartbeat check-ins.

Track the execution of long-lived processes by sending a heartbeat check-in.

Examples:

Send a heartbeat check-in

Appsignal::CheckIn.heartbeat("main_loop")

Parameters:

  • identifier (String)

    identifier of the heartbeat check-in to report.

  • continuous (Boolean) (defaults to: false)

    whether the heartbeats should be sent continuously during the lifetime of the process. Defaults to ‘false`.

Yields:

  • the block to monitor.

See Also:

Since:

  • 4.1.0



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/appsignal/check_in.rb', line 71

def heartbeat(identifier, continuous: false)
  if continuous
    continuous_heartbeats << Thread.new do
      loop do
        heartbeat(identifier)
        sleep HEARTBEAT_CONTINUOUS_INTERVAL_SECONDS
      end
    end

    return
  end

  event = Event.heartbeat(:identifier => identifier)
  scheduler.schedule(event)
end

.kill_continuous_heartbeatsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



15
16
17
# File 'lib/appsignal/check_in.rb', line 15

def kill_continuous_heartbeats
  continuous_heartbeats.each(&:kill)
end

.schedulerObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



88
89
90
91
92
93
94
95
96
# File 'lib/appsignal/check_in.rb', line 88

def scheduler
  return @scheduler if @scheduler

  NEW_SCHEDULER_MUTEX.synchronize do
    @scheduler ||= Scheduler.new
  end

  @scheduler
end

.stopObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



99
100
101
# File 'lib/appsignal/check_in.rb', line 99

def stop
  scheduler&.stop
end