Class: DataMigration::Task

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/data_migration/task.rb

Constant Summary collapse

STATUS_OPTIONS =
{
  started: "started",
  performing: "performing",
  paused: "paused",
  failed: "failed",
  completed: "completed"
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.job_classObject



52
53
54
# File 'lib/data_migration/task.rb', line 52

def self.job_class
  DataMigration.config.job_class
end

.listObject



82
83
84
# File 'lib/data_migration/task.rb', line 82

def self.list
  Dir[DataMigration.config.data_migrations_path_glob].map { |f| File.basename(f, ".*") }
end

.perform_later(name, **kwargs) ⇒ Object



65
66
67
# File 'lib/data_migration/task.rb', line 65

def self.perform_later(name, **kwargs)
  create!(name: name).perform_later(**kwargs)
end

.perform_now(name, **kwargs) ⇒ Object



56
57
58
# File 'lib/data_migration/task.rb', line 56

def self.perform_now(name, **kwargs)
  create!(name: name).perform_now(**kwargs)
end

.prepare(name, pause_minutes: nil, jobs_limit: nil) ⇒ Object



74
75
76
# File 'lib/data_migration/task.rb', line 74

def self.prepare(name, pause_minutes: nil, jobs_limit: nil)
  create!(name: name, pause_minutes: pause_minutes, jobs_limit: jobs_limit)
end

.root_pathObject



78
79
80
# File 'lib/data_migration/task.rb', line 78

def self.root_path
  DataMigration.config.data_migrations_full_path
end

Instance Method Details

#file_exists?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/data_migration/task.rb', line 90

def file_exists?
  self.class.list.include?(name)
end

#file_pathObject



86
87
88
# File 'lib/data_migration/task.rb', line 86

def file_path
  "#{self.class.root_path}/#{name}.rb"
end

#job_check_in!(job_id, job_args: [], job_kwargs: {}) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/data_migration/task.rb', line 98

def job_check_in!(job_id, job_args: [], job_kwargs: {})
  with_lock do
    self.current_jobs ||= {}

    raise DataMigration::JobConflictError, "#{user_title} already has job ##{job_id}" if current_jobs.key?(job_id)
    raise DataMigration::JobConcurrencyLimitError, "#{user_title} reached limit of #{jobs_limit} jobs" if jobs_limit.present? && current_jobs.size >= jobs_limit

    current_jobs[job_id] = {
      ts: Time.current,
      args: job_args,
      kwargs: job_kwargs
    }
    save!
  end
end

#job_check_out!(job_id, status: nil) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/data_migration/task.rb', line 114

def job_check_out!(job_id, status: nil)
  with_lock do
    current_jobs.delete(job_id)
    self.status = status if status
    save!(validate: false)
  end
end

#not_started?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/data_migration/task.rb', line 94

def not_started?
  status.nil? && started_at.nil?
end

#perform_later(**perform_args) ⇒ Object



69
70
71
72
# File 'lib/data_migration/task.rb', line 69

def perform_later(**perform_args)
  update!(kwargs: perform_args)
  self.class.job_class.perform_later(id, **perform_args)
end

#perform_now(**perform_args) ⇒ Object



60
61
62
63
# File 'lib/data_migration/task.rb', line 60

def perform_now(**perform_args)
  update!(kwargs: perform_args)
  self.class.job_class.perform_now(id, **perform_args)
end

#requires_pause?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/data_migration/task.rb', line 126

def requires_pause?
  pause_minutes.positive? && !paused?
end

#user_titleObject



122
123
124
# File 'lib/data_migration/task.rb', line 122

def user_title
  "Data migration ##{id} #{name}"
end