cron_helper 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: bd964229c9cdbfb7aa11cd0002df11496f72c8d7
4
+ data.tar.gz: e83495556082032e5c6d582e03ed6ec8ed03df45
5
+ SHA512:
6
+ metadata.gz: aa50228bb6f65b2434d89959b756e436d3cbb1fa82f7c81b9bd71b49fc464f3b4252242329c16ccf84b2a580d1c1d7d99f669d70656fe98750e4e2fa9810be27
7
+ data.tar.gz: 0a7748d1ab42cd61e0b0b922e6d7c92f9622fc5ca92e8bab9d6445336a207fa5981559ca4ea9f516cf9d65a1505632ef5c2eaa10859165698ed1b80b4e136fd8
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cron_helper.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Chad Remesch
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,96 @@
1
+ # CronHelper
2
+
3
+ The Cron Helper gem is designed to add a few additional features to cron jobs created with the [Whenever](https://github.com/javan/whenever) gem.
4
+
5
+ Features include:
6
+ * File based locking to prevent jobs from running if they are already running. This is especially useful if a job takes longer than it should (such as an hourly cron that happens to take two hours to run).
7
+ * Stdout and Stderr interception.
8
+ * The concept of "tasks" within a job (more on this below) for ordering and exception handling.
9
+ * Custom handling of output (write your own email handling, custom logging, alerting, etc).
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'cron_helper'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install cron_helper
26
+
27
+ ## Usage
28
+
29
+ For your typical Rails or Ruby app, you will first want to create your ````ApplicationJob````.
30
+ This is the class that all of your custom jobs should inherit from.
31
+ Here you put methods that are shared by all your custom jobs.
32
+
33
+ ```ruby
34
+ class ApplicationJob < CronHelper::Job
35
+ end
36
+ ```
37
+
38
+ Next you will create your app specific job classes.
39
+ The idea is to pick logical names that relate to how they are going to be scheduled and grouped.
40
+ Inside of each class you will register your tasks.
41
+
42
+ ```ruby
43
+ class HourlyJob < ApplicationJob
44
+ register :do_some_work
45
+ register :do_some_other_work
46
+ register :do_even_more_work
47
+
48
+ private
49
+
50
+ def do_some_work
51
+ raise 'I am a task that breaks quite often.'
52
+ end
53
+
54
+ def do_some_other_work
55
+ puts 'I am a task that loves to talk.'
56
+ STDERR.puts 'I even like to talk about errors.'
57
+ end
58
+
59
+ def do_even_more_work
60
+ # I am a silent champion of a task.
61
+ end
62
+ end
63
+ ```
64
+ Finally you will schedule your jobs using the [Whenever](https://github.com/javan/whenever]) gem.
65
+ Below is an example config/schedule.rb that also forces your jobs to run at a low priority.
66
+ Running jobs at a low priority is recommended when your server has other roles (web, app, db, etc).
67
+
68
+ ```ruby
69
+ job_type :runner, "cd :path && nice -n 20 script/rails runner -e :environment ':task' :output"
70
+
71
+ every 1.hour do
72
+ runner('Cron::Hourly.new.run')
73
+ end
74
+ ```
75
+
76
+ ## Tasks
77
+ Think of tasks as methods with exception protection and ordering.
78
+ Tasks are guaranteed to run in the order you register them.
79
+ They are also guaranteed to run even if a previous task encountered an exception.
80
+ This gives you control over creating new tasks without having to worry about breaking old ones.
81
+
82
+ ## Development
83
+
84
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
85
+
86
+ 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).
87
+
88
+ ## Contributing
89
+
90
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cron_helper.
91
+
92
+
93
+ ## License
94
+
95
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
96
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cron_helper"
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
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,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cron_helper/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cron_helper"
8
+ spec.version = CronHelper::VERSION
9
+ spec.authors = ["Chad Remesch"]
10
+ spec.email = ["chad@remesch.com"]
11
+
12
+ spec.summary = %q{Cron Helper adds addition features to cron jobs scheduled by the Whenever gem.}
13
+ spec.homepage = "https://github.com/chadrem/cron_helper"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.11"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "minitest", "~> 5.0"
24
+ end
@@ -0,0 +1,6 @@
1
+ require 'cron_helper/version'
2
+ require 'cron_helper/job'
3
+
4
+ module CronHelper
5
+ end
6
+
@@ -0,0 +1,103 @@
1
+ module CronHelper
2
+ class Job
3
+ def self.register(method_name)
4
+ @cron_methods ||= []
5
+ @cron_methods << method_name
6
+ end
7
+
8
+ def run
9
+ backup_streams
10
+ hijack_streams
11
+
12
+ result = ''
13
+
14
+ file_lock do
15
+ cron_methods.each do |m|
16
+ @output.truncate(0)
17
+
18
+ begin
19
+ send(m)
20
+ rescue Exception => e
21
+ @output.string.chomp!
22
+ puts "\n" if @output.string.length > 0
23
+ puts "EXCEPTION #{e.class} (#{e.message.chomp})\n#{e.backtrace.join("\n")}\n"
24
+ ensure
25
+ if @output.string.length > 0
26
+ result << "#######################################################\n"
27
+ result << "#{self.class.to_s}##{m.to_s}\n"
28
+ result << "#######################################################\n"
29
+ result << "#{@output.string.chomp}\n"
30
+ result << "---\n\n"
31
+ end
32
+ end
33
+ end
34
+ end
35
+ ensure
36
+ restore_streams
37
+ output_handler(result)
38
+ end
39
+
40
+ private
41
+
42
+ def output_handler(output)
43
+ puts output
44
+ end
45
+
46
+ def cron_methods
47
+ self.class.instance_variable_get('@cron_methods') || []
48
+ end
49
+
50
+ def backup_streams
51
+ @stderr_backup = $stderr
52
+ @stdout_backup = $stdout
53
+ end
54
+
55
+ def restore_streams
56
+ return unless @stderr_backup && @stdout_backup
57
+
58
+ $stderr = @stderr_backup
59
+ $stdout = @stdout_backup
60
+
61
+ @stderr_backup = @stdout_backup = nil
62
+ end
63
+
64
+ def hijack_streams
65
+ @output = $stderr = $stdout = StringIO.new
66
+ end
67
+
68
+ def file_lock(&block)
69
+ FileUtils.mkdir_p(lock_dir_path)
70
+
71
+ File.open(lock_file_path, File::RDWR|File::CREAT, 0644) do |f|
72
+ unless f.flock(File::LOCK_EX|File::LOCK_NB)
73
+ puts "CRON FAILED TO LOCK (#{cron_name} at #{Time.zone.now})"
74
+ return
75
+ end
76
+
77
+ begin
78
+ yield
79
+ rescue Exception => e
80
+ puts "CRON EXCEPTION (#{cron_name} at #{Time.zone.now}): #{e.message}\n#{e.backtrace}"
81
+ ensure
82
+ f.flock(File::LOCK_UN)
83
+ end
84
+ end
85
+ end
86
+
87
+ def cron_name
88
+ return self.class.to_s
89
+ end
90
+
91
+ def lock_dir_path
92
+ return File.join(Rails.root, 'tmp', 'crons')
93
+ end
94
+
95
+ def lock_file_name
96
+ return "#{cron_name}.lock"
97
+ end
98
+
99
+ def lock_file_path
100
+ return File.join(lock_dir_path, lock_file_name)
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,3 @@
1
+ module CronHelper
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cron_helper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Chad Remesch
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-01-13 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description:
56
+ email:
57
+ - chad@remesch.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - cron_helper.gemspec
71
+ - lib/cron_helper.rb
72
+ - lib/cron_helper/job.rb
73
+ - lib/cron_helper/version.rb
74
+ homepage: https://github.com/chadrem/cron_helper
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.5.1
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Cron Helper adds addition features to cron jobs scheduled by the Whenever
98
+ gem.
99
+ test_files: []