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 +4 -4
- data/crontab_rb.gemspec +2 -2
- data/lib/crontab_rb.rb +0 -2
- data/lib/crontab_rb/cron.rb +4 -10
- data/lib/crontab_rb/version.rb +1 -1
- data/lib/crontab_rb/write.rb +53 -3
- metadata +20 -8
- data/lib/crontab_rb/parse.rb +0 -67
- data/lib/crontab_rb/template.rb +0 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4fc1a8a4826e74c83f78925e262edc685c92f6c6
|
4
|
+
data.tar.gz: aedcc842f2bfca1aeebc93f7fe421d0cc2c3c8b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9ef71931a09891e52aef70bc213b57d31517d023d09b37ac7c7782668bf0ebff323de210de7a5d85ea1a1885ef468f262e51012afff392ec3dd613c18e2480f0
|
7
|
+
data.tar.gz: d59d4e2b8290fa1e4a93c576f54f581d01044413b8ed59fe4f02296ced01295d51b8837342e0820d2c082e54ffff8f419c2b3605e9d6c96f71049f9e091fa1cf
|
data/crontab_rb.gemspec
CHANGED
@@ -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
|
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
|
data/lib/crontab_rb.rb
CHANGED
data/lib/crontab_rb/cron.rb
CHANGED
@@ -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
|
data/lib/crontab_rb/version.rb
CHANGED
data/lib/crontab_rb/write.rb
CHANGED
@@ -1,8 +1,38 @@
|
|
1
|
+
require 'whenever'
|
1
2
|
module CrontabRb
|
2
3
|
class Write
|
3
|
-
|
4
|
-
|
5
|
-
|
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.
|
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:
|
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: '
|
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: '
|
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
|
data/lib/crontab_rb/parse.rb
DELETED
@@ -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
|