Class: BSV::Wallet::Daemon

Inherits:
Object
  • Object
show all
Defined in:
lib/bsv/wallet/daemon.rb

Overview

Background polling loop for entity-driven network interactions.

Periodically queries for entities needing push or fetch operations and dispatches them through Services. Each entity is processed independently – one failure does not block others.

The daemon does not import postgres models directly. Instead, it accepts callable query objects (lambdas, procs, or anything responding to call) that return arrays of Pushable/Fetchable entities. This keeps the wallet gem free of postgres dependencies.

Examples:

daemon = BSV::Wallet::Daemon.new(
  services: services,
  pending_pushes: -> { Broadcast.where(broadcast_at: nil).exclude(raw_tx: nil).all },
  stale_fetches:  -> { Broadcast.where { broadcast_at < Time.now - 30 }.all },
  pending_proofs: -> { Action.where(outgoing: true).exclude(wtxid: nil).where(tx_proof_id: nil).all },
  interval: 30
)
daemon.start

Instance Method Summary collapse

Constructor Details

#initialize(services:, pending_pushes: -> { [] }, stale_fetches: -> { [] }, pending_proofs: -> { [] }, interval: 30) ⇒ Daemon

Returns a new instance of Daemon.

Parameters:

  • services (BSV::Network::Services)

    network routing layer

  • pending_pushes (#call) (defaults to: -> { [] })

    returns entities needing push

  • stale_fetches (#call) (defaults to: -> { [] })

    returns entities needing fetch (broadcast status)

  • pending_proofs (#call) (defaults to: -> { [] })

    returns entities needing fetch (proof acquisition)

  • interval (Numeric) (defaults to: 30)

    seconds to sleep between cycles



31
32
33
34
35
36
37
38
39
# File 'lib/bsv/wallet/daemon.rb', line 31

def initialize(services:, pending_pushes: -> { [] }, stale_fetches: -> { [] },
               pending_proofs: -> { [] }, interval: 30)
  @services = services
  @pending_pushes = pending_pushes
  @stale_fetches = stale_fetches
  @pending_proofs = pending_proofs
  @interval = interval
  @running = false
end

Instance Method Details

#run_cycleObject

Execute one polling cycle. Public for testability.



58
59
60
61
62
63
64
65
# File 'lib/bsv/wallet/daemon.rb', line 58

def run_cycle
  push_pending
  fetch_stale
  fetch_proofs
  sleep @interval
rescue StandardError => e
  BSV.logger&.error { "[Daemon] cycle error: #{e.class}: #{e.message}" }
end

#running?Boolean

Whether the daemon is currently running.

Returns:

  • (Boolean)


53
54
55
# File 'lib/bsv/wallet/daemon.rb', line 53

def running?
  @running
end

#startObject

Start the polling loop. Blocks until stop is called.



42
43
44
45
# File 'lib/bsv/wallet/daemon.rb', line 42

def start
  @running = true
  run_cycle while @running
end

#stopObject

Signal the loop to exit after the current cycle completes.



48
49
50
# File 'lib/bsv/wallet/daemon.rb', line 48

def stop
  @running = false
end