I am automating some emails in my application with the gem delayed_job . Well, when I do rake jobs:work
and the job is executed, I get the following error:
Job Delayed :: PerformableMethod (id = 10) FAILED permanently with Delayed :: DeserializationError: ActiveRecord :: RecordNotFound, class: Competition, primary key: 16 (Could not find Competition with 'id' = 16)
That is, it will search for the competition with id = 16
that does not exist, when in fact the one that it should find is the competition with id = 23
, which is the one that was created at the moment of making the delayed_jobs that I show now.
Does anyone help me to see why he takes that id
and not the one he is creating?
Here I put the code:
competitions_controller.rb
def create
@competition = Competition.new(competition_params)
if @competition.save
@competition.delay(run_at: @competition.startdate).start_competition
@competition.delay(run_at: @competition.deadline).finish_competition
flash[:notice] = "Competition successfully created"
redirect_to competition_path(@competition)
else
render 'new'
end
end
competition.rb
def finish_competition
self.update(:finished, true)
CompetitionMailer.endCompetition(self).deliver_now
end
def start_competition
self.update(:started, true)
CompetitionMailer.startCompetition(self).deliver_now
end
To install delayed_job I followed these steps:
- Add
gem 'delayed_job_active_record'
to the gemfile -
bundle install
-
rails generate delayed_job:active_record
-
rake db:migrate
- In
config/application.rb
I addedconfig.active_job.queue_adapter = :delayed_job
Content of the job
#<Delayed::Backend::ActiveRecord::Job
id: 21,
priority: 0,
attempts: 0,
handler: "--- !ruby/object:Delayed::PerformableMethod\nobject:
!ruby/object:Competition\n raw_attributes:\n
titulo: Competición para prueba de Delayed jobs\n
descripcion: hola\n
premio: '55555'\n
dificultad: Medium\n
trainingdata: testPruebaACCURACY.csv\n
testdata: testPruebaACCURACY.csv\n
metric: Accuracy\n
type_competition: classification\n
nGroups: '1'\n
nWinners: '32'\n
deadline: &7 2017-07-18 23:43:00.000000000 Z\n
startdate: &9 2017-07-18 23:42:00.000000000 Z\n
id: '23'\n
created_at: &1 2017-07-18 21:41:04.876321372 Z\n
updated_at: *1\n
finished: \n
started: \n
attributes: !ruby/object:ActiveRecord::AttributeSet\n attributes:
!ruby/object:ActiveRecord::LazyAttributeHash\n types:\n
id: &3 !ruby/object:ActiveRecord: :ConnectionAdapters: :PostgreSQL::OID::Integer\n
precision: \n
scale: \n
limit: \n
range: !ruby/range\n
begin: -2147483648\n
end: 2147483648\n
excl: true\n
titulo: &2 !ruby/object:ActiveRecord::Type::String\n
precision: \n
scale: \n
limit: \n
descripcion: &5 !ruby/object:ActiveRecord::Type::Text\n
precision: \n
scale: \n
limit: \n
premio: *3\n
dificultad: *2\n
created_at: &11
!ruby/object:ActiveRecord: :AttributeMethods: :TimeZoneConversion: :TimeZoneConverter\n
subtype: &4
!ruby/object:ActiveRecord: :ConnectionAdapters: :PostgreSQL: :OID: :DateTime\n precision: \n
scale: \n
limit: \n
updated_at: &12
!ruby/object:ActiveRecord: :AttributeMethods: :TimeZoneConversion: :TimeZoneConverter\n
subtype: *4\n
deadline: &8 !ruby/object:ActiveRecord: :AttributeMethods: :TimeZoneConversion: :TimeZoneConverter\n
subtype: *4\n
trainingdata: *2\n
testdata: *2\n
metric: *2\n
type_competition: *2\n
finished: &6
!ruby/object:ActiveRecord::Type::Boolean\n
precision: \n
scale: \n
limit: \n
started: *6\n
startdate: &10
!ruby/object:ActiveRecord: :AttributeMethods: :TimeZoneConversion: :TimeZoneConverter\n
subtype: *4\n
nGroups: *3\n
nWinners: *3\n
values:\n
id: \n
titulo: \n
descripcion: \n
premio: \n
dificultad: \n
created_at: \n
updated_at: \n
deadline: \n
trainingdata: \n
testdata: \n
metric: \n
type_competition: \n
finished: \n
started: \n
startdate: \n
nGroups: \n
nWinners: \n
additional_types: {}\n
materialized: true\n
delegate_hash:\n
titulo: !ruby/object:ActiveRecord: :Attribute: :FromUser\n
name: titulo\n
value_before_type_cast: Competición para prueba de Delayed jobs\n
type: *2\n
value: Competición para prueba de Delayed jobs\n
descripcion: !ruby/object:ActiveRecord: :Attribute: :FromUser\n
name: descripcion\n
value_before_type_cast: Hola\n
type: *5\n
value: dasfas\n
premio: !ruby/object:ActiveRecord: :Attribute: :FromUser\n
name: premio\n
value_before_type_cast: '55555'\n
type: *3\n
value: 55555\n
dificultad: !ruby/object:ActiveRecord: :Attribute: :FromUser\n
name: dificultad\n
value_before_type_cast: Medium\n
type: *2\n
value: Medium\n
trainingdata: !ruby/object:ActiveRecord: :Attribute: :FromUser\n
name: trainingdata\n
value_before_type_cast: testPruebaACCURACY.csv\n
type: *2\n
value: testPruebaACCURACY.csv\n
testdata: !ruby/object:ActiveRecord: :Attribute: :FromUser\n
name: testdata\n
value_before_type_cast: testPruebaACCURACY.csv\n
type: *2\n
value: testPruebaACCURACY.csv\n
metric: !ruby/object:ActiveRecord: :Attribute: :FromUser\n
name: metric\n
value_before_type_cast: Accuracy\n
type: *2\n
value: Accuracy\n
type_competition: !ruby/object:ActiveRecord: :Attribute: :FromUser\n
name: type_competition\n
value_before_type_cast: classification\n
type: *2\n
value: classification\n
nGroups: !ruby/object:ActiveRecord: :Attribute: :FromUser\n
name: nGroups\n
value_before_type_cast: '1'\n
type: *3\n
value: 1\n
nWinners: !ruby/object:ActiveRecord: :Attribute: :FromUser\n
name: nWinners\n
value_before_type_cast: '32'\n
type: *3\n
value: 32\n
deadline: !ruby/object:ActiveRecord: :Attribute: :FromUser\n
name: deadline\n
value_before_type_cast: *7\n
type: *8\n
value: *7\n
startdate: !ruby/object:ActiveRecord: :Attribute: :FromUser\n
name: startdate\n
value_before_type_cast: *9\n
type: *10\n
value: *9\n
id: !ruby/object:ActiveRecord::Attribute::FromUser\n
name: id\n
value_before_type_cast: '23'\n
type: *3\n
value: 23\n
created_at: !ruby/object:ActiveRecord: :Attribute: :FromUser\n
name: created_at\n
value_before_type_cast: *1\n
type: *11\n
value: *1\n
updated_at: !ruby/object:ActiveRecord: :Attribute: :FromUser\n
name: updated_at\n
value_before_type_cast: *1\n
type: *12\n
value: *1\n
finished: !ruby/object:ActiveRecord: :Attribute: :FromDatabase\n
name: finished\n
value_before_type_cast: \n
type: *6\n
value: \n
started: !ruby/object:ActiveRecord: :Attribute: :FromDatabase\n
name: started\n
value_before_type_cast: \n
type: *6\n
value: \n
new_record: false\nmethod_name:
:finish_competition\nargs:\n- 23\n"
last_error: nil,
run_at: "2017-07-18 23:43:00",
locked_at: nil,
failed_at: nil,
locked_by: nil,
queue: nil,
created_at: "2017-07-18 21:41:05",
updated_at: "2017-07-18 21:41:05">
My version of Rails is 4.2.3