activejob-scheduler 0.0.1 → 1.0.0.pre

Sign up to get free protection for your applications and to get access to all the features.
data/bin/ajs DELETED
@@ -1,7 +0,0 @@
1
- #!/usr/bin/env ruby
2
- #
3
- # Easy-peasy binary goodness for the ActiveJob::Scheduler
4
-
5
- require 'active_job/scheduler'
6
-
7
- ActiveJob::Scheduler::CLI.run ARGV, ENV
@@ -1,113 +0,0 @@
1
- require 'optparse'
2
-
3
- module ActiveJob::Scheduler
4
- # Controller for the schedule via the CLI.
5
- class Cli
6
- attr_reader :env, :options
7
-
8
- # A short doc explaining the CLI.
9
- USAGE = <<-EOF.gsub(/ {6}/, '')
10
- Usage: activejob-scheduler [options]
11
-
12
- Runs an active_job scheduler process directly (rather than via rake).
13
-
14
- EOF
15
-
16
- # The various options our CLI takes.
17
- OPTIONS = [
18
- {
19
- args: ['-n', '--app-name [APP_NAME]',
20
- 'Application name for procline'],
21
- callback: ->(options) { ->(n) { options[:app_name] = n } }
22
- },
23
- {
24
- args: ['-B', '--background', 'Run in the background [BACKGROUND]'],
25
- callback: ->(options) { ->(b) { options[:background] = b } }
26
- },
27
- {
28
- args: ['-D', '--dynamic-schedule',
29
- 'Enable dynamic scheduling [DYNAMIC_SCHEDULE]'],
30
- callback: ->(options) { ->(d) { options[:dynamic] = d } }
31
- },
32
- {
33
- args: ['-E', '--environment [RAILS_ENV]', 'Environment name'],
34
- callback: ->(options) { ->(e) { options[:env] = e } }
35
- },
36
- {
37
- args: ['-I', '--initializer-path [INITIALIZER_PATH]',
38
- 'Path to optional initializer ruby file'],
39
- callback: ->(options) { ->(i) { options[:initializer_path] = i } }
40
- },
41
- {
42
- args: ['-i', '--interval [RESQUE_SCHEDULER_INTERVAL]',
43
- 'Interval for checking if a scheduled job must run'],
44
- callback: ->(options) { ->(i) { options[:poll_sleep_amount] = i } }
45
- },
46
- {
47
- args: ['-l', '--logfile [LOGFILE]', 'Log file name'],
48
- callback: ->(options) { ->(l) { options[:logfile] = l } }
49
- },
50
- {
51
- args: ['-F', '--logformat [LOGFORMAT]', 'Log output format'],
52
- callback: ->(options) { ->(f) { options[:logformat] = f } }
53
- },
54
- {
55
- args: ['-P', '--pidfile [PIDFILE]', 'PID file name'],
56
- callback: ->(options) { ->(p) { options[:pidfile] = p } }
57
- },
58
- {
59
- args: ['-q', '--quiet', 'Run with minimal output [QUIET]'],
60
- callback: ->(options) { ->(q) { options[:quiet] = q } }
61
- },
62
- {
63
- args: ['-v', '--verbose', 'Run with verbose output [VERBOSE]'],
64
- callback: ->(options) { ->(v) { options[:verbose] = v } }
65
- }
66
- ].freeze
67
-
68
- # Instantiate a new CLI handler with the given args.
69
- def initialize(argv, env)
70
- @env = env
71
- @args = argv
72
- @options = option_parser.parse! argv
73
- end
74
-
75
- # Start the scheduler CLI immediately from given command-line
76
- # arguments.
77
- def self.run(argv, env)
78
- new(argv, env).run
79
- end
80
-
81
- # Now that we have args, actually begin running the schedule by
82
- # loading each Job into Rufus::Scheduler. Rufus uses methods like
83
- # `every` and `cron` to determine what kind of job you're pushing
84
- # into it, so we use send() to give the Job object the power to make
85
- # that choice.
86
- def run
87
- jobs.each do |job|
88
- rufus.send job.interval, *job.rufus_params
89
- end
90
-
91
- rufus.start
92
- end
93
-
94
- private
95
- def option_parser
96
- OptionParser.new do |parser|
97
- parser.banner = USAGE
98
-
99
- OPTIONS.each do |opt|
100
- parser.on(*opt[:args], &(opt[:callback].call(options)))
101
- end
102
- end
103
- end
104
-
105
- def jobs
106
- @jobs ||= Jobs.from_yaml
107
- end
108
-
109
- def rufus
110
- @rufus ||= Rufus::Scheduler.new
111
- end
112
- end
113
- end
@@ -1,32 +0,0 @@
1
- require 'yaml'
2
-
3
- module ActiveJob::Scheduler
4
- class Jobs
5
- include Enumerable
6
-
7
- attr_reader :path
8
-
9
- def initialize(from_path)
10
- @path = from_path
11
- end
12
-
13
- def self.from_yaml
14
- new "config/jobs.yml"
15
- end
16
-
17
- def each
18
- collection.each { |job| yield job }
19
- end
20
-
21
- private
22
- def collection
23
- params.keys.map do |name|
24
- Job.new params[name].merge name: name
25
- end
26
- end
27
-
28
- def params
29
- @collection ||= YAML::load_file path
30
- end
31
- end
32
- end
@@ -1,38 +0,0 @@
1
- require 'active_job/scheduler/cli'
2
- require 'rake/tasklib'
3
-
4
- module ActiveJob::Scheduler
5
- # Run the scheduler as a Rake task, and preload the Rails environment.
6
- #
7
- # Example Task:
8
- #
9
- # ActiveJob::Scheduler::Task.new :schedule
10
- #
11
- # Example Shell Command:
12
- #
13
- # rake schedule
14
- #
15
- # The task can also be pre-loaded with a task called `schedule:setup`.
16
- class Task < Rake::TaskLib
17
- attr_reader :name
18
-
19
- def initialize(with_name=:schedule)
20
- @name = with_name
21
- yield self if block_given?
22
- define
23
- end
24
-
25
- def define
26
- namespace name do
27
- task :setup
28
-
29
- task :run do
30
- ActiveJob::Scheduler::Cli.run ARGV, ENV
31
- end
32
- end
33
-
34
- desc "Run the ActiveJob::Scheduler"
35
- task name => ["#{name}:setup", "#{name}:run"]
36
- end
37
- end
38
- end
@@ -1,44 +0,0 @@
1
- require 'spec_helper'
2
- require 'active_job/scheduler/job'
3
-
4
- class Worker
5
- def perform(record)
6
- # do nothing
7
- end
8
- end
9
-
10
- module ActiveJob::Scheduler
11
- describe Job do
12
- subject do
13
- Job.new \
14
- name: 'testing',
15
- every: '30s',
16
- job_class: 'Worker'
17
- end
18
-
19
- before do
20
- allow(subject.send(:jobject)).to receive(:enqueue).and_return true
21
- end
22
-
23
- it "finds the interval" do
24
- expect(subject.interval).to eq(:every)
25
- end
26
-
27
- it "finds the interval value" do
28
- expect(subject.interval_value).to eq('30s')
29
- end
30
-
31
- it "finds the job class name" do
32
- expect(subject.job_class).to eq('Worker')
33
- end
34
-
35
- it "it finds the description or gets one set" do
36
- expect(subject.description).to eq('Testing')
37
- end
38
-
39
- it "enqueues the job with active_job" do
40
- expect(subject).to be_valid
41
- expect(subject.enqueue).to be_true
42
- end
43
- end
44
- end
@@ -1,24 +0,0 @@
1
- require 'spec_helper'
2
- require 'active_job/scheduler/jobs'
3
-
4
- module ActiveJob::Scheduler
5
- describe Jobs do
6
- let :fixture_config do
7
- File.expand_path('../../../fixtures/jobs.yml', __FILE__)
8
- end
9
-
10
- subject { Jobs.new fixture_config }
11
-
12
- it "reads from a yaml file on disk" do
13
- expect(subject.path).to eq(fixture_config)
14
- end
15
-
16
- it "defines jobs from each params stanza it sees" do
17
- expect(subject.send(:collection).first).to be_a Job
18
- end
19
-
20
- it "iterates over the collection as an enumerable" do
21
- expect(subject).to respond_to :each
22
- end
23
- end
24
- end
@@ -1,3 +0,0 @@
1
- example:
2
- every: '1m'
3
- job_class: 'ExampleJob'
@@ -1,4 +0,0 @@
1
- $LOAD_PATH << File.expand_path('../../lib', __FILE__)
2
-
3
- require 'bundler/setup'
4
- require 'rspec'