diff --git a/app/jobs/entry_job.rb b/app/jobs/entry_job.rb new file mode 100644 index 00000000..8c902880 --- /dev/null +++ b/app/jobs/entry_job.rb @@ -0,0 +1,16 @@ +class EntryJob < ActiveJob::Base + queue_as :default + + retry_on RestClient::Unauthorized, wait: 5.minutes, attempts: 3 + + def perform(user_id, random_inspiration_id, sent_in_hour) + user = User.find(user_id) + random_inspiration = Inspiration.find(random_inspiration_id) + EntryMailer.send_entry(user, random_inspiration).deliver_now + rescue StandardError => e + Sentry.set_user(id: user.id, email: user.email) + Sentry.set_tags(plan: user.plan) + Sentry.capture_exception(e, extra: { sent_in_hour: sent_in_hour }) + raise + end +end diff --git a/lib/tasks/entry.rake b/lib/tasks/entry.rake index 6e5be82a..d3af2132 100644 --- a/lib/tasks/entry.rake +++ b/lib/tasks/entry.rake @@ -5,7 +5,7 @@ namespace :entry do # rake entry:send_entries_test task :send_entries_test => :environment do user = User.where(:email=>"admin@dabble.ex").first - EntryMailer.send_entry(user, Inspiration.random).deliver_now + EntryJob.perform_later(user.id, Inspiration.random.id, 0) end # TRIGGERED MANUALLY @@ -186,7 +186,7 @@ namespace :entry do if user.is_free? && user.emails_sent > 6 && user.entries.count == 0 && ENV['FREE_WEEK'] != 'true' user.update_columns(frequency: [], previous_frequency: user.frequency) elsif user.is_pro? || (user.is_free? && Time.now.strftime("%U").to_i % 2 == 0) || ENV['FREE_WEEK'] == 'true' # Every other week for free users - EntryMailer.send_entry(user, random_inspiration).deliver_now + EntryJob.perform_later(user.id, random_inspiration.id, sent_in_hour) sent_in_hour += 1 end rescue => error