使用简单的Scheduler Gem在Rails应用中计划任务

时间:2019-03-15 13:28:35

标签: ruby-on-rails rails-activejob

我正在尝试运行一种将来自API调用的响应添加到Cache的方法,我决定使用simple_scheduler gem

下面是我正在运行的代码段

#  update_cache_job.rb

class UpdateCacheJob < ActiveJob::Base
  def perform
    return QueuedJobs.new.update_cache
  end
end

# simple_scheduler.yml

# Global configuration options. The `queue_ahead` and `tz` options can also be set on each task.
queue_ahead: 120 # Number of minutes to queue jobs into the future
queue_name: "default" # The Sidekiq queue name used by SimpleScheduler::FutureJob
tz: "nil" # The application time zone will be used by default if not set

# Runs once every 2 minutes
simple_task:
  class: "UpdateCacheJob"
  every: "2.minutes"

我计划每2分钟运行一次的方法

class QueuedJobs
  include VariableHelper
    def initialize; end

    def update_cache
      @variables = obtain_software_development_table
       #   First refresh the project Reviews

       puts 'Updating reviews...'

       @records = Dashboard.new.obtain_projects_reviews.pluck(
       obtain_project_reviews_student_variable,
       obtain_project_reviews_id_variable,
     'Project'
           ).map { |student, id, project| { 'Student': student, 'ID': id,   
     'Project': project } }
       Rails.cache.write(
       :reviews,
       @records,
       expires_in: 15.minutes
       )
       @grouped_reviews = Rails.cache.read(
       :reviews
       ).group_by do |review|
       review[:Student]&.first
     end

     puts 'reviews refreshed.'

    #   Then refresh the coding challenges submissions

    puts "Updating challenges submissions.."
    @all_required_submissions_columns = Dashboard.new.all_coding_challenges_submissions.all.map do |submission|
      {
        id: submission.id,
        'Student': submission[obtain_coding_chall_subm_student_var],
        'Challenge': submission[obtain_coding_chall_subm_challenge_var]
      }
    end
    @all_grouped_submissions = @all_required_submissions_columns.group_by { |challenge| challenge[:Student]&.first }
    Rails.cache.write(
      :challenges_submissions,
      @all_grouped_submissions,
      expires_in: 15.minutes
    )

    puts "challenges submissions refreshed."
    end
end

我已经能够从rails控制台访问这些方法,但是每当我运行rake simple_scheduler时,它只会记录第一个看跌期权,有时甚至什么都不做。

我在这里需要做什么?

0 个答案:

没有答案