Class: Tina4::Background::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/tina4/background.rb

Overview

Handle for one registered background task — the ONE background surface, identical across the four frameworks: a handle with a boolean stop plus a count. Mirrors Python's BackgroundTask (handle.stop()), PHP's Tina4\BackgroundTask ($handle->stop()) and Node's background() handle (handle.stop()).

It also answers [:callback]/[:interval]/[:thread]/[:running] (read AND write) so a descriptor and a handle are the same object — code that introspected the old Hash descriptor keeps working unchanged.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(callback, interval) ⇒ Task

Returns a new instance of Task.



31
32
33
34
35
36
# File 'lib/tina4/background.rb', line 31

def initialize(callback, interval)
  @callback = callback
  @interval = interval
  @thread = nil
  @running = false
end

Instance Attribute Details

#callbackObject

Returns the value of attribute callback.



29
30
31
# File 'lib/tina4/background.rb', line 29

def callback
  @callback
end

#intervalObject

Returns the value of attribute interval.



29
30
31
# File 'lib/tina4/background.rb', line 29

def interval
  @interval
end

#runningObject

Returns the value of attribute running.



29
30
31
# File 'lib/tina4/background.rb', line 29

def running
  @running
end

#threadObject

Returns the value of attribute thread.



29
30
31
# File 'lib/tina4/background.rb', line 29

def thread
  @thread
end

Instance Method Details

#[](key) ⇒ Object

Hash-style read, so task[:thread] / task[:running] still work.



58
59
60
61
62
63
64
65
# File 'lib/tina4/background.rb', line 58

def [](key)
  case key
  when :callback then @callback
  when :interval then @interval
  when :thread then @thread
  when :running then @running
  end
end

#[]=(key, value) ⇒ Object

Hash-style write, so the scheduler's task[:thread] = ... still works.



68
69
70
71
72
73
74
75
# File 'lib/tina4/background.rb', line 68

def []=(key, value)
  case key
  when :callback then @callback = value
  when :interval then @interval = value
  when :thread then @thread = value
  when :running then @running = value
  end
end

#running?Boolean

Returns true while the task is registered and ticking.

Returns:

  • (Boolean)

    true while the task is registered and ticking.



53
54
55
# File 'lib/tina4/background.rb', line 53

def running?
  @running
end

#stop(timeout: 2.0) ⇒ Boolean

Stop this task and DEREGISTER it. Idempotent — a second call is a safe no-op that returns false.

Parameters:

  • timeout (Float) (defaults to: 2.0)

    Seconds to wait for an in-flight run before killing.

Returns:

  • (Boolean)

    true if this call removed the task, false if already gone.



43
44
45
# File 'lib/tina4/background.rb', line 43

def stop(timeout: 2.0)
  Background.stop_task(self, timeout: timeout)
end

#stopped?Boolean

Returns true once stop has run (the task is no longer running).

Returns:

  • (Boolean)

    true once stop has run (the task is no longer running).



48
49
50
# File 'lib/tina4/background.rb', line 48

def stopped?
  !@running
end