Module: Pigeon::Processor::BackgroundProcessor

Included in:
Pigeon::Processor
Defined in:
lib/pigeon/processor/background_processor.rb

Overview

Background processing functionality for the processor

Instance Method Summary collapse

Instance Method Details

#process_startup_messages(batch_size = 100) ⇒ Hash

Process pending messages on startup

Parameters:

  • batch_size (Integer) (defaults to: 100)

    Number of messages to process in one batch

Returns:

  • (Hash)

    Processing statistics



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/pigeon/processor/background_processor.rb', line 74

def process_startup_messages(batch_size = 100)
  Pigeon.config.logger.info("Processing pending messages on startup")

  # Process pending messages
  stats = process_pending(batch_size: batch_size)

  # Log the results
  Pigeon.config.logger.info("Startup processing complete: #{stats[:processed]} messages processed")

  stats
end

#processing?Boolean

Check if processing is running

Returns:

  • (Boolean)

    Whether processing is running



67
68
69
# File 'lib/pigeon/processor/background_processor.rb', line 67

def processing?
  @processing.true?
end

#start_background_processing(batch_size: 100, interval: 5, thread_count: 2) ⇒ Boolean

Start processing pending messages in the background

Parameters:

  • batch_size (Integer) (defaults to: 100)

    Number of messages to process in one batch

  • interval (Integer) (defaults to: 5)

    Interval in seconds between processing batches

  • thread_count (Integer) (defaults to: 2)

    Number of threads to use for processing

Returns:

  • (Boolean)

    Whether processing was started



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pigeon/processor/background_processor.rb', line 14

def start_background_processing(batch_size: 100, interval: 5, thread_count: 2)
  return false if @processing.true?

  @mutex.with_write_lock do
    return false if @processing.true?

    Pigeon.config.logger.info("Starting background processing of outbox messages")

    # Process any pending messages immediately
    process_startup_messages(batch_size)

    # Create a thread pool for processing
    @thread_pool = Concurrent::FixedThreadPool.new(thread_count)

    # Set up periodic processing
    @scheduler = Concurrent::TimerTask.new(execution_interval: interval) do
      process_pending(batch_size: batch_size)
    end

    # Start the scheduler
    @scheduler.execute

    @processing.make_true
  end

  true
end

#stop_background_processingBoolean

Stop processing pending messages

Returns:

  • (Boolean)

    Whether processing was stopped



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/pigeon/processor/background_processor.rb', line 44

def stop_background_processing
  return false unless @processing.true?

  @mutex.with_write_lock do
    return false unless @processing.true?

    Pigeon.config.logger.info("Stopping background processing of outbox messages")

    # Stop the scheduler
    @scheduler&.shutdown

    # Shutdown the thread pool
    @thread_pool&.shutdown
    @thread_pool&.wait_for_termination(10) # Wait up to 10 seconds for threads to finish

    @processing.make_false
  end

  true
end