mardi 21 juin 2016

how can i schedule a job at every 20 seconds using whenever gem in rails

I am trying to run a job at every 20 seconds. I am using whenever gem but as per its documentation it allows minimum 1 minute of time.

scheduler

every 1.minute do
  runner "DailyNotificationChecker.send_notifications"
end

job

i thought i can make an infinite loop and use sleep method and make it sleep for 20 seconds. that way this job will run every 20 seconds but what will happen if my cron will it this method again.

class DailyNotificationChecker 
    def self.send_notifications
        Rails.logger.info "Triggered NotificationChecker"
        if RUN_SCHEDULER == "true"
          Rails.logger.info "Running Performer"
          notes = Note.unprocessed
          if notes.present?
            notes.each do |note|
              RealtimeNotificationChecker.performer(note.user_id,"note_created")
              note_hash = {}
              note_hash[:note_id] = note.id
              url = URI.parse(PROCESS_NOTE_API_URL)
              resp, data = Net::HTTP.post_form(url, note_hash)
            end
          end
        sleep 20  #=> workaround for to sleep 20 seconds
        end
      end
end

Aucun commentaire:

Enregistrer un commentaire