crontab_rb 0.2.0 → 1.1.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: 65c278bcd55709539e1dd44c32586b83dc6675b6
4
- data.tar.gz: 6fe08b678086ca5fd6fb2e510f141919d8e96f32
3
+ metadata.gz: e783a42e7534104e29a1189d441279d752000694
4
+ data.tar.gz: '086aa3c2557aeb29b9456c9e3c3eec150bb99d20'
5
5
  SHA512:
6
- metadata.gz: baaf3561948a48335d927d85e4f811b803ae24316213da1234f1124c705ccfbe2cdf66271503974f471e3018640714523152288932f71debf06f70925cb538c6
7
- data.tar.gz: 91ee610bab6525da0843f97a51110269d8a2d70bf51c68f0f47ff9cead2d0318169dda79dbe33ce1912e89b9b5b78e8b5fe62cade25dfb96c4250bee96e84693
6
+ metadata.gz: 6b39bd65061b88f4b9cf223372dc7f8ab033635974e2200d5a02b761feff3e87baf5ef57188b61cc3b1657794056f5398ce4cb6e1947a009684edcda9df61849
7
+ data.tar.gz: e5636e5a5041cdac57c0aeb4aa77a0ca85268480f6101fdec72d03e1849410d1a7b274ec50ca027a88fc09d004e6148ae725588bc86736b072be5bf3fb606053
@@ -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
@@ -1,23 +1,19 @@
1
1
  require "crontab_rb/version"
2
2
  require "crontab_rb/configuration"
3
-
4
3
  module CrontabRb
5
4
  class << self
6
5
  attr_accessor :configuration
7
6
  end
8
-
7
+
9
8
  def self.configuration
10
9
  @configuration ||= Configuration.new
11
10
  end
12
-
11
+
13
12
  def self.configure
14
13
  yield(configuration)
15
14
  end
16
15
  end
17
16
 
18
17
  require "crontab_rb/database"
19
- require "crontab_rb/template"
20
- require "crontab_rb/parse"
21
18
  require "crontab_rb/write"
22
19
  require "crontab_rb/cron"
23
-
@@ -6,4 +6,4 @@ module CrontabRb
6
6
  @path_storage = "#{Dir.home}/.crontab_rb/"
7
7
  end
8
8
  end
9
- end
9
+ end
@@ -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,16 +60,12 @@ 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
72
66
 
73
67
  def write_crontab
74
- CrontabRb::Write.write_crontab
68
+ Write.write_crontab
75
69
  end
76
70
  end
77
71
  end
@@ -1,3 +1,3 @@
1
1
  module CrontabRb
2
- VERSION = "0.2.0"
2
+ VERSION = "1.1.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].presence,
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.0
4
+ version: 1.1.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-18 00:00:00.000000000 Z
11
+ date: 2020-06-11 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,65 +0,0 @@
1
- module CrontabRb
2
- class Parse
3
- def self.from_database
4
- new.call
5
- end
6
-
7
- def initialize
8
- @job_template = ":time /bin/bash -l -c 'cd :path && bundle exec bin/rails runner ':command''"
9
- @path = Dir.pwd
10
- end
11
-
12
- def call
13
- records = Database.all
14
- return "\n" if records.empty?
15
- contents = []
16
- records.each do |record|
17
- options = record
18
- options[:type] = record[:time]
19
- options[:time] = Template::EVERY[options[:time]]
20
- options[:path] = @path
21
- out = process_template(@job_template, options)
22
- out = process_template(out, options)
23
- contents << out + "\n"
24
- end
25
- contents.join("\n")
26
- end
27
-
28
- protected
29
-
30
- def process_template(template, options)
31
- template.gsub(/:\w+/) do |key|
32
- before_and_after = [$`[-1..-1], $'[0..0]]
33
- key_symbol = key.sub(':', '').to_sym
34
- option = ''
35
- if key_symbol === :at
36
- if options[:type].to_i/60 <= 1
37
- option = options[:at].to_i
38
- else
39
- t = options[:at].to_i*60
40
- option = Time.at(t).utc.strftime("%M %H")
41
- end
42
- else
43
- option = options[key_symbol] || key
44
- end
45
-
46
- if before_and_after.all? { |c| c == "'" }
47
- escape_single_quotes(option)
48
- elsif before_and_after.all? { |c| c == '"' }
49
- escape_double_quotes(option)
50
- else
51
- option
52
- end
53
- end.gsub(/\s+/m, " ").strip
54
- end
55
-
56
- def escape_single_quotes(str)
57
- str.gsub(/'/) { "'\\''" }
58
- end
59
-
60
- def escape_double_quotes(str)
61
- str.gsub(/"/) { '\"' }
62
- end
63
-
64
- end
65
- 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