Class: BudgetRefresherIncrementalWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job, Workers::StatusBroadcastable
Defined in:
app/workers/budget_refresher_incremental_worker.rb

Overview

Incremental budget refresher worker that uses upserts instead of delete-all-reinsert.
This is faster and safer for frequent (hourly) refreshes.

Key differences from BudgetRefresherWorkerWithStatus:

  • Uses incremental: true mode which performs upserts
  • Does not delete existing facts before inserting
  • Can be run more frequently without data loss risk

Usage:
BudgetRefresherIncrementalWorker.perform_async(year: 2026, month: 2)
BudgetRefresherIncrementalWorker.perform_async(current_month: true)

Instance Attribute Summary

Attributes included from Workers::StatusBroadcastable

#broadcast_status_updates

Instance Method Summary collapse

Methods included from Workers::StatusBroadcastable::Overrides

#at, #store, #total

Instance Method Details

#perform(options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/workers/budget_refresher_incremental_worker.rb', line 20

def perform(options = {})
  # Support current_month: true for scheduled jobs
  if options[:current_month]
    year = Date.current.year
    month = Date.current.month
  else
    year = options[:year].to_i
    month = options[:month].to_i
  end

  total 5
  at(1, "Initializing incremental budget refresh for #{Date::MONTHNAMES[month]} #{year}...")

  at(2, 'Pre-loading reference data (projects, suppliers)...')

  at(3, 'Computing ledger and budget aggregates...')

  # Pass a progress callback to the refresh method with incremental: true
  Analytic::BudgetFact.refresh_data_for_month(year, month, incremental: true) do |step, message|
    # Steps 3-4 are for the main processing
    at(3 + (step.to_f / 100).round(1), message) if step && message
  end

  at(4, 'Upserting budget facts...')

  at(5, 'Complete!')
  store redirect_to: "/reports/budget?company=all&date[year]=#{year}&date[month]=#{month}"
end