Class: AllStak::Modules::Cron
- Inherits:
-
Object
- Object
- AllStak::Modules::Cron
- 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
-
#initialize(transport, logger, config = nil) ⇒ Cron
constructor
A new instance of Cron.
-
#job(slug) ⇒ Object
Wrap a job in a block; heartbeat sent on exit.
- #ping(slug, status, duration_ms, message: nil) ⇒ Object
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.
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.) 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] = if 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.}") false end end |