Class: AllStak::Modules::Cron

Inherits:
Object
  • Object
show all
Defined in:
lib/allstak/modules/cron.rb

Overview

Cron / background-job monitoring. Backend auto-creates monitors on first ping.

Constant Summary collapse

PATH =
"/ingest/v1/heartbeat".freeze

Instance Method Summary collapse

Constructor Details

#initialize(transport, logger, config = nil) ⇒ Cron

Returns a new instance of Cron.



7
8
9
10
11
# File 'lib/allstak/modules/cron.rb', line 7

def initialize(transport, logger, config = nil)
  @transport = transport
  @logger = logger
  @config = config
end

Instance Method Details

#job(slug) ⇒ Object

Wrap a job in a block; heartbeat sent on exit. On success → status “success”. On raise → status “failed” with message, then the exception is re-raised.

Examples:

AllStak.cron.job("daily-report") { generate_report }


19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/allstak/modules/cron.rb', line 19

def job(slug)
  start = (Time.now.to_f * 1000).to_i
  begin
    result = yield
    duration = (Time.now.to_f * 1000).to_i - start
    ping(slug, "success", duration)
    result
  rescue => e
    duration = (Time.now.to_f * 1000).to_i - start
    ping(slug, "failed", duration, message: e.message)
    raise
  end
end

#ping(slug, status, duration_ms, message: nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/allstak/modules/cron.rb', line 33

def ping(slug, status, duration_ms, message: nil)
  return false if @transport.disabled?
  begin
    payload = { slug: slug, status: status, durationMs: duration_ms }
    payload[:message] = message if message
    if @config
      payload[:environment] = @config.environment if @config.respond_to?(:environment) && @config.environment
      payload[:release] = @config.release if @config.respond_to?(:release) && @config.release
    end
    code, _ = @transport.post(PATH, payload)
    code == 202
  rescue Transport::AllStakAuthError
    @logger.debug("[AllStak] cron ping skipped — SDK disabled")
    false
  rescue => e
    @logger.debug("[AllStak] cron ping failed silently: #{e.message}")
    false
  end
end