Class: Farce::ThreadScheduler

Inherits:
Abstract::Scheduler
  • Object
show all
Includes:
Shareable::Immutable, Internal::Inspect
Defined in:
lib/farce/thread_scheduler.rb

Overview

A scheduler that starts a new thread for each scheduled task. Executes synchronous tasks inline. Creating an instance starts no threads. Blocks and arguments stay local to the caller. Tasks are not tracked, and closing this stateless scheduler has no effect. Default instances support Marshal. Instances with a custom thread factory reject dumping.

Instance Method Summary collapse

Methods included from Shareable

#ractor_shareable?

Constructor Details

#initialize(&factory) ⇒ ThreadScheduler

Returns a new instance of ThreadScheduler.



14
15
16
17
# File 'lib/farce/thread_scheduler.rb', line 14

def initialize(&factory)
  @factory = factory
  super()
end

Instance Method Details

#close ⇒ self

Returns Does not wait for or stop task threads.

Returns:

  • (self) —

    Does not wait for or stop task threads.



58
# File 'lib/farce/thread_scheduler.rb', line 58

def close = self

#closed? ⇒ false

Returns This stateless scheduler is always available.

Returns:

  • (false) —

    This stateless scheduler is always available.



61
# File 'lib/farce/thread_scheduler.rb', line 61

def closed? = false

#execute(*args, **options) {|*args| ... } ⇒ self

Runs the block on the caller's thread and fiber. Exceptions propagate to the caller.

Parameters:

  • args (Array<Object>) —

    Arguments passed to the block.

  • options (Hash) —

    Scheduler options are ignored. All tasks use local arguments.

Yields:

  • (*args) —

    The task to run.

Returns:

  • (self)

Raises:

  • (LocalJumpError)


51
52
53
54
55
# File 'lib/farce/thread_scheduler.rb', line 51

def execute(*, **)
  raise LocalJumpError, "Cannot yield without a block" unless block_given?
  yield(*)
  self
end

#local?(wait: true) ⇒ true

Parameters:

  • wait (Boolean) (defaults to: true) —

    Ignored because tasks always run in the caller's Ractor.

Returns:

  • (true)


68
# File 'lib/farce/thread_scheduler.rb', line 68

def local?(wait: true) = true # rubocop:disable Lint/UnusedMethodArgument

#schedule(*args, **options) {|*args| ... } ⇒ self

Starts a thread in the caller's Ractor, preserving the block and arguments.

Parameters:

  • args (Array<Object>) —

    Arguments passed to the block.

  • options (Hash) —

    Scheduler options are ignored. All tasks use local arguments.

Yields:

  • (*args) —

    The task to run.

Returns:

  • (self)

Raises:

  • (LocalJumpError)


38
39
40
41
42
# File 'lib/farce/thread_scheduler.rb', line 38

def schedule(*, **, &)
  raise LocalJumpError, "Cannot schedule without a block" unless block_given?
  @factory ? @factory.call(*, &) : Thread.new(*, &)
  self
end

#state ⇒ Symbol

Returns Always :running.

Returns:

  • (Symbol) —

    Always :running.



64
# File 'lib/farce/thread_scheduler.rb', line 64

def state = :running