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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 87dace870a1b2973c408ec2e054522bc89a729db
4
- data.tar.gz: caf4330067b3ec97c18033a4a360a2430e63b975
3
+ metadata.gz: 65c278bcd55709539e1dd44c32586b83dc6675b6
4
+ data.tar.gz: 6fe08b678086ca5fd6fb2e510f141919d8e96f32
5
5
  SHA512:
6
- metadata.gz: 34bb9b5b393573c421e21c4c4a15bbc35cb0b648c34be9c38c73890be6596198b9568e8c4360cc515209a824061e6f3e17dd294438b7468f6073377fd14fbfa0
7
- data.tar.gz: dcf0c3546bffb5b41149d46230ab955fd6287b4838ff8931c64df2b244e24dc24b3652788e02049d3ec203331201b0d7760f956508fce818d0d2c0ad1dc691a0
6
+ metadata.gz: baaf3561948a48335d927d85e4f811b803ae24316213da1234f1124c705ccfbe2cdf66271503974f471e3018640714523152288932f71debf06f70925cb538c6
7
+ data.tar.gz: 91ee610bab6525da0843f97a51110269d8a2d70bf51c68f0f47ff9cead2d0318169dda79dbe33ce1912e89b9b5b78e8b5fe62cade25dfb96c4250bee96e84693
File without changes
data/README.md CHANGED
@@ -1,39 +1,55 @@
1
- # CrontabRb
1
+ # Crontab RB
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/crontab_rb`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ [![Gem Version](https://badge.fury.io/rb/crontab_rb.svg)](https://badge.fury.io/rb/crontab_rb)
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
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
- ## Installation
7
+ ### Installation
8
8
 
9
- Add this line to your application's Gemfile:
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
- And then execute:
16
-
17
- $ bundle
20
+ ### Getting started
18
21
 
19
- Or install it yourself as:
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
- $ gem install crontab_rb
26
+ CrontabRb::Cron.create(name: 'Backup database', time: '1', at: '10', command: 'rake backup_db')
27
+ ```
22
28
 
23
- ## Usage
29
+ Delete a cron job
30
+ ```ruby
31
+ CrontabRb::Cron.destroy("64f4f0bc-ad80-48ef-bbb9-98c9c17624bd")
32
+ ```
24
33
 
25
- TODO: Write usage instructions here
34
+ Get list cron jobs
35
+ ```ruby
36
+ CrontabRb::Cron.all
37
+ ```
26
38
 
27
- ## Development
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
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
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
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
46
+ ### Contributing
32
47
 
33
- ## Contributing
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
- ## License
51
+ ### Contribute
52
+ Fork Crontab RB and contribute to it. Pull requests are encouraged.
38
53
 
39
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
54
+ ### License
55
+ [MIT](LICENSE.md)
@@ -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
@@ -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
@@ -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
- # def escape_single_quotes(str)
47
- # str.gsub(/'/) { "'\\''" }
48
- # end
49
- #
50
- # def escape_double_quotes(str)
51
- # str.gsub(/"/) { '\"' }
52
- # end
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
@@ -7,4 +7,4 @@ module CrontabRb
7
7
  "4320" => ":at */3 * *"
8
8
  }
9
9
  end
10
- end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module CrontabRb
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -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.1.2
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-08-21 00:00:00.000000000 Z
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.txt
64
+ - LICENSE.md
65
65
  - README.md
66
66
  - Rakefile
67
67
  - bin/console