Umeumeume
Umeumeume

Reputation: 1988

Saving multiple objects in rails activerecord

I have a recurring job with Rails + Heroku scheduler.rake.

The problem is that it won't save them successfully when it has too many entries.

How can I change this code to make all save completed without error?

This method doesn't need to complete quickly since it will work in the fixed time in the morning, so I am ok to save it one by one with some delay.

(I am using Heroku web and worker each 1x, 25$/month)

Thanks!

*Edited with error logs 2017/02/08

class RecurringEntryJob < ActiveJob::Base
  queue_as :default

  def perform(*args)

    # Get active recurring entries
    recurring_entries = Recurring.where(deleted: false)

    Rails.logger.info "Recurring_Entry_Called"

    for recurring_entry in recurring_entries

        Rails.logger.info "“Recurring_Entry Processing for - #{recurring_entry[:id]}"

        # Get the current time with saved timezone
        timezone        = recurring_entry["timezone"]
        current_time    = Time.now.in_time_zone(timezone)

        setDate = recurring_entry["date"]

        # Check if daily is set
        if setDate  == 0 || 

           # Checkc if weekday matches
           setDate  == current_time.wday + 1 ||

           # Check if monthly date or end of month match
           setDate  == current_time.day + 7 || current_time.tomorrow.day == 1 && setDate == 36

           Rails.logger.info "Recurring_Entry Day of the week check passed for - #{recurring_entry[:id]}"

            # Save an entry at AM4:00
            if current_time.hour == 4

                Rails.logger.info "Recurring_Entry All cheks passed for - #{recurring_entry[:id]} - creating entry"

                # Create entry
                entry             = Entry.new

                entry.user        = User.find(recurring_entry["user_id"])
                entry.reason      = Reason.find_by!(uuid: recurring_entry["reason_uuid"])
                entry.account     = Account.find_by!(uuid: recurring_entry["account_uuid"])

                entry.uuid              = SecureRandom.uuid
                entry.memo              = recurring_entry["memo"]
                entry.date              = current_time
                entry.price             = recurring_entry["price"]
                entry.is_expense        = recurring_entry["is_expense"]
                entry.updated_mobile    = current_time
                entry.reason_uuid       = recurring_entry["reason_uuid"]
                entry.account_uuid      = recurring_entry["account_uuid"]
                entry.project_uuid      = recurring_entry["project_uuid"]

                entry.save!
            end
        end
    end
  end
end

Error logs

Feb 08 04:10:51 taxnote heroku/scheduler.5289:  Starting process with command `bundle exec rake recurring_entry` 
Feb 08 04:10:51 taxnote heroku/scheduler.5289:  State changed from starting to up 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] Performing RecurringEntryJob from Inline(default) 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] Recurring_Entry_Called 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] “Recurring_Entry Processing for - 102 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] “Recurring_Entry Processing for - 88 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] “Recurring_Entry Processing for - 89 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] “Recurring_Entry Processing for - 92 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] “Recurring_Entry Processing for - 93 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] “Recurring_Entry Processing for - 94 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] “Recurring_Entry Processing for - 95 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] “Recurring_Entry Processing for - 96 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] “Recurring_Entry Processing for - 98 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] “Recurring_Entry Processing for - 97 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] “Recurring_Entry Processing for - 99 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] “Recurring_Entry Processing for - 103 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] “Recurring_Entry Processing for - 104 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] “Recurring_Entry Processing for - 105 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] “Recurring_Entry Processing for - 106 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] Recurring_Entry Day of the week check passed for - 106 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] Recurring_Entry All cheks passed for - 106 - creating entry 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] “Recurring_Entry Processing for - 107 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] “Recurring_Entry Processing for - 108 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] “Recurring_Entry Processing for - 109 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] “Recurring_Entry Processing for - 110 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] “Recurring_Entry Processing for - 111 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] “Recurring_Entry Processing for - 112 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] “Recurring_Entry Processing for - 113 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] “Recurring_Entry Processing for - 115 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] “Recurring_Entry Processing for - 114 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] “Recurring_Entry Processing for - 87 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] “Recurring_Entry Processing for - 138 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] “Recurring_Entry Processing for - 117 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] “Recurring_Entry Processing for - 118 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] Recurring_Entry Day of the week check passed for - 118 
Feb 08 04:10:57 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] Recurring_Entry All cheks passed for - 118 - creating entry 
Feb 08 04:10:58 taxnote app/scheduler.5289:  [ActiveJob] [RecurringEntryJob] [88810de1-fffb-4e16-b8f8-17712bc5b084] Performed RecurringEntryJob from Inline(default) in 260.25ms 
Feb 08 04:10:58 taxnote app/scheduler.5289:  rake aborted! 
Feb 08 04:10:58 taxnote app/scheduler.5289:  ActiveRecord::RecordNotFound: Couldn't find Account 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.6/lib/active_record/core.rb:196:in `find_by!' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/app/jobs/recurring_entry_job.rb:42:in `block in perform' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.6/lib/active_record/relation/delegation.rb:46:in `each' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.6/lib/active_record/relation/delegation.rb:46:in `each' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/app/jobs/recurring_entry_job.rb:11:in `perform' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activejob-4.2.6/lib/active_job/execution.rb:32:in `block in perform_now' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:117:in `call' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:555:in `block (2 levels) in compile' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:505:in `call' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:498:in `block (2 levels) in around' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:343:in `block (2 levels) in simple' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/i18n-0.7.0/lib/i18n.rb:257:in `with_locale' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activejob-4.2.6/lib/active_job/translation.rb:7:in `block (2 levels) in <module:Translation>' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:441:in `instance_exec' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:441:in `block in make_lambda' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:342:in `block in simple' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:497:in `block in around' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:505:in `call' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:498:in `block (2 levels) in around' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:343:in `block (2 levels) in simple' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activejob-4.2.6/lib/active_job/logging.rb:23:in `block (4 levels) in <module:Logging>' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/notifications.rb:164:in `block in instrument' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/notifications/instrumenter.rb:20:in `instrument' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/notifications.rb:164:in `instrument' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activejob-4.2.6/lib/active_job/logging.rb:22:in `block (3 levels) in <module:Logging>' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activejob-4.2.6/lib/active_job/logging.rb:43:in `block in tag_logger' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/tagged_logging.rb:68:in `block in tagged' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/tagged_logging.rb:26:in `tagged' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/tagged_logging.rb:68:in `tagged' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activejob-4.2.6/lib/active_job/logging.rb:43:in `tag_logger' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activejob-4.2.6/lib/active_job/logging.rb:19:in `block (2 levels) in <module:Logging>' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:441:in `instance_exec' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:441:in `block in make_lambda' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:342:in `block in simple' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:497:in `block in around' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:505:in `call' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:92:in `__run_callbacks__' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:778:in `_run_perform_callbacks' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:81:in `run_callbacks' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activejob-4.2.6/lib/active_job/execution.rb:31:in `perform_now' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activejob-4.2.6/lib/active_job/execution.rb:21:in `execute' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activejob-4.2.6/lib/active_job/queue_adapters/inline_adapter.rb:14:in `enqueue' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activejob-4.2.6/lib/active_job/enqueuing.rb:71:in `block in enqueue' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:117:in `call' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:555:in `block (2 levels) in compile' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:505:in `call' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:498:in `block (2 levels) in around' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:343:in `block (2 levels) in simple' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activejob-4.2.6/lib/active_job/logging.rb:14:in `block (3 levels) in <module:Logging>' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activejob-4.2.6/lib/active_job/logging.rb:43:in `block in tag_logger' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/tagged_logging.rb:68:in `block in tagged' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/tagged_logging.rb:26:in `tagged' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/tagged_logging.rb:68:in `tagged' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activejob-4.2.6/lib/active_job/logging.rb:43:in `tag_logger' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activejob-4.2.6/lib/active_job/logging.rb:13:in `block (2 levels) in <module:Logging>' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:441:in `instance_exec' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:441:in `block in make_lambda' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:342:in `block in simple' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:497:in `block in around' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:505:in `call' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:92:in `__run_callbacks__' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:778:in `_run_enqueue_callbacks' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:81:in `run_callbacks' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activejob-4.2.6/lib/active_job/enqueuing.rb:67:in `enqueue' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/activejob-4.2.6/lib/active_job/enqueuing.rb:17:in `perform_later' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/lib/tasks/scheduler.rake:4:in `block in <top (required)>' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/bugsnag-4.0.2/lib/bugsnag/rake.rb:12:in `execute_with_bugsnag' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/rake-11.2.2/exe/rake:27:in `<top (required)>' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/bundler-1.13.7/lib/bundler/cli/exec.rb:74:in `load' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/bundler-1.13.7/lib/bundler/cli/exec.rb:74:in `kernel_load' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/bundler-1.13.7/lib/bundler/cli/exec.rb:27:in `run' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/bundler-1.13.7/lib/bundler/cli.rb:332:in `exec' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/bundler-1.13.7/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/bundler-1.13.7/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/bundler-1.13.7/lib/bundler/vendor/thor/lib/thor.rb:359:in `dispatch' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/bundler-1.13.7/lib/bundler/cli.rb:20:in `dispatch' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/bundler-1.13.7/lib/bundler/vendor/thor/lib/thor/base.rb:440:in `start' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/bundler-1.13.7/lib/bundler/cli.rb:11:in `start' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/bundler-1.13.7/exe/bundle:34:in `block in <top (required)>' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/bundler-1.13.7/lib/bundler/friendly_errors.rb:100:in `with_friendly_errors' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/vendor/bundle/ruby/2.3.0/gems/bundler-1.13.7/exe/bundle:26:in `<top (required)>' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/bin/bundle:3:in `load' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  /app/bin/bundle:3:in `<main>' 
Feb 08 04:10:58 taxnote app/scheduler.5289:  Tasks: TOP => recurring_entry 
Feb 08 04:10:58 taxnote app/scheduler.5289:  (See full trace by running task with --trace) 
Feb 08 04:10:58 taxnote heroku/scheduler.5289:  State changed from up to complete 
Feb 08 04:10:58 taxnote heroku/scheduler.5289:  Process exited with status 1 

Upvotes: 0

Views: 64

Answers (2)

Umeumeume
Umeumeume

Reputation: 1988

I could fix this issue by checking the error logs, it was because when account part was not founded it stopped the loop.

class RecurringEntryJob < ActiveJob::Base
  queue_as :default

  def perform(*args)

    # Get active recurring entries
    recurring_entries = Recurring.where(deleted: false)

    Rails.logger.info "Recurring_Entry_Called"

    for recurring_entry in recurring_entries

        # Rails.logger.info "“Recurring_Entry Processing for - #{recurring_entry[:id]}"

        # Get the current time with saved timezone
        timezone        = recurring_entry["timezone"]
        current_time    = Time.now.in_time_zone(timezone)

        setDate = recurring_entry["date"]

        # Check if daily is set
        if setDate  == 0 || 

           # Checkc if weekday matches
           setDate  == current_time.wday + 1 ||

           # Check if monthly date or end of month match
           setDate  == current_time.day + 7 || current_time.tomorrow.day == 1 && setDate == 36

           # Rails.logger.info "Recurring_Entry date check passed for - #{recurring_entry[:id]}"

            # Save an entry at AM4:00
            if current_time.hour == 4

                Rails.logger.info "Recurring_Entry time check passed for - #{recurring_entry[:id]} - creating entry"

                # Create entry
                entry             = Entry.new
                entry.user        = User.find(recurring_entry["user_id"])

                if entry.user.blank?
                    Rails.logger.info "User Blank for - #{recurring_entry[:id]}"
                    next
                end

                entry.reason      = Reason.find_by(uuid: recurring_entry["reason_uuid"])

                if entry.reason.blank? 
                    Rails.logger.info "Reason Blank for - #{recurring_entry[:id]}"
                    next
                end

                entry.account     = Account.find_by(uuid: recurring_entry["account_uuid"])

                if entry.account.blank? 
                    Rails.logger.info "Account Blank for - #{recurring_entry[:id]}"
                    next
                end

                entry.uuid              = SecureRandom.uuid
                entry.memo              = recurring_entry["memo"]
                entry.date              = current_time
                entry.price             = recurring_entry["price"]
                entry.is_expense        = recurring_entry["is_expense"]
                entry.updated_mobile    = current_time
                entry.reason_uuid       = recurring_entry["reason_uuid"]
                entry.account_uuid      = recurring_entry["account_uuid"]
                entry.project_uuid      = recurring_entry["project_uuid"]

                entry.save!

                Rails.logger.info "Recurring_Entry Completed for - #{recurring_entry[:id]}"
            end
        end
    end
  end
end

Upvotes: 0

michaeldever
michaeldever

Reputation: 359

What is the exact error you're seeing?

Without seeing the error you're getting, Rails is probably trying to instantiate all of the matching instances of Recurring, and this is taking more memory than you have available. An easy way to attempt to solve this problem is to use find_each rather than instantiating the full result set. This will load the matching records in batches (with a default of 1000 records, but you can configure this to suit.

Recurring.where(deleted: false).find_each do |recurring|
  # work on recurring
end

As the error that is happening is when an Account record is not found, you could add some error handling as follows to allow for that.

Recurring.where(deleted: false).find_each do |recurring|
  # work on recurring
rescue ActiveRecord::RecordNotFound => e
  Rails.logger.error("Account not found: #{recurring.account_uuid}")
  next
end

Upvotes: 1

Related Questions