Class: FiberJob::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/fiber_job/client.rb

Overview

Client handles job enqueueing and scheduling operations. Provides the core interface for adding jobs to queues with immediate, delayed, or scheduled execution.

This class is used internally by Job class methods and can also be used directly for advanced job management scenarios.

Examples:

Direct usage

FiberJob::Client.enqueue(MyJob, arg1, arg2)
FiberJob::Client.enqueue_in(3600, MyJob, arg1, arg2)  # 1 hour delay
FiberJob::Client.enqueue_at(Time.parse("2024-01-01"), MyJob, arg1)

See Also:

Author:

  • FiberJob Team

Since:

  • 1.0.0

Class Method Summary collapse

Class Method Details

.enqueue(job_class, *args) ⇒ String

Enqueues a job for immediate execution. The job will be added to the appropriate queue and processed by the next available worker.

Examples:

Enqueue a job immediately

FiberJob::Client.enqueue(EmailJob, user.id, "Welcome!")

Parameters:

  • job_class (Class)

    The job class to execute (must inherit from FiberJob::Job)

  • args (Array)

    Arguments to pass to the job’s perform method

Returns:

  • (String)

    Unique job identifier

Raises:

  • (ArgumentError)

    If job_class is not a valid job class

Since:

  • 1.0.0



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fiber_job/client.rb', line 33

def self.enqueue(job_class, *args)
  jid = JID.generate
  payload = {
    'jid' => jid,
    'class' => job_class.name,
    'args' => args,
    'enqueued_at' => Time.now.to_f
  }

  queue_name = job_class.queue
  Queue.push(queue_name, payload)

  jid
end

.enqueue_at(timestamp, job_class, *args) ⇒ String

Enqueues a job for execution at a specific time. The job will be scheduled and executed when the specified time is reached.

Examples:

Enqueue at specific time

tomorrow_9am = Time.parse("2024-01-02 09:00:00")
FiberJob::Client.enqueue_at(tomorrow_9am, DailyReportJob, date: Date.today)

# Using Unix timestamp
FiberJob::Client.enqueue_at(1672531200, MaintenanceJob, type: "cleanup")

Parameters:

  • timestamp (Time, Integer)

    Specific time or Unix timestamp for execution

  • job_class (Class)

    The job class to execute (must inherit from FiberJob::Job)

  • args (Array)

    Arguments to pass to the job’s perform method

Returns:

  • (String)

    Unique job identifier

Raises:

  • (ArgumentError)

    If timestamp is in the past or job_class is invalid

Since:

  • 1.0.0



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/fiber_job/client.rb', line 96

def self.enqueue_at(timestamp, job_class, *args)
  jid = JID.generate
  payload = {
    'jid' => jid,
    'class' => job_class.name,
    'args' => args,
    'enqueued_at' => Time.now.to_f
  }

  queue_name = job_class.queue
  Queue.schedule(queue_name, payload, timestamp.to_f)

  FiberJob.logger.info "Scheduled #{job_class.name} (#{jid}) to run at #{Time.at(timestamp)}"

  jid
end

.enqueue_in(delay_seconds, job_class, *args) ⇒ String

Enqueues a job for execution after a specified delay. The job will be scheduled for future execution and moved to the regular queue when the delay period expires.

Examples:

Enqueue with delay

FiberJob::Client.enqueue_in(300, EmailJob, user.id, "5 minute reminder")
FiberJob::Client.enqueue_in(1.hour, ReportJob, date: Date.today)

Parameters:

  • delay_seconds (Numeric)

    Number of seconds to delay execution

  • job_class (Class)

    The job class to execute (must inherit from FiberJob::Job)

  • args (Array)

    Arguments to pass to the job’s perform method

Returns:

  • (String)

    Unique job identifier

Raises:

  • (ArgumentError)

    If delay_seconds is negative or job_class is invalid

Since:

  • 1.0.0



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fiber_job/client.rb', line 62

def self.enqueue_in(delay_seconds, job_class, *args)
  jid = JID.generate
  scheduled_at = Time.now.to_f + delay_seconds
  payload = {
    'jid' => jid,
    'class' => job_class.name,
    'args' => args,
    'enqueued_at' => Time.now.to_f
  }

  queue_name = job_class.queue
  Queue.schedule(queue_name, payload, scheduled_at)

  FiberJob.logger.info "Scheduled #{job_class.name} (#{jid}) to run in #{delay_seconds}s"

  jid
end