crocodile 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: 3e35b8e2d866cda5902a83846d31abcff811cc74
4
+ data.tar.gz: 83e2ed773492325df10a068b6a8ed6b231df1531
5
+ SHA512:
6
+ metadata.gz: ddc93616e432e0e0b060d28cfa818a484e4f0234dd2a0f4fc1923a51e4b74e8669254fe6a342cea2a4dfc27add42d95affd9674ae3fb90eb45522d84f5d3040b
7
+ data.tar.gz: 9ce1f4006abaf5a3c067760bb211fd7d0b70e7d5fc725feca5d9474e7e834fd06d10cffe9ba145a41a492a77f4dd2a2953008a886ded5baf1b8bedc0daa95aa8
data/.gems ADDED
@@ -0,0 +1 @@
1
+ eventmachine -v 1.0.3
data/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ Copyright (C) 2014 Three Funky Monkeys
2
+
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/bin/crocodile ADDED
@@ -0,0 +1,121 @@
1
+ #! /usr/bin/env ruby
2
+ require 'optparse'
3
+ require 'eventmachine'
4
+
5
+ module Crocodile
6
+ def self.print_help
7
+ $stdout.puts "Usage: crocodile [-d] [-p /path/to/pids] job_name (start|stop)"
8
+ end
9
+ end
10
+
11
+ class CrocodileProcess
12
+ def initialize(job_name, pids_dir)
13
+ @name = job_name
14
+ @job_path = File.expand_path("jobs/#{job_name}")
15
+ @pid_path = File.expand_path("#{job_name}.pid", pids_dir)
16
+ end
17
+
18
+ def start
19
+ require @job_path
20
+ job_class = constantize(@name)
21
+
22
+ Signal.trap("INT") { EventMachine.stop }
23
+ Signal.trap("TERM") { EventMachine.stop }
24
+
25
+ EventMachine.run do
26
+ timer = EventMachine::PeriodicTimer.new(job_class.interval || 60) do
27
+ job_class.logger.info job_class.message
28
+ job_class.run
29
+ if job_class.one_run_only
30
+ timer.cancel
31
+ EventMachine.stop
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ def stop
38
+ if File.exists?(@pid_path)
39
+ Process.kill :TERM, File.open(@pid_path).read.strip.to_i
40
+ File.delete(@pid_path)
41
+ true
42
+ else
43
+ false
44
+ end
45
+ end
46
+
47
+ def daemonize!
48
+ Process.daemon(true)
49
+ File.open(@pid_path, File::RDWR|File::EXCL|File::CREAT, 0600) { |io| io.write(Process.pid) }
50
+ end
51
+
52
+ def clean
53
+ if File.exists?(@pid_path)
54
+ File.delete(@pid_path)
55
+ end
56
+ end
57
+
58
+ def constantize(name)
59
+ eval name.split("_").push("job").map(&:capitalize).join
60
+ end
61
+ end
62
+
63
+ opts = {}
64
+
65
+ OptionParser.new do |options|
66
+ options.on("-d", "--daemonize", "Run process in background") do |d|
67
+ opts[:daemonize] = d
68
+ end
69
+
70
+ options.on("-p", "--pids-dir=PIDS", "Where to store pids files") do |p|
71
+ opts[:pids_dir] = p
72
+ end
73
+ end.parse!
74
+
75
+ job_name = ARGV.shift
76
+ action = ARGV.shift
77
+
78
+ unless job_name
79
+ $stderr.puts "Must indicate a worker name"
80
+ Crocodile.print_help
81
+ exit 1
82
+ end
83
+
84
+ unless action
85
+ $stderr.puts "Must indicate an action"
86
+ Crocodile.print_help
87
+ exit 1
88
+ end
89
+
90
+ $stdout.sync = true
91
+
92
+ pids_dir = opts[:pids_dir] || File.expand_path("jobs/pids")
93
+
94
+ process = CrocodileProcess.new(job_name, pids_dir)
95
+
96
+ case action
97
+ when "start"
98
+ if opts[:daemonize]
99
+ process.daemonize!
100
+ end
101
+
102
+ $stdout.puts "Starting Job #{job_name} #{opts[:daemonize] ? "(daemonized)" : ""}"
103
+ process.start
104
+
105
+ when "stop"
106
+ $stdout.puts "Stopping Job #{job_name}"
107
+ if process.stop
108
+ $stdout.puts "Stopped"
109
+ else
110
+ $stderr.puts "PID file not found"
111
+ end
112
+
113
+ else
114
+ Crocodile.print_help
115
+ exit 2
116
+ end
117
+
118
+ at_exit do
119
+ process.clean
120
+ end
121
+
data/lib/crocodile.rb ADDED
@@ -0,0 +1 @@
1
+ require_relative './crocodile/crocodile_job'
@@ -0,0 +1,23 @@
1
+ require 'logger'
2
+
3
+ class CrocodileJob
4
+ def self.message
5
+ "Running..."
6
+ end
7
+
8
+ def self.interval
9
+ raise RuntimeError.new("Must reimplement self.interval in a subclass")
10
+ end
11
+
12
+ def self.run
13
+ raise RuntimeError.new("Must reimplement self.interval in a subclass")
14
+ end
15
+
16
+ def self.one_run_only
17
+ false
18
+ end
19
+
20
+ def self.logger
21
+ @@logger ||= Logger.new(STDOUT)
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: crocodile
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Lautaro Orazi
8
+ - Leonardo Mateo
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-07-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: eventmachine
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ description: A simple worker to run Ruby jobs periodically
29
+ email:
30
+ - taro@threefunkymonkeys.com
31
+ - kandalf@threefunkymonkeys.com
32
+ executables:
33
+ - crocodile
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - ".gems"
38
+ - LICENSE
39
+ - bin/crocodile
40
+ - lib/crocodile.rb
41
+ - lib/crocodile/crocodile_job.rb
42
+ homepage: https://github.com/threefunkymonkeys/crocodile
43
+ licenses:
44
+ - MIT
45
+ metadata: {}
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 2.2.1
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: Periodic Ruby jobs runner
66
+ test_files: []