使用ruby 2.0.0p353和rails 4.0.4进行rake任务时发生内存泄漏

时间:2014-03-28 12:12:50

标签: ruby-on-rails ruby memory-leaks

我已经用Google搜索并阅读了有关该主题的其他主题,并实施了我可以收集的所有建议。但是,我仍然遇到使用太多内存的问题(600MB +)。

我的users.rake文件:

    namespace :users do
      desc "TODO"
      task sync_events: :environment do

        User.find_in_batches(batch_size: 5) do |users|
         users.each(&:sync_events)
        end
      end

    end

user.rb模型中的sync_events方法:

def sync_events

  unless  self.username.empty?


     # Make the request to swagger
    request = RestClient.get 'https://****************/event/findByUser/' + self.username + '?fromDate=' + 1.days.ago.strftime('%Y-%m-%d')  + '&toDate=' + 10.days.from_now.strftime('%Y-%m-%d')

    json = JSON.parse request

    events = json['events']


    # We need to fetch the events from the DB or create them if they don't exists
    events.each do |event|

        # Find existing semester or create new
        semester_in_db = Semester.where(:name => event['semester'][0]['name']).first_or_create!

        # Find exisiting event, or create new one.
        event_in_db = Event.where(:ext_id => event['id']).first_or_create!(
            :title => event['title'], 
            :description => event['description'],
            :begin => Time.parse(event['begin']),
            :end => Time.parse(event['end']),
            :semester_id => semester_in_db.id
            )


        # Check if lectureres/users exist, or create new, and add them to the event
        event['lecturer'].each do |lecturer|

          # We only want to do this for users with emails - valid users
          if lecturer["email"].length > 5

            user = User.where(:username => lecturer['username']).first_or_create!(
                :email => lecturer['username'] + '@feil.no', 
                  :altEmail => lecturer['email'],
                :password => 'password', 
                :password_confirmation => 'password',
                :first_name => lecturer['firstname'],
                :last_name => lecturer['lastname'],
                  :identity =>  lecturer['firstname'] + ' ' +  lecturer['lastname']
                )
            #connect lecturer to event
            event_in_db.users << user unless event_in_db.user_ids.include?(user.id)

            #Add role
            user.add_role :lecturer, event_in_db

            #try to sync email, because sometimes email is not formatted correctly
            begin
              user.email = lecturer['email']
              user.save!
            rescue
              e = Error.new
              e.body =  lecturer['email']
              e.save
            end

          end

        end

        # Added student user
        event_in_db.users << self unless event_in_db.user_ids.include?(self.id)

        # add role student, unless lecturer
        self.add_role :student, event_in_db unless self.has_role? :lecturer, event_in_db

      end

    end

  end

您可能会发现我不是经过培训的红宝石开发人员。这段代码正在进行一些重大的重构。但是,与此同时,我无法理解为什么它会占据我的全部记忆。我在DB中拥有大约500多名用户。最终,我的佣金任务被杀死了。

如您所见,我已经使用了find_in_batches而不是全部。我还将所有实例变量转换为局部变量。

我还能做些什么?

0 个答案:

没有答案