croner 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: e2c53f99fb1f7001e5afa108ccecc0139b9d7568
4
+ data.tar.gz: 5900fc7bd559c463b9eda63b0810dfea562f2de4
5
+ SHA512:
6
+ metadata.gz: 244c31d44736a6e5cf850e62cb36e0b0c308f5234ad3e56c4ba16aba420c1bdcac16aa68f943eb5498177b0d579ad02ff7ae09fd010be24338589bba7232dba4
7
+ data.tar.gz: 380b32163e91d2995b5d394ed8449aa58b19b1f26e2003a497c7d764c3e5fa994209f34c50c7c1825b36b12e670ca1c5f9907e57049f74f4bbc9dd89275d94e3
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2017 ykogure
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # Croner
2
+ An plugin that manage cron jobs each applications and servers.
3
+
4
+ *Read this in other languages: [日本語](README.ja.md)*
5
+
6
+ ## Installation
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'croner'
11
+ ```
12
+
13
+ And then execute:
14
+ ```bash
15
+ $ bundle
16
+ ```
17
+
18
+ ## Usage
19
+ Make `config/croner/hosts/[hostname]` and run `croner:update`.
20
+
21
+ ## License
22
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
23
+
24
+ ## Author
25
+ [ykogure](https://github.com/ykogure)
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Croner'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+ require 'bundler/gem_tasks'
21
+
22
+ require 'rake/testtask'
23
+
24
+ Rake::TestTask.new(:test) do |t|
25
+ t.libs << 'lib'
26
+ t.libs << 'test'
27
+ t.pattern = 'test/**/*_test.rb'
28
+ t.verbose = false
29
+ end
30
+
31
+
32
+ task default: :test
@@ -0,0 +1,7 @@
1
+ module Croner
2
+ class Railtie < ::Rails::Railtie
3
+ rake_tasks do
4
+ load "tasks/croner_tasks.rake"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Croner
2
+ VERSION = '0.1.0'
3
+ end
data/lib/croner.rb ADDED
@@ -0,0 +1,54 @@
1
+ require 'croner/railtie'
2
+
3
+ module Croner
4
+ require "open3"
5
+ def self.run
6
+ # ホスト名毎のcron設定ファイルがある場合に実行する
7
+ return {status: false, message: ''} unless File.exist?(Rails.root.join('config', 'croner', 'hosts', `hostname`.delete("\n")))
8
+
9
+ # 設定内容を取得
10
+ insert_rows = File.read(Rails.root.join('config', 'croner', 'hosts', `hostname`.delete("\n"))).split("\n")
11
+
12
+ # 現在のcron設定を取得
13
+ cron_rows = `crontab -l`.split("\n")
14
+
15
+ application_name = Rails.application.class.parent_name
16
+ cron_start_row = "# ==================== START #{application_name} CronJobs by Croner ===================="
17
+ cron_end_row = "# ==================== END #{application_name} CronJobs by Croner ===================="
18
+
19
+ start_index = cron_rows.index(cron_start_row)
20
+ end_index = cron_rows.index(cron_end_row)
21
+
22
+ # 開始行か終了行のどちらかのみ存在する場合はエラー
23
+ return ArgumentError.new('現在のcron設定にCronerによる不正な行が存在しています') unless ((start_index.present? && end_index.present?) || (start_index.blank? && end_index.blank?))
24
+
25
+ if start_index.present? && end_index.present?
26
+ # 既存の内容を削除
27
+ cron_rows.slice!((start_index + 1)..(end_index - 1))
28
+ else
29
+ start_index = cron_rows.count
30
+ cron_rows << cron_start_row
31
+ cron_rows << cron_end_row
32
+ end
33
+
34
+ # cron設定を挿入
35
+ cron_rows.insert(start_index + 1, *insert_rows)
36
+
37
+ # ファイルに一時保存
38
+ File.open("cron_tmp", "w") do |f|
39
+ cron_rows.each{|row| f.puts(row)}
40
+ end
41
+
42
+ # ファイルの内容をcronに出力
43
+ stdout, stderror, status = Open3.capture3("crontab cron_tmp")
44
+
45
+ # ファイルを削除
46
+ File.delete("cron_tmp")
47
+
48
+ if status.success?
49
+ return {status: true}
50
+ else
51
+ return {status: false, message: stderror}
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,12 @@
1
+ namespace :croner do
2
+ desc 'Update Cron'
3
+ task update: :environment do
4
+ result = Croner.run
5
+
6
+ if result[:status]
7
+ puts 'cron update success!'
8
+ else
9
+ puts result[:message]
10
+ end
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: croner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ykogure
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-08-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: mysql2
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: An plugin that manage cron jobs each applications and servers
42
+ email:
43
+ - renkin1008@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - lib/croner.rb
52
+ - lib/croner/railtie.rb
53
+ - lib/croner/version.rb
54
+ - lib/tasks/croner_tasks.rake
55
+ homepage: https://github.com/ZIGExN/croner
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.6.11
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: An plugin that manage cron jobs each applications and servers
79
+ test_files: []