Module: Scheduled

Defined in:
lib/scheduled.rb,
lib/scheduled/version.rb

Defined Under Namespace

Classes: Job

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.every(interval, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/scheduled.rb', line 10

def every(interval, &block)
  if interval.is_a?(Integer)
    task = Concurrent::TimerTask.new(execution_interval: interval, run_now: true) do
      block.call
    end

    task.execute

  elsif interval.is_a?(String)
    run = ->() {
      parsed_cron = CronParser.new(interval)
      next_tick_delay = parsed_cron.next(Time.now) - Time.now

      task = Concurrent::ScheduledTask.execute(next_tick_delay) do
        block.call
        run.call
      end

      task.execute
    }

    run.call

  elsif interval.respond_to?(:call)
    job = Job.new

    task = Concurrent::TimerTask.new(execution_interval: 1, run_now: true) do |timer_task|
      case interval.call(job)
      when true
        block.call

        job.last_run = Time.now
      when :cancel
        timer_task.shutdown
      end
    end

    task.execute
  else
    raise ArgumentError, "Unsupported value for interval"
  end
end

.waitObject



53
54
55
56
57
58
59
# File 'lib/scheduled.rb', line 53

def wait
  trap("INT") { exit }

  loop do
    sleep 1
  end
end