crontab_rb 0.2.4 → 1.0.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
  SHA1:
3
- metadata.gz: ab17c3a440df6c408cb6f8370fbe53967a561873
4
- data.tar.gz: 8c5f205eca09a69fa2d84d75c96f95990fea181b
3
+ metadata.gz: 4fc1a8a4826e74c83f78925e262edc685c92f6c6
4
+ data.tar.gz: aedcc842f2bfca1aeebc93f7fe421d0cc2c3c8b7
5
5
  SHA512:
6
- metadata.gz: 37d6875b7f85d0deb0f17a5a00b3950e7add1c24147da95af9848ea70d1abf87ced7e912b27a555e2e03af275f4cbd748c4f13c785372c6de31cf5b6c1c2264b
7
- data.tar.gz: 9fba7f604be23d128146541277f9880d32f662f902ace0370e78b906133f70db92e7208a42004278a783292c77cd1eb11f95016e8fa00e1c5e8b2aa0a28bc643
6
+ metadata.gz: 9ef71931a09891e52aef70bc213b57d31517d023d09b37ac7c7782668bf0ebff323de210de7a5d85ea1a1885ef468f262e51012afff392ec3dd613c18e2480f0
7
+ data.tar.gz: d59d4e2b8290fa1e4a93c576f54f581d01044413b8ed59fe4f02296ced01295d51b8837342e0820d2c082e54ffff8f419c2b3605e9d6c96f71049f9e091fa1cf
@@ -30,8 +30,8 @@ Gem::Specification.new do |spec|
30
30
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
31
  spec.require_paths = ["lib"]
32
32
 
33
- spec.add_development_dependency "bundler", "~> 1.15"
33
+ spec.add_development_dependency 'bundler'
34
34
  spec.add_development_dependency "rake", "~> 10.0"
35
35
  spec.add_development_dependency "pry", "~> 0.12.2"
36
-
36
+ spec.add_dependency 'whenever'
37
37
  end
@@ -15,7 +15,5 @@ module CrontabRb
15
15
  end
16
16
 
17
17
  require "crontab_rb/database"
18
- require "crontab_rb/template"
19
- require "crontab_rb/parse"
20
18
  require "crontab_rb/write"
21
19
  require "crontab_rb/cron"
@@ -9,17 +9,16 @@ module CrontabRb
9
9
 
10
10
  def initialize(attributes={})
11
11
  @id = attributes[:id]
12
- @name = attributes[:name]
13
- @command = attributes[:command]
14
- @time = attributes[:time]
15
- @at = attributes[:at]
12
+ @name = attributes[:name] || ''
13
+ @command = attributes[:command] || ''
14
+ @time = attributes[:time] || ''
15
+ @at = attributes[:at] || ''
16
16
  @updated_at = attributes[:updated_at]
17
17
  end
18
18
 
19
19
  def self.create(options={})
20
20
  options = convert_to_symbol(options)
21
21
  cron = new(options)
22
- cron.validate
23
22
  record = Database.create(options)
24
23
  cron.id = record[:id]
25
24
  cron.updated_at = record[:updated_at]
@@ -47,7 +46,6 @@ module CrontabRb
47
46
  record[:time] = options[:time] || record[:time]
48
47
  record[:at] = options[:at] || record[:at]
49
48
  cron = new(record)
50
- cron.validate
51
49
  Database.new(record).save
52
50
  cron.write_crontab
53
51
  cron
@@ -62,10 +60,6 @@ module CrontabRb
62
60
  cron
63
61
  end
64
62
 
65
- def validate
66
- raise "Time attribute of crontab_rb only accept #{CrontabRb::Template::EVERY.keys.join(",")}" unless CrontabRb::Template::EVERY.keys.include?(time)
67
- end
68
-
69
63
  def self.convert_to_symbol(hash)
70
64
  Hash[hash.map{|k, v| [k.to_sym, v]}]
71
65
  end
@@ -1,3 +1,3 @@
1
1
  module CrontabRb
2
- VERSION = "0.2.4"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -1,8 +1,38 @@
1
+ require 'whenever'
1
2
  module CrontabRb
2
3
  class Write
3
- def self.write_crontab
4
- contents = CrontabRb::Parse.from_database
5
- command = "crontab -"
4
+ def self.write_crontab
5
+ command = 'crontab -'
6
+ shortcut_jobs, regular_jobs = [], []
7
+ contents = ''
8
+ records = Database.all
9
+ records.each do |record|
10
+ options = {
11
+ at: record[:at],
12
+ job_template: "/bin/bash -l -c ':job'",
13
+ template: "cd :path && :bundle_command :runner_command -e :environment ':task' :output",
14
+ environment_variable: 'RAILS_ENV',
15
+ environment: 'production',
16
+ path: Dir.pwd,
17
+ chronic_options: {},
18
+ runner_command: "bin/rails runner",
19
+ bundle_command: "bundle exec",
20
+ task: record[:command],
21
+ mailto: ''
22
+ }
23
+ job = Whenever::Job.new(options)
24
+ Whenever::Output::Cron.output(record[:time], job, chronic_options: {}) do |cron|
25
+ cron << "\n\n"
26
+ if cron[0,1] == "@"
27
+ shortcut_jobs << cron
28
+ else
29
+ regular_jobs << cron
30
+ end
31
+ end
32
+ end
33
+ contents = shortcut_jobs.join + combine(regular_jobs).join
34
+
35
+
6
36
  IO.popen(command, 'r+') do |crontab|
7
37
  crontab.write(contents)
8
38
  crontab.close_write
@@ -12,5 +42,25 @@ module CrontabRb
12
42
  puts "[write] crontab file updated"
13
43
  end
14
44
  end
45
+
46
+ def self.combine(entries)
47
+ entries.map! { |entry| entry.split(/ +/, 6) }
48
+ 0.upto(4) do |f|
49
+ (entries.length-1).downto(1) do |i|
50
+ next if entries[i][f] == '*'
51
+ comparison = entries[i][0...f] + entries[i][f+1..-1]
52
+ (i-1).downto(0) do |j|
53
+ next if entries[j][f] == '*'
54
+ if comparison == entries[j][0...f] + entries[j][f+1..-1]
55
+ entries[j][f] += ',' + entries[i][f]
56
+ entries.delete_at(i)
57
+ break
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ entries.map { |entry| entry.join(' ') }
64
+ end
15
65
  end
16
66
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crontab_rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nguyen Anh
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-09-19 00:00:00.000000000 Z
11
+ date: 2020-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.15'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.15'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: 0.12.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: whenever
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description: Easy and safe way to manage your crontab file via CRUD
56
70
  email:
57
71
  - cauut2117610@gmail.com
@@ -71,8 +85,6 @@ files:
71
85
  - lib/crontab_rb/configuration.rb
72
86
  - lib/crontab_rb/cron.rb
73
87
  - lib/crontab_rb/database.rb
74
- - lib/crontab_rb/parse.rb
75
- - lib/crontab_rb/template.rb
76
88
  - lib/crontab_rb/version.rb
77
89
  - lib/crontab_rb/write.rb
78
90
  homepage: https://github.com/Nguyenanh/crontab_rb
@@ -1,67 +0,0 @@
1
- module CrontabRb
2
- class Parse
3
- def self.from_database
4
- new.call
5
- end
6
-
7
- def initialize
8
- @template = "cd :path && bundle exec bin/rails runner -e #{Rails.env} ':command' >/dev/null 2>&1"
9
- @job_template = "/bin/bash -l -c ':job'"
10
- @path = Dir.pwd
11
- end
12
-
13
- def call
14
- records = Database.all
15
- return "\n" if records.empty?
16
- contents = []
17
- records.each do |record|
18
- options = record
19
- options[:type] = record[:time]
20
- options[:time] = Template::EVERY[options[:time]]
21
- options[:path] = @path
22
- job = process_template(@template, options)
23
- task = process_template(@job_template, options.merge(:job => job))
24
- timer = process_timer(options)
25
- contents << timer + " " + task + "\n"
26
- end
27
- contents.join("\n")
28
- end
29
-
30
- protected
31
-
32
- def process_timer(options)
33
- options[:time].gsub(/:\w+/) do |key|
34
- if options[:type].to_i/60 <= 1
35
- options[:at].to_i
36
- else
37
- t = options[:at].to_i*60
38
- Time.at(t).utc.strftime("%M %H")
39
- end
40
- end.gsub(/\s+/m, " ").strip
41
- end
42
-
43
- def process_template(template, options)
44
- template.gsub(/:\w+/) do |key|
45
- before_and_after = [$`[-1..-1], $'[0..0]]
46
- option = options[key.sub(':', '').to_sym] || key
47
-
48
- if before_and_after.all? { |c| c == "'" }
49
- escape_single_quotes(option)
50
- elsif before_and_after.all? { |c| c == '"' }
51
- escape_double_quotes(option)
52
- else
53
- option
54
- end
55
- end.gsub(/\s+/m, " ").strip
56
- end
57
-
58
- def escape_single_quotes(str)
59
- str.gsub(/'/) { "'\\''" }
60
- end
61
-
62
- def escape_double_quotes(str)
63
- str.gsub(/"/) { '\"' }
64
- end
65
-
66
- end
67
- end
@@ -1,10 +0,0 @@
1
- module CrontabRb
2
- module Template
3
- EVERY= {
4
- "1" => "*/:at * * * *",
5
- "60" => ":at * * * *",
6
- "1440" => ":at * * *",
7
- "4320" => ":at */3 * *"
8
- }
9
- end
10
- end