Class: RCron::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/rcron/task.rb

Defined Under Namespace

Classes: TaskThread

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameObject (readonly)

Name of the task



10
11
12
# File 'lib/rcron/task.rb', line 10

def name
  @name
end

#rcronObject (readonly)

RCron scheduler for this task



7
8
9
# File 'lib/rcron/task.rb', line 7

def rcron
  @rcron
end

#scheduleObject (readonly)

Parsed cron schedule



16
17
18
# File 'lib/rcron/task.rb', line 16

def schedule
  @schedule
end

#schedule_expressionObject (readonly)

Cron schedule expression



13
14
15
# File 'lib/rcron/task.rb', line 13

def schedule_expression
  @schedule_expression
end

#timeoutObject (readonly)

Timeout for the task



19
20
21
# File 'lib/rcron/task.rb', line 19

def timeout
  @timeout
end

Instance Method Details

#deqObject Also known as: dq

Removes the task from the scheduler



56
57
58
59
# File 'lib/rcron/task.rb', line 56

def deq
  @queued = false
  nil
end

#exclusive?Boolean

Returns whether if the same task should not run simultaneously

Returns:

  • (Boolean)


45
46
47
# File 'lib/rcron/task.rb', line 45

def exclusive?
  @exclusive
end

#queued?Boolean

Returns if the task is queued to the scheduler

Returns:

  • (Boolean)


51
52
53
# File 'lib/rcron/task.rb', line 51

def queued?
  @queued
end

#runObject

Executes the task manually



27
28
29
30
31
32
33
# File 'lib/rcron/task.rb', line 27

def run
  if @block.arity >= 1
    @block.call self
  else
    @block.call
  end
end

#running?Boolean

Returns if the task is being executed by one or more threads

Returns:

  • (Boolean)


37
38
39
40
41
# File 'lib/rcron/task.rb', line 37

def running?
  @mutex.synchronize {
    return @threads.empty? == false
  }
end

#scheduled?(at) ⇒ Boolean

Returns if the task is supposed to be triggered at the given moment.

Parameters:

  • at (Time)

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rcron/task.rb', line 65

def scheduled? at
  if @previous_start.nil? || (at - at.sec).to_i > (@previous_start - @previous_start.sec).to_i
    s, m, h, day, mon, year, wd = at.to_a

    td = Date.new(year, mon, day) # at.to_date # Doesn't work with current JRuby
    wom = ((td - td.day + 1).wday + td.day - 1) / 7 + 1
    last_day = (td + 1).month > td.month

    (@schedule[:years].nil?    || @schedule[:years].has_key?(year)) &&
    (@schedule[:months].nil?   || @schedule[:months].has_key?(mon)) &&
    (@schedule[:weekdays].nil? || [true, wom].include?(@schedule[:weekdays][wd])) &&
    (@schedule[:days].nil?     || @schedule[:days].has_key?(day) || (last_day && @schedule[:days].has_key?(-1)) ) &&
    (@schedule[:hours].nil?    || @schedule[:hours].has_key?(h)) &&
    (@schedule[:minutes].nil?  || @schedule[:minutes].has_key?(m))
  else
    false
  end
end

#threadsObject

Threads running this task



22
23
24
# File 'lib/rcron/task.rb', line 22

def threads
  @mutex.synchronize { return @threads.dup }
end