Class: MatViews::RefreshViewJob

Inherits:
ApplicationJob show all
Defined in:
app/jobs/mat_views/refresh_view_job.rb

Overview

ActiveJob that handles ‘REFRESH MATERIALIZED VIEW` for a given MatViewDefinition.

The job mirrors CreateViewJob‘s lifecycle: it measures duration and persists state in MatViewRun.

The actual refresh implementation is delegated based on definition.refresh_strategy:

Row count reporting can be controlled via row_count_strategy:

  • :estimated (default) - fast, approximate via reltuples

  • :exact - accurate ‘COUNT(*)`

  • nil - skip counting

Examples:

Enqueue a refresh with exact row count

MatViews::RefreshViewJob.perform_later(definition.id, :exact)

Enqueue using keyword-hash form

MatViews::RefreshViewJob.perform_later(definition.id, row_count_strategy: :estimated)

See Also:

Instance Method Summary collapse

Instance Method Details

#perform(mat_view_definition_id, row_count_strategy_arg = nil) ⇒ Hash

Perform the refresh job for the given materialised view definition.

Parameters:

  • mat_view_definition_id (Integer, String)
  • row_count_strategy_arg (:Symbol, String) (defaults to: nil)

    One of: :estimated, :exact, :none or nil.

Returns:

  • (Hash)

    Serialized ServiceResponse#to_h:

    • :status [Symbol]

    • :error [String, nil]

    • :duration_ms [Integer]

    • :meta [Hash]

Raises:

  • (StandardError)

    Re-raised on unexpected failure after marking the run failed.



66
67
68
69
70
71
# File 'app/jobs/mat_views/refresh_view_job.rb', line 66

def perform(mat_view_definition_id, row_count_strategy_arg = nil)
  definition = MatViews::MatViewDefinition.find(mat_view_definition_id)
  record_run(definition, :refresh) do
    service(definition).new(definition, row_count_strategy: normalize_strategy(row_count_strategy_arg)).call
  end
end