delayed job not performing after being enqued, rails - ruby-on-rails

I did this in controller:
MyTestJob.perform_later
When this is called, this is displayed in my rails console:
[ActiveJob] Enqueued MyTestJob (Job ID: e7af2684-4dc4-4a1f-bac7-b189dddb6f2f) to DelayedJob(default)
[ActiveJob] (0.4ms) BEGIN
[ActiveJob] SQL (0.6ms) INSERT INTO "public"."delayed_jobs" ("queue", "handler", "run_at", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["queue", "default"], ["handler", "--- !ruby/object:ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper\njob_data:\n job_class: GenerateCouponcodesJob\n job_id: e7af2684-4dc4-4a1f-bac7-b189dddb6f2f\n queue_name: default\n arguments: []\n"], ["run_at", "2015-10-28 09:58:17.891850"], ["created_at", "2015-10-28 09:58:17.892095"], ["updated_at", "2015-10-28 09:58:17.892095"]]
[ActiveJob] (11.0ms) COMMIT
Rendered campaign/test.html.erb within layouts/application (0.1ms)
Completed 200 OK in 254ms (Views: 233.6ms | ActiveRecord: 12.0ms)
SO the above log says that job is enqued (which proves my delayed job worker is working fine), but it is not getting performed,
here is my job:
class MyTestJob < ActiveJob::Base
queue_as :default
def perform(*args)
directory = Rails.root
File.open(File.join(directory, 'file.txt'), 'w') do |f|
f.puts "contents"
end
Delayed::Worker.logger.debug("Log Entry")
end
end
No file created and no log created in delayed_job.log.
I have done everything correctly but still, there are no error as well...

the above log says that job is enqueued (which proves my delayed job worker is working fine)
Nope. This means that the "enqueuer" is working fine. Says nothing about the worker.
It should be run as a separate process.
bundle exec rake jobs:work

Related

Rails Runner or Rake task can't trigger Active Job job

I have setup a Rails 7 project running in Docker. Using the whenever gem (using cron) I tried to execute either
a rails runner task
a rake task
Both shall do the same thing: call a class method WebpageChangeCheck.check_all which itself calls a method of a model. Which then creates an Active Job job. But both fail in the final step to create the job:
app/cron_jobs/webpage_change_check.rb:
class WebpageChangeCheck
def self.check_all
Webpage.all.each do |page|
if page.checking_active
page.check_for_change
end
end
end
end
app/models/webpage.rb:
def check_for_change
self.update(counter: self.counter += 1)
UpdateOffersHashJob.perform_later(self)
end
update_offers_hash_job.rb:
class UpdateOffersHashJob < ApplicationJob
queue_as :default
require 'nokogiri'
require 'open-uri'
require 'net/http'
after_perform do |job|
compare_hashes(job.arguments.first)
end
def perform(page)
page.update(offers_hash_old: page.offers_hash_new)
all_offers = ""
doc = Nokogiri::HTML(URI.open(page.url))
doc.css(page.selector).each do |offer|
all_offers += offer.to_s
end
page.update(offers_hash_new: all_offers.delete(" \t\r\n\ "))
end
private
def compare_hashes(page)
...
end
end
What works:
calling the same class method from rails console creates the Active Job as expected. I get the following output:
irb(main):002:0> WebpageChangeCheck.check_all
Webpage Load (1.2ms) SELECT "webpages".* FROM "webpages"
Webpage Update All (3.9ms) UPDATE "webpages" SET "counter" = COALESCE("counter", 0) + $1 WHERE "webpages"."id" = $2 [["counter", 1], ["id", 1]]
Enqueued UpdateOffersHashJob (Job ID: 707e164d-b8b9-407b-aa35-4b23c37b4f07) to Async(default) with arguments: #<GlobalID:0x00007f2e0b7c4878 #uri=#<URI::GID gid://my_rails_app/Webpage/1>>
=>
[#<Webpage:0x00007f2e0b7bded8
id: 1,
title: "example.com",
url: "https://www.example.com",
user_id: 1,
created_at: Wed, 18 Jan 2023 14:22:51.904097000 CET +01:00,
updated_at: Fri, 20 Jan 2023 00:12:02.749748000 CET +01:00,
interval: 1,
checking_active: true,
selector: ".headline_content",
counter: 386>]
Webpage Load (0.6ms) SELECT "webpages".* FROM "webpages" WHERE "webpages"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Performing UpdateOffersHashJob (Job ID: 707e164d-b8b9-407b-aa35-4b23c37b4f07) from Async(default) enqueued at 2023-01-20T07:26:02Z with arguments: #<GlobalID:0x00007f2e092e1088 #uri=#<URI::GID gid://my_rails_app/Webpage/1>>
TRANSACTION (0.2ms) BEGIN
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
irb(main):003:0> TRANSACTION (1.0ms) COMMIT
TRANSACTION (0.4ms) BEGIN
Webpage Load (0.6ms) SELECT "webpages".* FROM "webpages" WHERE "webpages"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Sim Create (1.4ms) INSERT INTO "sims" ("to", "time", "api_response", "success", "webpage_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["to", 49123456789], ["time", nil], ["api_response", nil], ["success", nil], ["webpage_id", 1], ["created_at", "2023-01-20 07:26:03.108189"], ["updated_at", "2023-01-20 07:26:03.108189"]]
TRANSACTION (3.5ms) COMMIT
TRANSACTION (0.4ms) BEGIN
Sim Update (1.0ms) UPDATE "sims" SET "time" = $1, "api_response" = $2, "success" = $3, "updated_at" = $4 WHERE "sims"."id" = $5 [["time", "Fri, 20.01.23 - 08h26 03s"], ["api_response", "100\nVerbucht: 0\nPreis: 0.075\nGuthaben: 0.35\nText: ALERT\nSMS-Typ: direct\nFlash SMS: false\nEncoding: gsm\nGSM0338: true\nDebug: true"], ["success", true], ["updated_at", "2023-01-20 07:26:03.295980"], ["id", 22]]
TRANSACTION (3.8ms) COMMIT
Performed UpdateOffersHashJob (Job ID: 707e164d-b8b9-407b-aa35-4b23c37b4f07) from Async(default) in 585.89ms
What I see from bash (from rake or rails runner) the Active Job gets created but it will not perform:
root#f77855c949a8:/opt/app# rake debug check_all_pages
Webpage Load (1.5ms) SELECT "webpages".* FROM "webpages"
↳ app/cron_jobs/webpage_change_check.rb:4:in `check_all'
Webpage Update All (2.9ms) UPDATE "webpages" SET "counter" = COALESCE("counter", 0) + $1 WHERE "webpages"."id" = $2 [["counter", 1], ["id", 1]]
↳ app/models/webpage.rb:9:in `check_for_change'
[ActiveJob] Enqueued UpdateOffersHashJob (Job ID: f4495cb8-868f-4ed5-9f03-7f4407b5efa4) to Async(default) with arguments: #<GlobalID:0x00007fd55771ece0 #uri=#<URI::GID gid://my_rails_app/Webpage/1>>
root#f77855c949a8:/opt/app#
here you also go with the rake task:
my_rails_app/lib/tasks/checker_task.rake:
desc "checks all Webpages for changes. Called from cronjob."
task check_all_pages: :environment do
WebpageChangeCheck.check_all
end
Conclusion:
it seems like the environment loaded properly, including environment variables. I have access to my classes and models.
nevertheless there seems to be a difference which prevents the Job from being performed.
part of the problem might be there is not enough logging to debug it, so this could be improved as well!
Any ideas?
Thanks a lot!
You need to process your job queue in another process, e.g.
rake jobs:work
So, thanks to the help in the comments I realized I overlooked to install the delayed_job backend/processing queue incl. db table for Active Job cause I thought this comes with it. It is needed to enqueue and work off the jobs. Not suitable for large amounts of jobs but sufficient for my purpose.
Here is a complete guide:
https://axiomq.com/blog/deal-with-long-running-rails-tasks-with-delayed-job/
You can also check these docs:
https://guides.rubyonrails.org/active_job_basics.html
https://github.com/collectiveidea/delayed_job#active-job
Short version:
I added the gem 'delayed_job_active_record'
Add and execute
bundle install
config/application.rb:
config.active_job.queue_adapter = :delayed_job
rails generate delayed_job:active_record
rails db:migrate
rails jobs:work
As mentioned, this is a seperate process that has to be run.
Question remains why the job performs when being run through rails console. Maybe it won't queue then but execute rightaway ..

How to identify Rails 6 bulk insert error

I have the following relations set up:
user has_many quizzes
quiz belongs_to user
quiz has_many questions
question belongs_to quiz
App is set up to use PostgreSQL. I'm trying to bulk insert a bunch of records using the insert_all! method
begin
quiz = user.quizzes.create!(title: title, slug: slug)
quiz_questions = params[:quiz][:questions].map! do |q|
# creating an attribute hash here (code removed for conciseness of question)
end
result = quiz.questions.insert_all!(quiz_questions)
This threw an error which was caught by my "catch all" block
rescue ActiveRecord::ActiveRecordError
render json: { message: ['Something went wrong'] }, status: 500
The running server console printed this message:
TRANSACTION (0.9ms) BEGIN
↳ app/controllers/quizzes_controller.rb:14:in `create'
Quiz Create (2.8ms) INSERT INTO "quizzes" ("title", "user_id", "slug", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["title", "a quiz"], ["user_id", 1], ["slug", "a-quizk2DqYk"], ["created_at", "2021-12-01 05:00:05.800134"], ["updated_at", "2021-12-01 05:00:05.800134"]]
↳ app/controllers/quizzes_controller.rb:14:in `create'
TRANSACTION (1.6ms) COMMIT
↳ app/controllers/quizzes_controller.rb:14:in `create'
Question Bulk Insert (0.6ms) INSERT INTO "questions" ("question","a","b","c","d","score","answer","quiz_id") VALUES ('what is name', 'str', 'char', 'num', 'bool', 5, 'A', 1), ('die', 'yes', 'no', 'ok', 'what', 5, 'B', 1) RETURNING "id"
↳ (eval):6:in `block in insert_all!'
Completed 500 Internal Server Error in 153ms (Views: 0.2ms | ActiveRecord: 38.1ms | Allocations: 49609)
So I think I am not calling insert_all! correctly because the server just does an insert without the BEGIN and COMMIT bookends. Also, I would like to know which error is being thrown and caught by the catch all block. What would be the correct way to do insert_all! ?
you could wrap your bulk insert into a transaction
def bulk_insert
ActiveRecord::Base.transaction do
quiz = user.quizzes.create!(title: title, slug: slug)
quiz_questions = params[:quiz][:questions].map! do |q|
# creating an attribute hash here
# ...
# note that you could validate attribute manually
raise ActiveRecord::Rollback if q.description.blank?
end
result = quiz.questions.insert_all!(quiz_questions)
end
rescue ActiveRecord::Rollback => e
puts e
end

Why some model attributes not saved in the db?

I have a Quote model in my rails app which has various attr types, some of which are being sent to / saved by the db, some are not and I cannot understand why. Please can you help me understand, thanks.
quotes_controller.rb
class QuotesController < ApplicationController
def create
#quote = Quote.new(quote_params)
if #quote.save
redirect_to root_url, notice: 'Quote request created'
else
render :new
end
end
private
def quote_params
params.require(:quote).permit(:gla, :prev_cover, :co_name, :postcode, :industry, :lives_overseas,
:scheme_start_date, :payment_frequency, :commision_level)
end
end
quote.rb model
class Quote < ApplicationRecord
validates :gla, presence: { message: "Must be selected" }
enum industry: [ :financial_services, :architect, :business_consultancy ]
enum payment_frequency: [ :annually, :monthly ]
end
schema.rb
create_table "quotes", force: :cascade do |t|
t.boolean "prev_cover"
t.string "co_name"
t.integer "co_number"
t.string "postcode"
t.string "industry"
t.boolean "lives_overseas"
t.date "scheme_start_date"
t.string "payment_frequency"
t.integer "commission_level"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "gla"
end
rails console:
Pry> Quote.last.attributes
Quote Load (0.4ms) SELECT "quotes".* FROM "quotes" ORDER BY "quotes"."id" DESC LIMIT $1 [["LIMIT", 1]]
=> {"id"=>6,
"prev_cover"=>true,
"co_name"=>"test1",
"co_number"=>nil,
"postcode"=>"al1 1aa",
"industry"=>nil,
"lives_overseas"=>true,
"scheme_start_date"=>Wed, 31 May 2017,
"payment_frequency"=>nil,
"commission_level"=>nil,
"created_at"=>Wed, 31 May 2017 19:23:07 UTC +00:00,
"updated_at"=>Wed, 31 May 2017 19:23:07 UTC +00:00,
"gla"=>true}
Stack Trace:
Started POST "/quotes" for 127.0.0.1 at 2017-05-31 21:04:37 +0100
Processing by QuotesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ILAo0Bs9Wq9lrVPlM2e6+a1kioV9zbni9Uxd5Yt/QSLNY3aVWyJ4TsEUmXN62RWgbueHksr/yN6avwEm8v7bEQ==", "quote"=>{"gla"=>"1", "prev_cover"=>"true", "co_name"=>"testing1", "co_number"=>"123456", "postcode"=>"al1 1aa", "industry"=>"", "lives_overseas"=>"true", "scheme_start_date(1i)"=>"2017", "scheme_start_date(2i)"=>"5", "scheme_start_date(3i)"=>"31", "payment_frequency"=>"", "commission_level"=>"10"}, "commit"=>"Get quote"}
Unpermitted parameters: co_number, commission_level
(0.1ms) BEGIN
SQL (0.2ms) INSERT INTO "quotes" ("prev_cover", "co_name", "postcode", "lives_overseas", "scheme_start_date", "created_at", "updated_at", "gla") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["prev_cover", "t"], ["co_name", "testing1"], ["postcode", "al1 1aa"], ["lives_overseas", "t"], ["scheme_start_date", "2017-05-31"], ["created_at", "2017-05-31 20:04:37.489368"], ["updated_at", "2017-05-31 20:04:37.489368"], ["gla", "t"]]
(0.3ms) COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 3ms (ActiveRecord: 0.6ms)
Started GET "/" for 127.0.0.1 at 2017-05-31 21:04:37 +0100
Processing by QuotesController#new as HTML
Rendering quotes/new.html.erb within layouts/application
Rendered quotes/new.html.erb within layouts/application (9.3ms)
Completed 200 OK in 34ms (Views: 32.7ms | ActiveRecord: 0.0ms)
From Rails console.
[1] pry(main)> Quote.last
Quote Load (0.2ms) SELECT "quotes".* FROM "quotes" ORDER BY "quotes"."id" DESC LIMIT $1 [["LIMIT", 1]]
=> #<Quote:0x007f951b14e918
id: 7,
prev_cover: true,
co_name: "testing1",
co_number: nil,
postcode: "al1 1aa",
industry: nil,
lives_overseas: true,
scheme_start_date: Wed, 31 May 2017,
payment_frequency: nil,
commission_level: nil,
created_at: Wed, 31 May 2017 20:04:37 UTC +00:00,
updated_at: Wed, 31 May 2017 20:04:37 UTC +00:00,
gla: true>
Ok stack trace and #toddmetheny help me sort out the missing or typo'd permitted attrs. Now just the enums Quote.industries and Quote.payment_frequencies whose values aren't getting saved.
Ok, so code changed to;
And this sends the attrs from the form, but they still don't get created in the db, stack trace:
Started POST "/quotes" for 127.0.0.1 at 2017-05-31 21:39:58 +0100
Processing by QuotesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"rkgX7CwrEHS/KnqG1C77mYkCiCEOWGshTxMCsbtGPdjGiDP20J4ccrAgplAGuKrdJyhECRWrsmXI0Ee9GNa6Zw==", "quote"=>{"gla"=>"1", "prev_cover"=>"true", "co_name"=>"halejulia", "co_number"=>"134532", "postcode"=>"al1 1aa", "industry"=>"financial_services", "lives_overseas"=>"true", "scheme_start_date(1i)"=>"2017", "scheme_start_date(2i)"=>"5", "scheme_start_date(3i)"=>"31", "payment_frequency"=>"monthly", "commission_level"=>"10"}, "commit"=>"Get quote"}
(0.1ms) BEGIN
SQL (0.3ms) INSERT INTO "quotes" ("prev_cover", "co_name", "co_number", "postcode", "industry", "lives_overseas", "scheme_start_date", "payment_frequency", "commission_level", "created_at", "updated_at", "gla") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id" [["prev_cover", "t"], ["co_name", "halejulia"], ["co_number", 134532], ["postcode", "al1 1aa"], ["industry", 0], ["lives_overseas", "t"], ["scheme_start_date", "2017-05-31"], ["payment_frequency", 1], ["commission_level", 10], ["created_at", "2017-05-31 20:39:58.957674"], ["updated_at", "2017-05-31 20:39:58.957674"], ["gla", "t"]]
(0.3ms) COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 3ms (ActiveRecord: 0.7ms)
rails console :
Quote.last.payment_frequency
Quote Load (0.4ms) SELECT "quotes".* FROM "quotes" ORDER BY "quotes"."id" DESC LIMIT $1 [["LIMIT", 1]]
=> nil
So still the params aren't being persisted in the db, they're permitted params! any ideas?
Strange, a psql select * from .. query shows that the values have been saved, yet Rails console; Quote.last.payment_frequency returns nil???
Aha, i see that for a enum the column type should be integer, yet it is string in my model, could this be the issue perhaps?
Data type of enum'd attrs changed to integer and all behaves as expected.
Post the stack trace. co_number isn't whitelisted in the permitted params. So that's at least part of the issue with that particular field. The others are...but post what you're actually seeing in the logs so we can see what's being passed through the form. There will also be messages in the stack trace that give you clues as to why those values aren't saving.
Update: the stack trace lists 2 unpermitted parameters: co_number and commission_level (you have a typo permitting commission level and co_number isn't there)
A couple things have blank values, too...like payment_frequency and industry...I'd dig into why those things are blank if they shouldn't be. Does the form have values for those things? They aren't being passed. That seems to account for the rest of your nil values.

Rails app Carrierwave uploads work on development but stop working on production

My uploads work perfectly on my local machine, but fail to work on production server. Code is identical, there are no unsubmitted commits etc.
Here is what happens on my local machine:
Started POST "/admin/projects" for 127.0.0.1 at 2015-10-08 17:17:11 +0300
Processing by Admin::ProjectsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"BLD9208mLcwkXDjyqYbf7sFBs51mgrGAGZYFIi2VT+jstdj0FdC0PB+DmrX3epz7m7UU5LXmN+961/9EN7ExQA==", "projectphotos"=>[#<ActionDispatch::Http::UploadedFile:0x007fbffb8133d8 #tempfile=#<Tempfile:/var/folders/dz/tf2gsysx18n_cj_m1xqbpyw80000gn/T/RackMultipart20151008-24531-w7z0b4.jpg>, #original_filename="_MG_9839.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"projectphotos[]\"; filename=\"_MG_9839.jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x007fbffb813360 #tempfile=#<Tempfile:/var/folders/dz/tf2gsysx18n_cj_m1xqbpyw80000gn/T/RackMultipart20151008-24531-1f7lkwr.jpg>, #original_filename="_MG_9847.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"projectphotos[]\"; filename=\"_MG_9847.jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x007fbffb8132e8 #tempfile=#<Tempfile:/var/folders/dz/tf2gsysx18n_cj_m1xqbpyw80000gn/T/RackMultipart20151008-24531-1u4h5f2.jpg>, #original_filename="_MG_9857.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"projectphotos[]\"; filename=\"_MG_9857.jpg\"\r\nContent-Type: image/jpeg\r\n">], "project"=>{"title"=>"Тестик", "desctitle"=>"Проектик", "description"=>"пупик", "teammember_ids"=>["6", ""]}, "commit"=>"Сохранить проект"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Teammember Load (0.2ms) SELECT "teammembers".* FROM "teammembers" WHERE "teammembers"."id" = $1 LIMIT 1 [["id", 6]]
(0.1ms) BEGIN
SQL (1.8ms) INSERT INTO "projects" ("title", "description", "desctitle", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["title", "Тестик"], ["description", "пупик"], ["desctitle", "Проектик"], ["created_at", "2015-10-08 14:17:11.018140"], ["updated_at", "2015-10-08 14:17:11.018140"]]
SQL (1.4ms) INSERT INTO "participations" ("teammember_id", "project_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["teammember_id", 6], ["project_id", 51], ["created_at", "2015-10-08 14:17:11.021601"], ["updated_at", "2015-10-08 14:17:11.021601"]]
(1.4ms) COMMIT
(0.1ms) BEGIN
SQL (3.4ms) INSERT INTO "projectphotos" ("image", "project_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["image", "_MG_9839.jpg"], ["project_id", 51], ["created_at", "2015-10-08 14:17:12.074996"], ["updated_at", "2015-10-08 14:17:12.074996"]]
(0.5ms) COMMIT
(0.1ms) BEGIN
SQL (0.4ms) INSERT INTO "projectphotos" ("image", "project_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["image", "_MG_9847.jpg"], ["project_id", 51], ["created_at", "2015-10-08 14:17:12.914800"], ["updated_at", "2015-10-08 14:17:12.914800"]]
(0.4ms) COMMIT
(0.1ms) BEGIN
SQL (0.4ms) INSERT INTO "projectphotos" ("image", "project_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["image", "_MG_9857.jpg"], ["project_id", 51], ["created_at", "2015-10-08 14:17:13.759695"], ["updated_at", "2015-10-08 14:17:13.759695"]]
(0.4ms) COMMIT
Redirected to http://localhost:9292/admin/projects
Completed 302 Found in 2780ms (ActiveRecord: 11.4ms)
Just perfect, images being uploaded and so on.
Now, the exact same operation on production server:
I, [2015-10-08T10:23:50.330774 #13367] INFO -- : Started POST "/admin/projects" for 89.169.175.47 at 2015-10-08 10:23:50 -0400
I, [2015-10-08T10:23:50.334229 #13367] INFO -- : Processing by Admin::ProjectsController#create as HTML
I, [2015-10-08T10:23:50.334437 #13367] INFO -- : Parameters: {"utf8"=>"✓", "authenticity_token"=>"2hx8xJdCrvkmwkfKGI4Y/gzu3bc/hPWNsrWgtNo/97kWFWKauYOrWh6v15mHQ/28sqaaAb08hEhHrkuvFjmxPw==", "projectphotos"=>[#<ActionDispatch::Http::UploadedFile:0x007f6d9006fc98 #tempfile=#<Tempfile:/tmp/RackMultipart20151008-13367-18ri23t.jpg>, #original_filename="_MG_9839.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"projectphotos[]\"; filename=\"_MG_9839.jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x007f6d9006fc20 #tempfile=#<Tempfile:/tmp/RackMultipart20151008-13367-yrlhkm.jpg>, #original_filename="_MG_9847.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"projectphotos[]\"; filename=\"_MG_9847.jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x007f6d9006fba8 #tempfile=#<Tempfile:/tmp/RackMultipart20151008-13367-1l2x4tu.jpg>, #original_filename="_MG_9857.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"projectphotos[]\"; filename=\"_MG_9857.jpg\"\r\nContent-Type: image/jpeg\r\n">], "project"=>{"title"=>"Тестовый проект", "desctitle"=>"Тестик", "description"=>"Тестовый проект", "teammember_ids"=>[""]}, "commit"=>"Сохранить проект"}
D, [2015-10-08T10:23:50.337997 #13367] DEBUG -- : User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
D, [2015-10-08T10:23:50.343076 #13367] DEBUG -- : (0.1ms) BEGIN
D, [2015-10-08T10:23:50.346752 #13367] DEBUG -- : SQL (1.2ms) INSERT INTO "projects" ("title", "description", "desctitle", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["title", "Тестовый проект"], ["description", "Тестовый проект"], ["desctitle", "Тестик"], ["created_at", "2015-10-08 14:23:50.343540"], ["updated_at", "2015-10-08 14:23:50.343540"]]
D, [2015-10-08T10:23:50.348048 #13367] DEBUG -- : (0.7ms) COMMIT
D, [2015-10-08T10:23:50.349034 #13367] DEBUG -- : (0.2ms) BEGIN
D, [2015-10-08T10:23:50.373831 #13367] DEBUG -- : (0.3ms) COMMIT
D, [2015-10-08T10:23:50.374391 #13367] DEBUG -- : (0.2ms) BEGIN
D, [2015-10-08T10:23:50.384639 #13367] DEBUG -- : (0.2ms) COMMIT
D, [2015-10-08T10:23:50.385026 #13367] DEBUG -- : (0.2ms) BEGIN
D, [2015-10-08T10:23:50.392234 #13367] DEBUG -- : (0.2ms) COMMIT
I, [2015-10-08T10:23:50.393967 #13367] INFO -- : Redirected to http://46.101.161.208/admin/projects
I, [2015-10-08T10:23:50.394322 #13367] INFO -- : Completed 302 Found in 60ms (ActiveRecord: 6.2ms)
Images are in the params hash, but are not inserted into database. Could someone hint me into the right direction?
Here is my projects_controller.rb:
def update
if #project.update(project_params)
if params[:projectphotos]
params[:projectphotos].each { |image| #project.projectphotos.create(image: image) }
end
redirect_to admin_projects_path
flash[:success] = "Updated"
end
end
def edit
end
def show
end
def create
#project = Project.new(project_params)
if #project.save
if params[:projectphotos]
params[:projectphotos].each { |image| #project.projectphotos.create(image: image) }
end
redirect_to admin_projects_path
flash[:success] = 'Created'
else
flash[:alert] = 'Something went wrong'
render 'new'
end
end
This exact same code already worked on production some time afo, but I think at some point, after I added ImageMagick for resizing it stopped working. I switched to MiniMagick and while on other projects resizing and image uploading works well with MiniMagick, here I have this issue on production. To be honest, this is why I don't believe, that the issue is caused by MiniMagick, just wanted to share this info.
Update:
As requested, here is my uploader code:
# encoding: utf-8
class ImageUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Create different versions of your uploaded files:
# version :thumb do
# process :resize_to_fit => [50, 50]
# end
version :thumb_next do
process :resize_to_fill => [600, 460]
end
version :main_image do
process :resize_to_fill => [1170, 700]
end
version :thumb_small do
process :resize_to_fit => [50,50]
end
version :thumb_medium do
process :resize_to_fit => [350,350]
end
end
Update 2:
This is getting even more ridiculous, I added some simple outputs to debug my production code:
projects_controller.rb
def update
if #project.update(project_params)
if params[:projectphotos]
p "inside params"
params[:projectphotos].each do |image|
p image
p "inside of each image param"
if #project.projectphotos.create(image: image)
p "created image"
end
end
end
redirect_to admin_projects_path
flash[:success] = "Updated"
end
end
With that update I get this on production:
deploy#rails:~$ cat apps/furnitureapp/current/log/puma.error.log
=== puma startup: 2015-10-12 05:23:35 -0400 ===
* Inherited unix:///home/deploy/apps/furnitureapp/shared/tmp/sockets/furnitureapp-puma.sock
"inside params"
#<ActionDispatch::Http::UploadedFile:0x007f6e08304828 #tempfile=#<Tempfile:/tmp/RackMultipart20151012-26945-1wzj8kl.jpg>, #original_filename="American-Psycho-blood.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"projectphotos[]\"; filename=\"American-Psycho-blood.jpg\"\r\nContent-Type: image/jpeg\r\n">
"inside of each image param"
"created image"
#<ActionDispatch::Http::UploadedFile:0x007f6e083047b0 #tempfile=#<Tempfile:/tmp/RackMultipart20151012-26945-ahjgw2.jpg>, #original_filename="i-love-sales.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"projectphotos[]\"; filename=\"i-love-sales.jpg\"\r\nContent-Type: image/jpeg\r\n">
"inside of each image param"
"created image"
But still no images and still no INSERT sql-queries! Projectphoto.all shows no entries, although it clearly should have created an image.
Ok, apparently there was no imagemagick installed on my production server.
Thanks to this article - https://chuanhesmile.wordpress.com/2014/12/13/issue-fixed-rollback-transaction-when-uploading-images-using-carrierwave/
The problem I had - there was no error message, nor a rollback when performing a 'create' action. Instead I rewrote my controller into using 'build' and 'save'.
def update
if #project.update(project_params)
if params[:projectphotos]
p "inside params"
params[:projectphotos].each do |image|
p image
p "inside of each image param"
#newImage = #project.projectphotos.build(image: image)
if #newImage.save
p "saved image"
end
end
end
redirect_to admin_projects_path
flash[:success] = "Обновлено"
end
end
This gave me at least a rollback.
I was able to debug the rollback thanks to this SO question how to find the cause of ActiveRecord ROLLBACK (important to save your file with a BANG - '!'). This finally led to some understandable errors:
2.2.0 :011 > #pr.save!
(1.0ms) BEGIN
(0.4ms) ROLLBACK
ActiveRecord::RecordInvalid: Validation failed: Projectphotos image Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: ImageMagick/GraphicsMagick is not installed
Maybe it helps someone.
Please check if ImageMagick/GraphicsMagick is installed

Delayed_job suddenly doesn't seem to do anything?

I have a scraper set up to use delayed_job so that it runs in the background.
class Scraper
def do_scrape
# do some scraping stuff
end
handle_asynchronously :do_scrape
end
Now I can comment out the handle_asynchronously line, open the console and run the scraper just fine. It does exactly what I expect it to do.
However, when I try to fire the scrape as a delayed job, it doesn't seem to do anything at all. Further to that, it doesn't seem to log anything important either.
Here's how my log looks from enqueueing a job to running rake jobs:work.
County Load (1.0ms) SELECT "counties".* FROM "counties" WHERE "counties"."name" = 'Fermanagh' LIMIT 1
(0.1ms) BEGIN
SQL (20.5ms) INSERT INTO "delayed_jobs" ("attempts", "created_at", "failed_at", "handler", "last_error", "locked_at", "locked_by", "priority", "run_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id" [["attempts", 0], ["created_at", Mon, 30 May 2011 21:19:25 UTC +00:00], ["failed_at", nil], ["handler", "---
# serialized object omitted for conciseness
nmethod_name: :refresh_listings_in_the_county_without_delay\nargs: []\n\n"], ["last_error", nil], ["locked_at", nil], ["locked_by", nil], ["priority", 0], ["run_at", Mon, 30 May 2011 21:19:25 UTC +00:00], ["updated_at", Mon, 30 May 2011 21:19:25 UTC +00:00]]
(0.9ms) COMMIT
Delayed::Backend::ActiveRecord::Job Load (0.4ms) SELECT "delayed_jobs".* FROM "delayed_jobs" WHERE (locked_by = 'host:David-Tuites-MacBook-Pro.local pid:7743' AND locked_at > '2011-05-30 17:19:32.116511') LIMIT 1
(0.1ms) BEGIN
SQL (0.3ms) DELETE FROM "delayed_jobs" WHERE "delayed_jobs"."id" = $1 [["id", 42]]
(0.4ms) COMMIT
As you can see, it seems to just inset a job and then delete it straight away? This scraping method should take at least a few minutes.
The worst part is, it was working perfectly last night and I can't think of a single thing I'm doing differently. I tried fixing the gem to a previous version incase it was updated recently but doesn't seem to have fixed the problem.
Any ideas?
Have you configured your delayed job to delete failed jobs? Look for the following setting in your initializer:
Delayed::Worker.destroy_failed_jobs = true
If yes then set it to false and look into the delayed_jobs table for the exception due to which it failed and debug further.

Resources