Class: Admin::BackgroundMigrationsController

Inherits:
CrmController
  • Object
show all
Defined in:
app/controllers/admin/background_migrations_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject



7
8
9
10
11
# File 'app/controllers/admin/background_migrations_controller.rb', line 7

def index
  @migrations = OnlineMigrations::BackgroundMigrations::Migration
                .order(created_at: :desc)
                .includes(:migration_jobs)
end

#pauseObject

Pause a running migration



42
43
44
45
46
47
48
49
50
# File 'app/controllers/admin/background_migrations_controller.rb', line 42

def pause
  if @migration.status.in?(%w[running enqueued])
    @migration.update(status: 'paused')
    flash[:success] = 'Migration has been paused'
  else
    flash[:warning] = "Cannot pause migration in '#{@migration.status}' status"
  end
  redirect_to admin_background_migration_path(@migration)
end

#resumeObject

Resume a paused migration



53
54
55
56
57
58
59
60
61
# File 'app/controllers/admin/background_migrations_controller.rb', line 53

def resume
  if @migration.status == 'paused'
    @migration.update(status: 'enqueued')
    flash[:success] = 'Migration has been resumed'
  else
    flash[:warning] = "Cannot resume migration in '#{@migration.status}' status"
  end
  redirect_to admin_background_migration_path(@migration)
end

#retry_failedObject

Retry all failed jobs for a migration



29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/admin/background_migrations_controller.rb', line 29

def retry_failed
  failed_count = @migration.migration_jobs.where(status: 'failed').update_all(status: 'enqueued', attempts: 0)
  if failed_count.positive?
    # Also reset migration status if it was failed
    @migration.update(status: 'enqueued') if @migration.status == 'failed'
    flash[:success] = "#{failed_count} failed job(s) have been re-enqueued"
  else
    flash[:info] = 'No failed jobs to retry'
  end
  redirect_to admin_background_migration_path(@migration)
end

#run_batchObject

Force run a single batch for a migration



18
19
20
21
22
23
24
25
26
# File 'app/controllers/admin/background_migrations_controller.rb', line 18

def run_batch
  result = OnlineMigrations.run_background_data_migrations
  if result
    flash[:success] = "Batch processed successfully for migration ##{result.migration_id}"
  else
    flash[:info] = 'No pending batches to process at this time'
  end
  redirect_to admin_background_migration_path(@migration)
end

#showObject



13
14
15
# File 'app/controllers/admin/background_migrations_controller.rb', line 13

def show
  @jobs = @migration.migration_jobs.order(created_at: :desc).limit(100)
end

#trigger_schedulerObject

Trigger the scheduler manually



64
65
66
67
68
# File 'app/controllers/admin/background_migrations_controller.rb', line 64

def trigger_scheduler
  OnlineMigrationsSchedulerWorker.perform_async
  flash[:success] = 'Background migration scheduler has been triggered'
  redirect_to admin_background_migrations_path
end