0

I am trying to call a job in the console but always get errors.

Followed the following documentation:

Created the following file: (lib/jobs/archive_survey_jobs.rb)

module Jobs
  class ArchiveSurveysJob < ActiveJob::Base
    @queue = :setup

    def perform(survey_type)
      if SurveyTypes.all_classes.map(&:to_s).include?(survey_type)

        surveys = Survey.where(something: 'stuff')\
                        .where.not(something: 'stuff',
                                   something: 'stuff')

        surveys.each do |survey|
          do_something
        end
      end
    end
  end
end

I understand that I can do something like Resque.enqueue(ArchiveSurveysJob, 'string_here')

How can I call this in the console? If I try: Jobs::ArchiveSurveysJob.create(survey_type: 'string_here'), when I check the Resque Statuses it resulted in an error: `The task failed because of an error:

undefined local variable or method `args' for #

If I try this:

Jobs::ArchiveSurveysJob.perform(`string_here`)

Or:

Jobs::ArchiveSurveysJob.perform_now('string_here')

I get:

ArgumentError: wrong number of arguments (given 0, expected 1..2)

Please let me know if I am missing some documentation or if I am doing something wrong.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
Joao Quaresma
  • 41
  • 1
  • 7

2 Answers2

1

In your example, perform is an instance method. youre calling the class level perform which might not have any arguments. I dont know about your version of resque specifically, however you can enqueue jobs from the console like this in at least one version: Resque.enqueue(Jobs::ArchiveSurveysJob)

ps, make sure your resque workers are running. In dev environments I typically have resque running in line:

# config/initializers/resque.rb
Resque.inline = true if Rails.env.dev? || Rails.env.test?
michaelf
  • 11
  • 1
  • +1, though, it's better to use `inline` in a block-ensure as [this code (from resque source code)](https://github.com/resque/resque/blob/master/test/job_hooks_test.rb#L464-L473) – Lam Phan Aug 31 '21 at 04:09
0

How I managed to do this was by creating a Rake task and calling the Job there.

Joao Quaresma
  • 41
  • 1
  • 7