cron_wrapper 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data.tar.gz.sig ADDED
Binary file
data/Manifest ADDED
@@ -0,0 +1,5 @@
1
+ Manifest
2
+ Rakefile
3
+ bin/cron_wrapper
4
+ cron_wrapper.gemspec
5
+ lib/cron_wrapper.rb
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('cron_wrapper', '0.0.1') do |p|
6
+ p.description = "A gem that provides useful features for running ruby or Rails scripts with cron"
7
+ p.url = "http://bugzilla.visaops.net/project_automate_groups/trunk/projects/cron_wrapper"
8
+ p.author = "Bryan Taylor"
9
+ p.summary = 'features: locking to prevent resource contention, standard logging, optional rails integration'
10
+ p.email = "btaylor39 @nospam@ csc.com"
11
+ p.ignore_pattern = ["tmp/*", "script/*"]
12
+ p.development_dependencies = []
13
+ end
14
+
data/bin/cron_wrapper ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'cron_wrapper'
4
+ CronWrapper.new(ARGV).run
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{cron_wrapper}
5
+ s.version = "0.0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Bryan Taylor"]
9
+ s.cert_chain = ["/home/bryan/gem_private_key/gem-public_cert.pem"]
10
+ s.date = %q{2011-02-18}
11
+ s.default_executable = %q{cron_wrapper}
12
+ s.description = %q{A gem that provides useful features for running ruby or Rails scripts with cron}
13
+ s.email = %q{btaylor39 @nospam@ csc.com}
14
+ s.executables = ["cron_wrapper"]
15
+ s.extra_rdoc_files = ["bin/cron_wrapper", "lib/cron_wrapper.rb"]
16
+ s.files = ["Manifest", "Rakefile", "bin/cron_wrapper", "cron_wrapper.gemspec", "lib/cron_wrapper.rb"]
17
+ s.homepage = %q{http://bugzilla.visaops.net/project_automate_groups/trunk/projects/cron_wrapper}
18
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Cron_wrapper"]
19
+ s.require_paths = ["lib"]
20
+ s.rubyforge_project = %q{cron_wrapper}
21
+ s.rubygems_version = %q{1.3.7}
22
+ s.signing_key = %q{/home/bryan/gem_private_key/gem-private_key.pem}
23
+ s.summary = %q{features: locking to prevent resource contention, standard logging, optional rails integration}
24
+
25
+ if s.respond_to? :specification_version then
26
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
27
+ s.specification_version = 3
28
+
29
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
30
+ else
31
+ end
32
+ else
33
+ end
34
+ end
@@ -0,0 +1,123 @@
1
+ require 'optparse'
2
+ require 'logger'
3
+
4
+ class CronWrapper
5
+
6
+ attr_reader :logger
7
+
8
+
9
+ def initialize(*args)
10
+ @options = {}
11
+ @options[:skip_rails] = false
12
+ @options[:root] = '.'
13
+ @options[:lock_dir] = nil
14
+ @options[:dry_run] = false
15
+ @options[:silent] = false
16
+ @options[:verbose] = false
17
+ @options[:log] = nil
18
+
19
+ # do options
20
+ foo = OptionParser.new do |opts|
21
+ opts.banner = "Usage: cron_wrapper [options] <script>\
22
+ where script is the name (without file extension) of a file located in <root>/lib/cron
23
+ Example:
24
+ > pwd
25
+ /home/projects/foo
26
+ > ls lib/cron
27
+ awesome_script.rb
28
+ >cron_wrapper awesome_script
29
+ "
30
+
31
+ opts.on("--dry-run", "dry run (default: off)") { |o| @options[:dry_run] = o }
32
+ opts.on("--skip-rails", "Do not try to load Rails (default: off)") { |o| @options[:skip_rails] = o }
33
+ opts.on("--root", "Root or working directory (default: .)") { |o| @options[:root] = o }
34
+ opts.on("--lock_dir", "Lock dir (default: <root>/tmp/locks)") { |o| @options[:lock_dir] = o }
35
+ opts.on("-s", "--silent", "Do not output anything (default: off)") { |o| @options[:silent] = o }
36
+ opts.on("-v", "--[no-]verbose", "Run verbosely (default: off)") { |o| @options[:verbose] = o }
37
+
38
+ opts.on_tail("-h", "--help", "Show this message") do
39
+ puts opts
40
+ exit
41
+ end
42
+ end.parse!
43
+
44
+
45
+ # Setup rails
46
+ if !@options[:skip_rails]
47
+ if File.exist?(File.join(@options[:root], 'config/application.rb'))
48
+ libs = [ File.join(@options[:root], 'config/application.rb') ]
49
+ else
50
+ libs = [ File.join(@options[:root],'config/environment.rb') ]
51
+ end
52
+ libs.each do |lib|
53
+ begin
54
+ require lib
55
+ rescue LoadError
56
+ abort "option rails is on, but could not load rails via '#{lib}'"
57
+ end
58
+ end
59
+ end
60
+
61
+
62
+ # Setup logging
63
+ @options[:logger] ||= STDOUT
64
+ @logger = Logger.new(@options[:logger])
65
+ @logger.level = case
66
+ when (!@options[:verbose] && !@options[:silent])
67
+ Logger::INFO
68
+ when (@options[:verbose] && !@options[:silent])
69
+ Logger::DEBUG
70
+ when (!@options[:verbose] && @options[:silent])
71
+ Logger::FATAL
72
+ when (@options[:verbose] && @options[:silent])
73
+ Logger::WARN
74
+ end
75
+
76
+ # Setup locking
77
+ @options[:lock_dir] ||= File.join @options[:root], "tmp/locks"
78
+ if !File.exists?(@options[:lock_dir])
79
+ logger.warn("You lock setting #{@options[:lock_dir]} doesn't exist - using /tmp")
80
+ @options[:lock_dir] = '/tmp'
81
+ end
82
+ end
83
+
84
+
85
+
86
+ def run
87
+ base_name = ARGV[0]
88
+ target = File.join(@options[:root], "lib/cron/#{base_name}.rb")
89
+ lock_file = File.join(@options[:lock_dir], "/#{base_name}.lock")
90
+
91
+ if File.exists? lock_file
92
+ lock_pid = File.read(lock_file)
93
+ ps_output = `ps -p #{lock_pid}`
94
+ if ps_output.split("\n").length > 1
95
+ logger.debug "#{base_name} is already running, skipping"
96
+ exit
97
+ else
98
+ logger.debug "cleaning stale pid #{lock_pid} from #{lock_file}"
99
+ end
100
+ end
101
+
102
+ File.open(lock_file, 'w') do |file|
103
+ file << Process::pid
104
+ end
105
+
106
+
107
+ # Load target script
108
+ begin
109
+ load target
110
+ puts "#{target} successfully loaded via cron_wrapper"
111
+ rescue LoadError => e
112
+ puts "#{target} failed to load via cron_wrapper"
113
+ logger.info e.message
114
+ logger.debug e.backtrace
115
+ end
116
+
117
+ FileUtils.rm_f lock_file rescue nil
118
+
119
+ end
120
+
121
+
122
+
123
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cron_wrapper
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Bryan Taylor
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain:
17
+ - |
18
+ -----BEGIN CERTIFICATE-----
19
+ MIIDMDCCAhigAwIBAgIBADANBgkqhkiG9w0BAQUFADA+MRIwEAYDVQQDDAlidGF5
20
+ bG9yMzkxEzARBgoJkiaJk/IsZAEZFgNjc2MxEzARBgoJkiaJk/IsZAEZFgNjb20w
21
+ HhcNMTEwMjE2MTI1MzQzWhcNMTIwMjE2MTI1MzQzWjA+MRIwEAYDVQQDDAlidGF5
22
+ bG9yMzkxEzARBgoJkiaJk/IsZAEZFgNjc2MxEzARBgoJkiaJk/IsZAEZFgNjb20w
23
+ ggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDid0W3CiAzCKP+H70D/FFq
24
+ RnwfMdTqsIGK6pKZVHIqqcZxntJw1r3zHwvDwwjh67/iysB0mum6Palizm0xtBqZ
25
+ wTHO+GXNsTz/+zfihkJMSVhSY8Y58SNuV5m1CmzdVSyVrTyOlkc6+IpHTlpaCpf4
26
+ r3mGtZbsuEpLvU/A3iC778k8Bm9hoKhI/3CoMWQjSTHYfrMDWetuGhu8NbdGK3BN
27
+ E1dgEBdQLAjf8yeY3pmmUE88cr9EvrFVdx7mnI5lzOwZMuFSgEBw5AsB6+vvlTW3
28
+ i75Q4pJwjeM41i2B1u7T4kskmOogfZl3YAulPXrEaTCKHDEQb/Jp4WtTy2v8m+79
29
+ AgMBAAGjOTA3MAkGA1UdEwQCMAAwCwYDVR0PBAQDAgSwMB0GA1UdDgQWBBS36r/0
30
+ ouuKO1EVrbluXYEBlANxODANBgkqhkiG9w0BAQUFAAOCAQEAgNN7J2bWCZDSQQm8
31
+ 7PfoCViUKWIcWEZB+C05R0JDpFue0lL5s6aAt61rPXL6k5UiLtj8QkGSe0vasC9G
32
+ f8eBWU5l3g/HDvsFZ5cIBWk0saF7xlIZC3E98LF8Vcfl9WFibHCuwApxzzcAQvdU
33
+ ocHFXaTM2tz8iRO5pW+Do3GC7Mjbo2mvAGN0H5f4tZkZ+Ttm25bAPpnHfXaWPxKq
34
+ RL48Oo1H6iw8nmjm8YrQC6UdtW8ogetCkJwwFmK/U5YQQsE0NnUSG5Ii5R32ZdaY
35
+ eMvMROg/ZYJZzMayO+XXWYgXhy2UDEP/hNhNzM3E+XJUilcA4dZ/fdX57vL0Qpvn
36
+ NK5lPw==
37
+ -----END CERTIFICATE-----
38
+
39
+ date: 2011-02-18 00:00:00 +00:00
40
+ default_executable:
41
+ dependencies: []
42
+
43
+ description: A gem that provides useful features for running ruby or Rails scripts with cron
44
+ email: btaylor39 @nospam@ csc.com
45
+ executables:
46
+ - cron_wrapper
47
+ extensions: []
48
+
49
+ extra_rdoc_files:
50
+ - bin/cron_wrapper
51
+ - lib/cron_wrapper.rb
52
+ files:
53
+ - Manifest
54
+ - Rakefile
55
+ - bin/cron_wrapper
56
+ - cron_wrapper.gemspec
57
+ - lib/cron_wrapper.rb
58
+ has_rdoc: true
59
+ homepage: http://bugzilla.visaops.net/project_automate_groups/trunk/projects/cron_wrapper
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options:
64
+ - --line-numbers
65
+ - --inline-source
66
+ - --title
67
+ - Cron_wrapper
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 11
85
+ segments:
86
+ - 1
87
+ - 2
88
+ version: "1.2"
89
+ requirements: []
90
+
91
+ rubyforge_project: cron_wrapper
92
+ rubygems_version: 1.3.7
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: "features: locking to prevent resource contention, standard logging, optional rails integration"
96
+ test_files: []
97
+
metadata.gz.sig ADDED
@@ -0,0 +1 @@
1
+ � !b5s?�N󄸱J,�:t����L��Œ�rHIg�ެ�u'�=>�