Commit 9b8e7db59dcfd950d2b958ae3185d09ede6be0fc

Authored by Daniela Feitosa
1 parent c05eb777

profile_suggestions: scheduling suggestions job

(ActionItem3234)
app/models/profile_suggestion.rb
... ... @@ -157,4 +157,22 @@ class ProfileSuggestion < ActiveRecord::Base
157 157 self.save
158 158 end
159 159  
  160 + def self.generate_all_profile_suggestions
  161 + Delayed::Job.enqueue(ProfileSuggestion::GenerateAllJob.new) unless ProfileSuggestion::GenerateAllJob.exists?
  162 + end
  163 +
  164 + def self.generate_profile_suggestions(person_id)
  165 + Delayed::Job.enqueue ProfileSuggestionsJob.new(person_id) unless ProfileSuggestionsJob.exists?(person_id)
  166 + end
  167 +
  168 + class GenerateAllJob
  169 + def self.exists?
  170 + Delayed::Job.by_handler("--- !ruby/object:ProfileSuggestion::GenerateAllJob {}\n").count > 0
  171 + end
  172 +
  173 + def perform
  174 + Person.find_each {|person| ProfileSuggestion.generate_profile_suggestions(person.id) }
  175 + end
  176 + end
  177 +
160 178 end
... ...
config/schedule.rb
... ... @@ -26,3 +26,7 @@ set :output, "log/cron.log"
26 26 every 1.minute do
27 27 runner "SearchTerm.calculate_scores"
28 28 end
  29 +
  30 +every 90.days do
  31 + runner "ProfileSuggestion.generate_all_profile_suggestions"
  32 +end
... ...
lib/profile_suggestions_job.rb
1 1 class ProfileSuggestionsJob < Struct.new(:person_id)
2 2  
  3 + def self.exists?(person_id)
  4 + !find(person_id).empty?
  5 + end
  6 +
  7 + def self.find(person_id)
  8 + Delayed::Job.by_handler("--- !ruby/struct:ProfileSuggestionsJob\nperson_id: #{person_id}\n")
  9 + end
  10 +
3 11 def perform
4 12 begin
5 13 person = Person.find(person_id)
... ...