# frozen_string_literal: true module Wayfarer module CLI class Job < Base desc "perform JOB URL", "Perform JOB with URL" option :mock_redis, type: :boolean option :batch, type: :string, default: SecureRandom.uuid def perform(job, url) load_environment mock_redis if options[:mock_redis] url = URI(url) job = job.classify.constantize.new task = Wayfarer::Task.new(url, "tmp") job.arguments.push(task) job.perform(task) GC.new(job).run end desc "enqueue JOB URL", "Enqueue JOB with URL" option :batch, type: :string, default: SecureRandom.uuid def enqueue(job, url) load_environment mock_redis if options[:mock_redis] url = URI(url) job = job.classify.constantize job.crawl_later(url, batch: options[:batch]) end desc "execute JOB URL", "Execute JOB with async adapter" option :mock_redis, type: :boolean option :batch, type: :string, default: SecureRandom.uuid option :min_threads, type: :numeric, default: 1 option :max_threads, type: :numeric, default: 1 def execute(job, url) load_environment mock_redis if options[:mock_redis] url = URI(url) job = job.classify.constantize job.queue_adapter = ActiveJob::QueueAdapters::AsyncAdapter.new(min_threads: options[:min_threads], max_threads: options[:max_threads]) scheduler = job.queue_adapter.instance_variable_get(:@scheduler) executor = scheduler.instance_variable_get(:@async_executor) job.crawl_later(url, batch: options[:batch]) sleep(1) while executor.scheduled_task_count > executor.completed_task_count end end end end