crontab_rb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f538e7a9a45b50d8a35d59effe009c8193c4692b
4
+ data.tar.gz: 49d9d09a79411597a986282b5b55ba85436b8655
5
+ SHA512:
6
+ metadata.gz: cf3667606f931071840a06729ac3fbd635712c291a7c273a483aefa8ca044da7f363042418ec109c6e3808396ef42730dfc2d2a6db19eabea153ec288bbbbfc6
7
+ data.tar.gz: b0a99648d0ef318d6b122315ba6168afaf28b5ed1680e59866a6268d3f2a842df9e4d1651c04261d87e13f3fe0a65d7ea62df5a163f9c7118894570a1cab5d2d
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /crontab_rb.db
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in crontab_rb.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Nguyen Anh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # CrontabRb
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.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'crontab_rb'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install crontab_rb
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
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.
30
+
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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/crontab_rb.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "crontab_rb"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "crontab_rb/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "crontab_rb"
8
+ spec.version = CrontabRb::VERSION
9
+ spec.authors = ["Nguyen Anh"]
10
+ spec.email = ["cauut2117610@gmail.com"]
11
+
12
+ spec.summary = ""
13
+ spec.description = "Easy and safe way to manage your crontab file via CRUD"
14
+ spec.homepage = "https://github.com/Nguyenanh/crontab_rb"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata["allowed_push_host"] = "https://rubygems.org"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ # end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.15"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "pry", "~> 0.12.2"
36
+
37
+ end
@@ -0,0 +1,9 @@
1
+ module CrontabRb
2
+ class Configuration
3
+ attr_accessor :path_storage
4
+
5
+ def initialize
6
+ @path_storage = "#{Dir.home}/.crontab_rb/"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,77 @@
1
+ module CrontabRb
2
+ class Cron
3
+ attr_accessor :id
4
+ attr_accessor :name
5
+ attr_accessor :command
6
+ attr_accessor :time
7
+ attr_accessor :at
8
+ attr_accessor :updated_at
9
+
10
+ def initialize(attributes={})
11
+ @id = attributes[:id]
12
+ @name = attributes[:name]
13
+ @command = attributes[:command]
14
+ @time = attributes[:time]
15
+ @at = attributes[:at]
16
+ @updated_at = attributes[:updated_at]
17
+ end
18
+
19
+ def self.create(options={})
20
+ options = convert_to_symbol(options)
21
+ cron = new(options)
22
+ cron.validate
23
+ record = Database.create(options)
24
+ cron.id = record[:id]
25
+ cron.updated_at = record[:updated_at]
26
+ cron.write_crontab
27
+ cron
28
+ end
29
+
30
+ def self.all
31
+ records = Database.all
32
+ records.map {|record| new(record)}
33
+ rescue
34
+ []
35
+ end
36
+
37
+ def self.find(id)
38
+ record = Database.find(id)
39
+ record.nil? ? nil : new(record)
40
+ end
41
+
42
+ def self.update(id, options={})
43
+ record = Database.find(id)
44
+ return nil if record.nil?
45
+ record[:name] = options[:name] || record[:name]
46
+ record[:command] = options[:command] || record[:command]
47
+ record[:time] = options[:time] || record[:time]
48
+ record[:at] = options[:at] || record[:at]
49
+ cron = new(record)
50
+ cron.validate
51
+ Database.new(record).save
52
+ cron.write_crontab
53
+ cron
54
+ end
55
+
56
+ def self.destroy(id)
57
+ record = Database.find(id)
58
+ return nil if record.nil?
59
+ cron = new(record)
60
+ Database.delete(id)
61
+ cron.write_crontab
62
+ cron
63
+ end
64
+
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
+ def self.convert_to_symbol(hash)
70
+ Hash[hash.map{|k, v| [k.to_sym, v]}]
71
+ end
72
+
73
+ def write_crontab
74
+ CrontabRb::Write.write_crontab
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,50 @@
1
+ require "pstore"
2
+ require 'securerandom'
3
+ module CrontabRb
4
+ class Database
5
+ CRONTABRB = 'crontab_rb'
6
+
7
+ Dir.mkdir(CrontabRb.configuration.path_storage) unless File.directory?(CrontabRb.configuration.path_storage)
8
+
9
+ @@pstore = PStore.new("#{CrontabRb.configuration.path_storage}#{CRONTABRB}.pstore")
10
+
11
+ def initialize(options={})
12
+ @options = {}
13
+ @options[:id] = options[:id] || SecureRandom.uuid
14
+ @options[:name] = options[:name]
15
+ @options[:command] = options[:command]
16
+ @options[:time] = options[:time]
17
+ @options[:at] = options[:at]
18
+ @options[:updated_at] = Time.now.strftime("%Y-%m-%d %H:%M:%S")
19
+ end
20
+
21
+ def self.create(options={})
22
+ new(options).save
23
+ end
24
+
25
+ def self.all
26
+ @@pstore.transaction(true) do
27
+ @@pstore.roots.map {|root| @@pstore[root]}
28
+ end
29
+ end
30
+
31
+ def self.find(name)
32
+ @@pstore.transaction(true) do
33
+ @@pstore[name]
34
+ end
35
+ end
36
+
37
+ def self.delete(name)
38
+ @@pstore.transaction do
39
+ @@pstore.delete(name)
40
+ end
41
+ end
42
+
43
+ def save
44
+ @@pstore.transaction do
45
+ @@pstore[@options[:id]] = @options
46
+ @options
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,55 @@
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 :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
+ key_symbol = key.sub(':', '').to_sym
33
+ if key_symbol === :at
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("%H %M")
39
+ end
40
+ else
41
+ options[key_symbol] || key
42
+ end
43
+ end.gsub(/\s+/m, " ").strip
44
+ end
45
+
46
+ # def escape_single_quotes(str)
47
+ # str.gsub(/'/) { "'\\''" }
48
+ # end
49
+ #
50
+ # def escape_double_quotes(str)
51
+ # str.gsub(/"/) { '\"' }
52
+ # end
53
+
54
+ end
55
+ end
@@ -0,0 +1,10 @@
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
@@ -0,0 +1,3 @@
1
+ module CrontabRb
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,19 @@
1
+ module CrontabRb
2
+ class Write
3
+ def self.write_crontab
4
+ contents = CrontabRb::Parse.from_database
5
+ command = "crontab -"
6
+ IO.popen(command, 'r+') do |crontab|
7
+ crontab.write(contents)
8
+ crontab.close_write
9
+ end
10
+ success = $?.exitstatus.zero?
11
+ if success
12
+ puts "[write] crontab file updated"
13
+ exit(0)
14
+ else
15
+ exit(1)
16
+ end
17
+ end
18
+ end
19
+ end
data/lib/crontab_rb.rb ADDED
@@ -0,0 +1,23 @@
1
+ require "crontab_rb/version"
2
+ require "crontab_rb/configuration"
3
+
4
+ module CrontabRb
5
+ class << self
6
+ attr_accessor :configuration
7
+ end
8
+
9
+ def self.configuration
10
+ @configuration ||= Configuration.new
11
+ end
12
+
13
+ def self.configure
14
+ yield(configuration)
15
+ end
16
+ end
17
+
18
+ require "crontab_rb/database"
19
+ require "crontab_rb/template"
20
+ require "crontab_rb/parse"
21
+ require "crontab_rb/write"
22
+ require "crontab_rb/cron"
23
+
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: crontab_rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nguyen Anh
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-08-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.12.2
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.12.2
55
+ description: Easy and safe way to manage your crontab file via CRUD
56
+ email:
57
+ - cauut2117610@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - bin/console
68
+ - bin/setup
69
+ - crontab_rb.gemspec
70
+ - lib/crontab_rb.rb
71
+ - lib/crontab_rb/configuration.rb
72
+ - lib/crontab_rb/cron.rb
73
+ - lib/crontab_rb/database.rb
74
+ - lib/crontab_rb/parse.rb
75
+ - lib/crontab_rb/template.rb
76
+ - lib/crontab_rb/version.rb
77
+ - lib/crontab_rb/write.rb
78
+ homepage: https://github.com/Nguyenanh/crontab_rb
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.6.13
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: ''
102
+ test_files: []