que-scheduler 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8e54bf837ce1acc9f829cdf334cc2cc7cd95d3fd
4
- data.tar.gz: 5d0f91e0731cb89afd76d8d59879f896a56821ef
3
+ metadata.gz: 9640010d4ce493c0f9dc4e14cba6dd02a3b73b78
4
+ data.tar.gz: e30a1e6efc66e40f1946847008de970be9f23c44
5
5
  SHA512:
6
- metadata.gz: ee6bbaea0a10b325e0e81c4137fb94ba09eb8697dc59134251bb4794fc5e64381bfab040bc5e9c41c42302cdee763b90c71d96b1df47d612a32a660388812489
7
- data.tar.gz: 790f298880c4bbeb5b7b876221a1da6cab6ec061feb350cdb3e879f3148bad08d9450e3996c14ea02d2b97c349401b399419b223c1421d7aea81e223a7137dca
6
+ metadata.gz: dda04070144829a52281735bde7fce1c7ea7125a0273914524b4fa9d427ef26f17f08a6a097554b4cca9accf01dc72ee36c8b664ac173f754b59fedd597bda2a
7
+ data.tar.gz: 0e00aeedf7609aca824ce75c20ffcfd2a40786f57832c65164d921fa9a1589681d3f0755c47bfa80a1f190d5f1b6025abe254b7a11de29af2e59f9ad31a1a133
@@ -2,11 +2,9 @@ require 'fugit'
2
2
 
3
3
  module Que
4
4
  module Scheduler
5
- ScheduleParserResult = Struct.new(:missed_jobs, :schedule_dictionary, :seconds_until_next_job)
5
+ ScheduleParserResult = Struct.new(:missed_jobs, :schedule_dictionary)
6
6
 
7
7
  class ScheduleParser
8
- SCHEDULER_FREQUENCY = 60
9
-
10
8
  class << self
11
9
  def parse(jobs_list, as_time, last_time, known_jobs)
12
10
  missed_jobs = {}
@@ -25,12 +23,13 @@ module Que
25
23
  missed_jobs[desc[:clazz]] = missed unless missed.empty?
26
24
  end
27
25
 
28
- seconds_until_next_job = SCHEDULER_FREQUENCY # TODO: make it 1 sec after next known run
29
- ScheduleParserResult.new(missed_jobs, schedule_dictionary, seconds_until_next_job)
26
+ ScheduleParserResult.new(missed_jobs, schedule_dictionary)
30
27
  end
31
28
 
32
29
  private
33
30
 
31
+ # Given a job description, the last scheduler run time, and this run time, return all
32
+ # the instances that should be enqueued for that job class.
34
33
  def calculate_missed_runs(desc, last_scheduler_run_time, as_time)
35
34
  jobs_for_class = []
36
35
  missed_times = []
@@ -52,6 +51,8 @@ module Que
52
51
  jobs_for_class
53
52
  end
54
53
 
54
+ # Given a cron, and a "last time", return the next Time the event will occur, or nil if it
55
+ # is after "to".
55
56
  def next_run_time(cron, from, to)
56
57
  fugit_cron = Fugit::Cron.parse(cron)
57
58
  next_time = fugit_cron.next_time(from)
@@ -5,34 +5,49 @@ require_relative 'schedule_parser'
5
5
  module Que
6
6
  module Scheduler
7
7
  class SchedulerJob < Que::Job
8
+ SCHEDULER_FREQUENCY = 60
9
+
8
10
  # Highest possible priority.
9
11
  @priority = 0
10
12
 
11
13
  def run(last_time = nil, known_jobs = [])
12
14
  ::ActiveRecord::Base.transaction do
13
15
  last_time = last_time.nil? ? Time.now : Time.zone.parse(last_time)
14
- as_time = Time.now
16
+ as_time = Time.zone.now
15
17
 
16
18
  Que.log({ message: "que-scheduler last ran at #{last_time}." })
17
19
 
18
- result =
19
- ScheduleParser.parse(SchedulerJob.scheduler_config, as_time, last_time, known_jobs)
20
- result.missed_jobs.each do |job_class, args_arrays|
21
- args_arrays.each { |args|
22
- Que.log({ message: "que-scheduler enqueueing #{job_class} with args: #{args}" })
23
- job_class.enqueue(*args)
24
- }
25
- end
26
-
27
- SchedulerJob.enqueue(
28
- as_time,
29
- result.schedule_dictionary,
30
- run_at: as_time + result.seconds_until_next_job
31
- )
20
+ # Obtain the hash of required jobs. Keys are the job classes, and the values are arrays
21
+ # each containing more arrays for the arguments of that instance.
22
+ result = enqueue_required_jobs(last_time, as_time, known_jobs)
23
+ # And enqueue this job again.
24
+ enqueue_self_again(as_time, result.schedule_dictionary)
32
25
  destroy
33
26
  end
34
27
  end
35
28
 
29
+ private
30
+
31
+ def enqueue_required_jobs(last_time, as_time, known_jobs)
32
+ result =
33
+ ScheduleParser.parse(SchedulerJob.scheduler_config, as_time, last_time, known_jobs)
34
+ result.missed_jobs.each do |job_class, args_arrays|
35
+ args_arrays.each do |args|
36
+ Que.log({ message: "que-scheduler enqueueing #{job_class} with args: #{args}" })
37
+ job_class.enqueue(*args)
38
+ end
39
+ end
40
+ result
41
+ end
42
+
43
+ def enqueue_self_again(as_time, schedule_dictionary)
44
+ SchedulerJob.enqueue(
45
+ as_time,
46
+ schedule_dictionary,
47
+ run_at: as_time.beginning_of_minute + SCHEDULER_FREQUENCY
48
+ )
49
+ end
50
+
36
51
  class << self
37
52
  def scheduler_config
38
53
  @scheduler_config ||= begin
@@ -42,7 +57,7 @@ module Que
42
57
  end
43
58
 
44
59
  # Convert the config hash into a list of real classes and args, parsing the cron and
45
- # unmissable parameters.
60
+ # "unmissable" parameters.
46
61
  def jobs_list(schedule)
47
62
  schedule.map do |k, v|
48
63
  clazz = Object.const_get(v['class'] || k)
@@ -1,5 +1,5 @@
1
1
  module Que
2
2
  module Scheduler
3
- VERSION = '0.3.0'.freeze
3
+ VERSION = '0.4.0'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: que-scheduler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Harry Lascelles