crontab_rb 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/{LICENSE.txt → LICENSE.md} +0 -0
- data/README.md +35 -19
- data/lib/crontab_rb/cron.rb +10 -10
- data/lib/crontab_rb/database.rb +7 -7
- data/lib/crontab_rb/parse.rb +25 -15
- data/lib/crontab_rb/template.rb +1 -1
- data/lib/crontab_rb/version.rb +1 -1
- data/lib/crontab_rb/write.rb +2 -2
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 65c278bcd55709539e1dd44c32586b83dc6675b6
|
4
|
+
data.tar.gz: 6fe08b678086ca5fd6fb2e510f141919d8e96f32
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: baaf3561948a48335d927d85e4f811b803ae24316213da1234f1124c705ccfbe2cdf66271503974f471e3018640714523152288932f71debf06f70925cb538c6
|
7
|
+
data.tar.gz: 91ee610bab6525da0843f97a51110269d8a2d70bf51c68f0f47ff9cead2d0318169dda79dbe33ce1912e89b9b5b78e8b5fe62cade25dfb96c4250bee96e84693
|
data/{LICENSE.txt → LICENSE.md}
RENAMED
File without changes
|
data/README.md
CHANGED
@@ -1,39 +1,55 @@
|
|
1
|
-
#
|
1
|
+
# Crontab RB
|
2
2
|
|
3
|
-
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/crontab_rb.svg)](https://badge.fury.io/rb/crontab_rb)
|
4
4
|
|
5
|
-
|
5
|
+
crontab_rb is a Ruby gem that provides easy and safe way to manage your cron jobs unix file via CRUD.
|
6
6
|
|
7
|
-
|
7
|
+
### Installation
|
8
8
|
|
9
|
-
|
9
|
+
```sh
|
10
|
+
$ gem install crontab_rb
|
11
|
+
```
|
12
|
+
|
13
|
+
Or with Bundler in your Gemfile.
|
10
14
|
|
11
15
|
```ruby
|
12
16
|
gem 'crontab_rb'
|
13
17
|
```
|
18
|
+
Run bundle install to install the backend and crontab_rb gems.
|
14
19
|
|
15
|
-
|
16
|
-
|
17
|
-
$ bundle
|
20
|
+
### Getting started
|
18
21
|
|
19
|
-
|
22
|
+
Add new a cron job at every 10th minute.
|
23
|
+
```ruby
|
24
|
+
# */10 * * * * /bin/bash -l -c 'cd path_to_rails_app && bundle exec rake backup_db'
|
20
25
|
|
21
|
-
|
26
|
+
CrontabRb::Cron.create(name: 'Backup database', time: '1', at: '10', command: 'rake backup_db')
|
27
|
+
```
|
22
28
|
|
23
|
-
|
29
|
+
Delete a cron job
|
30
|
+
```ruby
|
31
|
+
CrontabRb::Cron.destroy("64f4f0bc-ad80-48ef-bbb9-98c9c17624bd")
|
32
|
+
```
|
24
33
|
|
25
|
-
|
34
|
+
Get list cron jobs
|
35
|
+
```ruby
|
36
|
+
CrontabRb::Cron.all
|
37
|
+
```
|
26
38
|
|
27
|
-
|
39
|
+
Update a cron job
|
40
|
+
```ruby
|
41
|
+
# 30 * * * * /bin/bash -l -c 'cd path_to_rails_app && bundle exec rake remove_log'
|
28
42
|
|
29
|
-
|
43
|
+
CrontabRb::Cron.destroy("64f4f0bc-ad80-48ef-bbb9-98c9c17624bd", {name: 'Remove log file', time: '60', at: '30', command: 'rake remove_log'})
|
44
|
+
```
|
30
45
|
|
31
|
-
|
46
|
+
### Contributing
|
32
47
|
|
33
|
-
|
48
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Nguyenanh/crontab_rb.
|
34
49
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/crontab_rb.
|
36
50
|
|
37
|
-
|
51
|
+
### Contribute
|
52
|
+
Fork Crontab RB and contribute to it. Pull requests are encouraged.
|
38
53
|
|
39
|
-
|
54
|
+
### License
|
55
|
+
[MIT](LICENSE.md)
|
data/lib/crontab_rb/cron.rb
CHANGED
@@ -6,7 +6,7 @@ module CrontabRb
|
|
6
6
|
attr_accessor :time
|
7
7
|
attr_accessor :at
|
8
8
|
attr_accessor :updated_at
|
9
|
-
|
9
|
+
|
10
10
|
def initialize(attributes={})
|
11
11
|
@id = attributes[:id]
|
12
12
|
@name = attributes[:name]
|
@@ -15,7 +15,7 @@ module CrontabRb
|
|
15
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)
|
@@ -26,19 +26,19 @@ module CrontabRb
|
|
26
26
|
cron.write_crontab
|
27
27
|
cron
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
def self.all
|
31
31
|
records = Database.all
|
32
32
|
records.map {|record| new(record)}
|
33
33
|
rescue
|
34
34
|
[]
|
35
35
|
end
|
36
|
-
|
36
|
+
|
37
37
|
def self.find(id)
|
38
38
|
record = Database.find(id)
|
39
39
|
record.nil? ? nil : new(record)
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
def self.update(id, options={})
|
43
43
|
record = Database.find(id)
|
44
44
|
return nil if record.nil?
|
@@ -52,7 +52,7 @@ module CrontabRb
|
|
52
52
|
cron.write_crontab
|
53
53
|
cron
|
54
54
|
end
|
55
|
-
|
55
|
+
|
56
56
|
def self.destroy(id)
|
57
57
|
record = Database.find(id)
|
58
58
|
return nil if record.nil?
|
@@ -61,17 +61,17 @@ module CrontabRb
|
|
61
61
|
cron.write_crontab
|
62
62
|
cron
|
63
63
|
end
|
64
|
-
|
64
|
+
|
65
65
|
def validate
|
66
66
|
raise "Time attribute of crontab_rb only accept #{CrontabRb::Template::EVERY.keys.join(",")}" unless CrontabRb::Template::EVERY.keys.include?(time)
|
67
67
|
end
|
68
|
-
|
68
|
+
|
69
69
|
def self.convert_to_symbol(hash)
|
70
70
|
Hash[hash.map{|k, v| [k.to_sym, v]}]
|
71
71
|
end
|
72
|
-
|
72
|
+
|
73
73
|
def write_crontab
|
74
74
|
CrontabRb::Write.write_crontab
|
75
75
|
end
|
76
76
|
end
|
77
|
-
end
|
77
|
+
end
|
data/lib/crontab_rb/database.rb
CHANGED
@@ -4,10 +4,10 @@ module CrontabRb
|
|
4
4
|
class Database
|
5
5
|
CRONTABRB = 'crontab_rb'
|
6
6
|
|
7
|
-
Dir.mkdir(CrontabRb.configuration.path_storage) unless File.directory?(CrontabRb.configuration.path_storage)
|
7
|
+
Dir.mkdir(CrontabRb.configuration.path_storage) unless File.directory?(CrontabRb.configuration.path_storage)
|
8
8
|
|
9
9
|
@@pstore = PStore.new("#{CrontabRb.configuration.path_storage}#{CRONTABRB}.pstore")
|
10
|
-
|
10
|
+
|
11
11
|
def initialize(options={})
|
12
12
|
@options = {}
|
13
13
|
@options[:id] = options[:id] || SecureRandom.uuid
|
@@ -17,29 +17,29 @@ module CrontabRb
|
|
17
17
|
@options[:at] = options[:at]
|
18
18
|
@options[:updated_at] = Time.now.strftime("%Y-%m-%d %H:%M:%S")
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
def self.create(options={})
|
22
22
|
new(options).save
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
def self.all
|
26
26
|
@@pstore.transaction(true) do
|
27
27
|
@@pstore.roots.map {|root| @@pstore[root]}
|
28
28
|
end
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
def self.find(name)
|
32
32
|
@@pstore.transaction(true) do
|
33
33
|
@@pstore[name]
|
34
34
|
end
|
35
35
|
end
|
36
|
-
|
36
|
+
|
37
37
|
def self.delete(name)
|
38
38
|
@@pstore.transaction do
|
39
39
|
@@pstore.delete(name)
|
40
40
|
end
|
41
41
|
end
|
42
|
-
|
42
|
+
|
43
43
|
def save
|
44
44
|
@@pstore.transaction do
|
45
45
|
@@pstore[@options[:id]] = @options
|
data/lib/crontab_rb/parse.rb
CHANGED
@@ -3,12 +3,12 @@ module CrontabRb
|
|
3
3
|
def self.from_database
|
4
4
|
new.call
|
5
5
|
end
|
6
|
-
|
6
|
+
|
7
7
|
def initialize
|
8
|
-
@job_template = ":time /bin/bash -l -c 'cd :path && bundle exec :command'"
|
8
|
+
@job_template = ":time /bin/bash -l -c 'cd :path && bundle exec bin/rails runner ':command''"
|
9
9
|
@path = Dir.pwd
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
def call
|
13
13
|
records = Database.all
|
14
14
|
return "\n" if records.empty?
|
@@ -24,32 +24,42 @@ module CrontabRb
|
|
24
24
|
end
|
25
25
|
contents.join("\n")
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
protected
|
29
29
|
|
30
30
|
def process_template(template, options)
|
31
31
|
template.gsub(/:\w+/) do |key|
|
32
|
+
before_and_after = [$`[-1..-1], $'[0..0]]
|
32
33
|
key_symbol = key.sub(':', '').to_sym
|
34
|
+
option = ''
|
33
35
|
if key_symbol === :at
|
34
36
|
if options[:type].to_i/60 <= 1
|
35
|
-
options[:at].to_i
|
37
|
+
option = options[:at].to_i
|
36
38
|
else
|
37
39
|
t = options[:at].to_i*60
|
38
|
-
Time.at(t).utc.strftime("%M %H")
|
40
|
+
option = Time.at(t).utc.strftime("%M %H")
|
39
41
|
end
|
40
42
|
else
|
41
|
-
options[key_symbol] || key
|
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
|
42
52
|
end
|
43
53
|
end.gsub(/\s+/m, " ").strip
|
44
54
|
end
|
45
55
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
56
|
+
def escape_single_quotes(str)
|
57
|
+
str.gsub(/'/) { "'\\''" }
|
58
|
+
end
|
59
|
+
|
60
|
+
def escape_double_quotes(str)
|
61
|
+
str.gsub(/"/) { '\"' }
|
62
|
+
end
|
53
63
|
|
54
64
|
end
|
55
|
-
end
|
65
|
+
end
|
data/lib/crontab_rb/template.rb
CHANGED
data/lib/crontab_rb/version.rb
CHANGED
data/lib/crontab_rb/write.rb
CHANGED
@@ -2,7 +2,7 @@ module CrontabRb
|
|
2
2
|
class Write
|
3
3
|
def self.write_crontab
|
4
4
|
contents = CrontabRb::Parse.from_database
|
5
|
-
command = "crontab -"
|
5
|
+
command = "crontab -"
|
6
6
|
IO.popen(command, 'r+') do |crontab|
|
7
7
|
crontab.write(contents)
|
8
8
|
crontab.close_write
|
@@ -13,4 +13,4 @@ module CrontabRb
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
end
|
16
|
-
end
|
16
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: crontab_rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.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-
|
11
|
+
date: 2019-09-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -61,7 +61,7 @@ extra_rdoc_files: []
|
|
61
61
|
files:
|
62
62
|
- ".gitignore"
|
63
63
|
- Gemfile
|
64
|
-
- LICENSE.
|
64
|
+
- LICENSE.md
|
65
65
|
- README.md
|
66
66
|
- Rakefile
|
67
67
|
- bin/console
|