Class: Ocpp::Rails::CleanupStateChangesJob

Inherits:
ApplicationJob
  • Object
show all
Defined in:
app/jobs/ocpp/rails/cleanup_state_changes_job.rb

Instance Method Summary collapse

Instance Method Details

#performObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/jobs/ocpp/rails/cleanup_state_changes_job.rb', line 6

def perform
  unless Ocpp::Rails.configuration.state_change_cleanup_enabled
    ::Rails.logger.info("StateChange cleanup disabled, skipping")
    return
  end

  begin
    retention_days = Ocpp::Rails.configuration.state_change_retention_days
    deleted_count = Ocpp::Rails::StateChange.older_than(retention_days).delete_all

    ::Rails.logger.info("Deleted #{deleted_count} StateChange records older than #{retention_days} days")
  rescue => error
    ::Rails.logger.error("StateChange cleanup failed: #{error.message}")
  ensure
    # Re-enqueue for next run (24 hours later) only if cleanup is enabled
    if Ocpp::Rails.configuration.state_change_cleanup_enabled
      self.class.set(wait: 24.hours).perform_later
    end
  end
end