schked 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3b8b3e6f7d5d7a1db0b90f3ca5ec69ff064b79ab0923aa956a6ecde419426703
4
- data.tar.gz: 26f4ec5f80c97e1311913cc381448578b735a6b44517b1c2192cf8702e89e1c9
3
+ metadata.gz: e7a1a402bae88d8b2da45e9042700f5b5c4c47fb96a645859fa81da00dc8314d
4
+ data.tar.gz: cb6e164f3ce79f164b1f66e301b723f6b2a71e6dc899520d0f067db6c2645f54
5
5
  SHA512:
6
- metadata.gz: 0fa825e6c8ea53aa82e575542da294e92c8d0c19ecb1ef3e2419460ce99c6306c606ebe3518daf2a57d0478c5c122c74a37df8578ad7bd2231fdd1542b8263b2
7
- data.tar.gz: be98b368b66d02c8c3656ac26015e9f9a4ab841c7d9d37a87988860500208d1a1abcdbb46b25ed272342d017356aff5580c2efba20af318db0b374431b7612da
6
+ metadata.gz: 84ff673846e38e623eca53cba4d76489cd6d4ebedc2520d5c356689ccb730fa4a89333138d59fec3287036ddd9d82aa9bce3ee8e7776dd8c54cea0f0d0a79a0b
7
+ data.tar.gz: c520cbf6f29f77d321280951902bb9cb4827aff680e7d638ea9c78786a46a95001adfd741d750665e3dcfc2f7b96266ed07ea9c8185ec2d131dc9b2e007149ba
data/lib/schked/cli.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "schked"
4
3
  require "thor"
5
4
  require "shellwords"
6
5
 
@@ -30,7 +29,7 @@ module Schked
30
29
  def start
31
30
  load_requires
32
31
 
33
- Schked.start
32
+ Schked.worker.wait
34
33
  end
35
34
 
36
35
  desc "show", "Output schedule to stdout"
@@ -39,16 +38,19 @@ module Schked
39
38
  load_requires
40
39
 
41
40
  puts "====="
42
- puts Schked.schedule
41
+ puts Schked.worker.schedule
43
42
  puts "====="
44
43
  end
45
44
 
46
45
  private
47
46
 
48
47
  def load_requires
49
- return unless options[:require]&.any?
48
+ if options[:require]&.any?
49
+ options[:require].each { |file| require(File.join(Dir.pwd, file)) }
50
+ end
50
51
 
51
- options[:require].each { |file| require(File.join(Dir.pwd, file)) }
52
+ # We have to load Schked at here, because of Rails and our railtie.
53
+ require "schked"
52
54
  end
53
55
  end
54
56
  end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "logger"
4
+
5
+ module Schked
6
+ class Config
7
+ attr_writer :logger
8
+
9
+ def paths
10
+ @paths ||= []
11
+ end
12
+
13
+ def logger
14
+ @logger ||= Logger.new(STDOUT).tap { |l| l.level = Logger::INFO }
15
+ end
16
+
17
+ def register_callback(name, &block)
18
+ callbacks[name] << block
19
+ end
20
+
21
+ def fire_callback(name, *args)
22
+ callbacks[name].each do |callback|
23
+ callback.call(*args)
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def callbacks
30
+ @callbacks ||= Hash.new { |hsh, key| hsh[key] = [] }
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/railtie"
4
+
5
+ module Schked
6
+ class Railtie < Rails::Railtie
7
+ initializer "schked.paths" do |app|
8
+ Schked.config.paths << app.root.join("config", "schedule.rb")
9
+ Schked.config.logger = ::Rails.logger
10
+ end
11
+ end
12
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Schked
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rufus/scheduler"
4
+
5
+ module Schked
6
+ class Worker
7
+ def initialize(config:)
8
+ @config = config
9
+
10
+ @scheduler = Rufus::Scheduler.new
11
+
12
+ define_callbacks
13
+ load_schedule
14
+ end
15
+
16
+ def wait
17
+ scheduler.join
18
+ end
19
+
20
+ def stop
21
+ scheduler.stop
22
+ end
23
+
24
+ def schedule
25
+ config.
26
+ paths.
27
+ map { |path| File.read(path) }.
28
+ join("\n")
29
+ end
30
+
31
+ private
32
+
33
+ attr_reader :config, :scheduler
34
+
35
+ def define_callbacks
36
+ cfg = config
37
+
38
+ scheduler.define_singleton_method(:on_error) do |job, error|
39
+ cfg.logger.fatal("Task #{job.opts[:as] || job.job_id} failed with error: #{error.message}")
40
+ cfg.logger.error(error.backtrace.join("\n")) if error.backtrace
41
+
42
+ cfg.fire_callback(:on_error, job, error)
43
+ end
44
+
45
+ scheduler.define_singleton_method(:on_pre_trigger) do |job, time|
46
+ cfg.logger.info("Started task: #{job.opts[:as] || job.job_id}")
47
+
48
+ cfg.fire_callback(:before_start, job, time)
49
+ end
50
+
51
+ scheduler.define_singleton_method(:on_post_trigger) do |job, time|
52
+ cfg.logger.info("Finished task: #{job.opts[:as] || job.job_id}")
53
+
54
+ cfg.fire_callback(:after_finish, job, time)
55
+ end
56
+ end
57
+
58
+ def load_schedule
59
+ scheduler.instance_eval(schedule)
60
+ end
61
+ end
62
+ end
data/lib/schked.rb CHANGED
@@ -1,25 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "rufus/scheduler"
4
-
5
3
  require "schked/version"
4
+ require "schked/config"
5
+ require "schked/worker"
6
+ require "schked/railtie" if defined?(Rails)
6
7
 
7
8
  module Schked
8
9
  module_function
9
10
 
10
- def paths
11
- @paths ||= []
12
- end
13
-
14
- def schedule
15
- paths.
16
- map { |path| File.read(path) }.
17
- join("\n")
11
+ def config
12
+ @config ||= Config.new
18
13
  end
19
14
 
20
- def start
21
- scheduler = Rufus::Scheduler.new
22
- scheduler.instance_eval(schedule)
23
- scheduler.join
15
+ def worker
16
+ @worker ||= Worker.new(config: config)
24
17
  end
25
18
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schked
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - bibendi@evilmartians.com
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-29 00:00:00.000000000 Z
11
+ date: 2019-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rufus-scheduler
@@ -147,7 +147,10 @@ files:
147
147
  - exe/schked
148
148
  - lib/schked.rb
149
149
  - lib/schked/cli.rb
150
+ - lib/schked/config.rb
151
+ - lib/schked/railtie.rb
150
152
  - lib/schked/version.rb
153
+ - lib/schked/worker.rb
151
154
  homepage:
152
155
  licenses: []
153
156
  metadata: {}