auto_cron 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/auto_cron.gemspec +1 -1
- data/lib/auto_cron/railtie.rb +5 -6
- data/lib/auto_cron.rb +57 -59
- data/lib/tasks/auto_cron_tasks.rake +2 -1
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.4
|
data/auto_cron.gemspec
CHANGED
data/lib/auto_cron/railtie.rb
CHANGED
data/lib/auto_cron.rb
CHANGED
@@ -1,69 +1,67 @@
|
|
1
1
|
require 'auto_cron/railtie' if defined?(Rails)
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
require 'erb'
|
3
|
+
class AutoCron
|
4
|
+
def initialize( template, application )
|
5
|
+
require 'erb'
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
def updated_crontab
|
14
|
-
# Check for unopened or unclosed identifier blocks
|
15
|
-
if read_crontab.index(comment_open) && !read_crontab.index(comment_close)
|
16
|
-
warn "[fail] Unclosed indentifier; Your crontab file contains '#{comment_open}', but no '#{comment_close}'"
|
17
|
-
exit(1)
|
18
|
-
elsif !read_crontab.index(comment_open) && read_crontab.index(comment_close)
|
19
|
-
warn "[fail] Unopened indentifier; Your crontab file contains '#{comment_close}', but no '#{comment_open}'"
|
20
|
-
exit(1)
|
21
|
-
end
|
22
|
-
|
23
|
-
# If an existing identier block is found, replace it with the new cron entries
|
24
|
-
if read_crontab.index(comment_open) && read_crontab.index(comment_close)
|
25
|
-
read_crontab.gsub(Regexp.new("#{comment_open}.+#{comment_close}", Regexp::MULTILINE), auto_cron_wrapped.chomp)
|
26
|
-
else # Otherwise, append the new cron entries after any existing ones
|
27
|
-
[read_crontab, auto_cron_wrapped].join("\n\n")
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
protected
|
7
|
+
@auto_cron_dir = File.join( Rails.root, "config", "auto_cron" )
|
8
|
+
@template = template
|
9
|
+
@application = application
|
10
|
+
end
|
32
11
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
else
|
42
|
-
''
|
43
|
-
end
|
44
|
-
full_template_path = File.join( @auto_cron_dir, "#{ @template }.erb" )
|
45
|
-
header + "\n\n" + ERB.new( File.read( full_template_path ) ).result
|
12
|
+
def updated_crontab
|
13
|
+
# Check for unopened or unclosed identifier blocks
|
14
|
+
if read_crontab.index(comment_open) && !read_crontab.index(comment_close)
|
15
|
+
warn "[fail] Unclosed indentifier; Your crontab file contains '#{comment_open}', but no '#{comment_close}'"
|
16
|
+
exit(1)
|
17
|
+
elsif !read_crontab.index(comment_open) && read_crontab.index(comment_close)
|
18
|
+
warn "[fail] Unopened indentifier; Your crontab file contains '#{comment_close}', but no '#{comment_open}'"
|
19
|
+
exit(1)
|
46
20
|
end
|
47
21
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
@current_crontab = $?.exitstatus.zero? ? command_results : ''
|
22
|
+
# If an existing identier block is found, replace it with the new cron entries
|
23
|
+
if read_crontab.index(comment_open) && read_crontab.index(comment_close)
|
24
|
+
read_crontab.gsub(Regexp.new("#{comment_open}.+#{comment_close}", Regexp::MULTILINE), auto_cron_wrapped.chomp)
|
25
|
+
else # Otherwise, append the new cron entries after any existing ones
|
26
|
+
[read_crontab, auto_cron_wrapped].join("\n\n")
|
54
27
|
end
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
28
|
+
end
|
29
|
+
|
30
|
+
protected
|
31
|
+
|
32
|
+
def auto_cron_wrapped
|
33
|
+
@auto_cron_wrapped ||= [comment_open, auto_cron_inner, comment_close].join("\n") + "\n"
|
34
|
+
end
|
35
|
+
|
36
|
+
def auto_cron_inner
|
37
|
+
header_template = File.join( @auto_cron_dir, 'header.erb' )
|
38
|
+
header = if File.exist?( header_template )
|
39
|
+
ERB.new( File.read( header_template ) ).result
|
40
|
+
else
|
41
|
+
''
|
66
42
|
end
|
43
|
+
full_template_path = File.join( @auto_cron_dir, "#{ @template }.erb" )
|
44
|
+
header + "\n\n" + ERB.new( File.read( full_template_path ) ).result
|
45
|
+
end
|
46
|
+
|
47
|
+
def read_crontab
|
48
|
+
return @current_crontab if @current_crontab
|
67
49
|
|
50
|
+
command = ['crontab -l']
|
51
|
+
command_results = %x[#{command.join(' ')} 2> /dev/null]
|
52
|
+
@current_crontab = $?.exitstatus.zero? ? command_results : ''
|
68
53
|
end
|
69
|
-
|
54
|
+
|
55
|
+
def comment_base
|
56
|
+
"auto_cron generated tasks for: #{ @application }"
|
57
|
+
end
|
58
|
+
|
59
|
+
def comment_open
|
60
|
+
"# Begin #{ comment_base }"
|
61
|
+
end
|
62
|
+
|
63
|
+
def comment_close
|
64
|
+
"# End #{ comment_base }"
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
@@ -3,7 +3,8 @@ require File.dirname(__FILE__) + "/../auto_cron"
|
|
3
3
|
desc "Generates the auto-cron and saves it to the crontab"
|
4
4
|
task :auto_cron do
|
5
5
|
require 'tempfile'
|
6
|
-
|
6
|
+
auto_cron = AutoCron.new ENV['TEMPLATE'], ENV['APPLICATION']
|
7
|
+
cron_body = auto_cron.updated_crontab
|
7
8
|
tmp_cron_path = Tempfile.new( 'auto_cron' ).path
|
8
9
|
File.open( tmp_cron_path, File::WRONLY | File::APPEND ) do |file|
|
9
10
|
file << cron_body
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: auto_cron
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 4
|
10
|
+
version: 0.1.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Alex Farrill
|