minicron 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -163,20 +163,6 @@ CREATE TABLE `schema_migrations` (
163
163
 
164
164
 
165
165
 
166
- # Dump of table users
167
- # ------------------------------------------------------------
168
-
169
- DROP TABLE IF EXISTS `users`;
170
-
171
- CREATE TABLE `users` (
172
- `id` int(11) NOT NULL AUTO_INCREMENT,
173
- `email` int(11) NOT NULL,
174
- `password` int(11) NOT NULL,
175
- PRIMARY KEY (`id`)
176
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
177
-
178
-
179
-
180
166
 
181
167
  /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
182
168
  /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
@@ -53,7 +53,7 @@ module Minicron
53
53
  begin
54
54
  monitor(schedule)
55
55
  rescue Exception => e
56
- if Minicron.config['global']['trace']
56
+ if Minicron.config['trace']
57
57
  puts e.message
58
58
  puts e.backtrace
59
59
  end
@@ -36,7 +36,7 @@ module Minicron
36
36
 
37
37
  # TODO: Handle errors here!
38
38
  # Get the job and execution id from the response
39
- ids = JSON.parse(responses.first[:body]).first['channel'].split('/')[3]
39
+ ids = JSON.parse(@responses.first[:body]).first['channel'].split('/')[3]
40
40
 
41
41
  # Split them up
42
42
  ids = ids.split('-')
@@ -51,8 +51,9 @@ module Minicron
51
51
  # Helper that wraps the publish function making it quicker to use
52
52
  #
53
53
  # @option options [String] job_id
54
- # @option options [String, Symbol] type status or output
55
54
  # @option options [Integer] execution_id
55
+ # @option options [String, Symbol] type status or output
56
+ # @option options [String, Hash]
56
57
  def send(options = {})
57
58
  # Publish the message to the correct channel
58
59
  publish("/job/#{options[:job_id]}/#{options[:execution_id]}/#{options[:type]}", options[:message])
@@ -61,7 +62,7 @@ module Minicron
61
62
  # Publishes a message on the given channel to the server
62
63
  #
63
64
  # @param channel [String]
64
- # @param message [String]
65
+ # @param message [String, Hash]
65
66
  def publish(channel, message)
66
67
  # Set up the data to send to faye
67
68
  data = { :channel => channel, :data => {
@@ -96,7 +96,7 @@ module Minicron
96
96
 
97
97
  # Tidy up after we are done with the client
98
98
  def tidy_up
99
- EM.stop
99
+ EM.stop if EM.reactor_running?
100
100
  end
101
101
  end
102
102
  end
@@ -13,7 +13,7 @@ module Minicron
13
13
  Faye::WebSocket.load_adapter('thin')
14
14
 
15
15
  # Show debug verbose output if requested
16
- if Minicron.config['global']['verbose']
16
+ if Minicron.config['verbose']
17
17
  log = Logger.new(STDOUT)
18
18
  log.level = Logger::DEBUG
19
19
  Faye.logger = log
@@ -35,23 +35,23 @@ module Minicron
35
35
 
36
36
  def add_faye_events
37
37
  @server.on(:handshake) do |client_id|
38
- p [:handshake, client_id] if Minicron.config['global']['verbose']
38
+ p [:handshake, client_id] if Minicron.config['verbose']
39
39
  end
40
40
 
41
41
  @server.on(:subscribe) do |client_id, channel|
42
- p [:subscribe, client_id, channel] if Minicron.config['global']['verbose']
42
+ p [:subscribe, client_id, channel] if Minicron.config['verbose']
43
43
  end
44
44
 
45
45
  @server.on(:unsubscribe) do |client_id, channel|
46
- p [:unsubscribe, client_id, channel] if Minicron.config['global']['verbose']
46
+ p [:unsubscribe, client_id, channel] if Minicron.config['verbose']
47
47
  end
48
48
 
49
49
  @server.on(:publish) do |client_id, channel, data|
50
- p [:published, client_id, channel, data] if Minicron.config['global']['verbose']
50
+ p [:published, client_id, channel, data] if Minicron.config['verbose']
51
51
  end
52
52
 
53
53
  @server.on(:disconnect) do |client_id|
54
- p [:disconnect, client_id] if Minicron.config['global']['verbose']
54
+ p [:disconnect, client_id] if Minicron.config['verbose']
55
55
  end
56
56
  end
57
57
  end
@@ -5,18 +5,22 @@ module Minicron
5
5
  module Transport
6
6
  # Used to mangage the web server minicron runs on
7
7
  class Server
8
- attr_reader :server
8
+ @server = nil
9
+
10
+ class << self
11
+ attr_accessor :server
12
+ end
9
13
 
10
14
  # Starts the thin server
11
15
  #
12
16
  # @param host [String] the host e.g 0.0.0.0
13
17
  # @param port [Integer]
14
18
  # @param path [String] The absolute path to the server e.g /server
15
- def start!(host, port, path)
19
+ def self.start!(host, port, path)
16
20
  return false if running?
17
21
 
18
22
  # Start the faye or rails apps depending on the path
19
- server = Thin::Server.new(host, port) do
23
+ @server = Thin::Server.new(host, port) do
20
24
  use Rack::CommonLogger
21
25
  use Rack::ShowExceptions
22
26
 
@@ -37,25 +41,25 @@ module Minicron
37
41
  end
38
42
  end
39
43
 
40
- server.start
44
+ @server.start
41
45
  true
42
46
  end
43
47
 
44
48
  # Stops the thin server if it's running
45
49
  # @return [Boolean] whether the server was stopped or not
46
- def stop!
47
- return false unless running? && !server.nil?
50
+ def self.stop!
51
+ return false unless running? && !@server.nil?
48
52
 
49
- server.stop
53
+ @server.stop
50
54
  true
51
55
  end
52
56
 
53
57
  # Returns a bool based on whether
54
58
  # @return [Boolean]
55
- def running?
56
- return false unless !server.nil?
59
+ def self.running?
60
+ return false unless !@server.nil?
57
61
 
58
- server.running?
62
+ @server.running?
59
63
  end
60
64
  end
61
65
  end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ describe Minicron::PagerDuty do
4
+ describe '#intiailize' do
5
+ it 'should create an instance of the Pagerduty gem' do
6
+ pagerduty = Minicron::PagerDuty.new
7
+
8
+ expect(pagerduty.instance_variable_get(:@client)).to be_a Pagerduty
9
+ end
10
+ end
11
+
12
+ describe '#get_message' do
13
+ context 'when kind is miss' do
14
+ it 'should return the correct message' do
15
+ pagerduty = Minicron::PagerDuty.new
16
+ time = Time.now.utc
17
+ options = {
18
+ :job_id => 1,
19
+ :expected_at => time,
20
+ :execution_id => 2,
21
+ :kind => 'miss'
22
+ }
23
+ message = "Job #1 failed to execute at its expected time - #{time}"
24
+
25
+ expect(pagerduty.get_message(options)).to eq message
26
+ end
27
+ end
28
+
29
+ context 'when kind is fail' do
30
+ it 'should return the correct message' do
31
+ pagerduty = Minicron::PagerDuty.new
32
+ options = {
33
+ :job_id => 1,
34
+ :execution_id => 2,
35
+ :kind => 'fail'
36
+ }
37
+ message = "Execution #2 of Job #1 failed"
38
+
39
+ expect(pagerduty.get_message(options)).to eq message
40
+ end
41
+ end
42
+
43
+ context 'when kind is not supported' do
44
+ it 'should raise an Exception' do
45
+ pagerduty = Minicron::PagerDuty.new
46
+ options = {
47
+ :kind => 'derp'
48
+ }
49
+
50
+ expect do
51
+ pagerduty.get_message(options)
52
+ end.to raise_error Exception
53
+ end
54
+ end
55
+ end
56
+
57
+ describe '#send' do
58
+ it 'should trigger an alert on the pagerduty client' do
59
+ pagerduty = Minicron::PagerDuty.new
60
+
61
+ pagerduty.instance_variable_get(:@client).should_receive(:trigger).with('title', :message => 'yo')
62
+
63
+ pagerduty.send('title', 'yo')
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ describe Minicron::SMS do
4
+ before (:each) do
5
+ Minicron.parse_config_hash({
6
+ 'alerts' => {
7
+ 'sms' => {
8
+ 'twilio' => {
9
+ 'account_sid' => 'abc123',
10
+ 'auth_token' => 'abc456'
11
+ }
12
+ }
13
+ }
14
+ })
15
+ end
16
+
17
+ describe '#intiailize' do
18
+ it 'should create an instance of the Twilio gem' do
19
+ sms = Minicron::SMS.new
20
+
21
+ expect(sms.instance_variable_get(:@client)).to be_a Twilio::REST::Client
22
+ end
23
+ end
24
+
25
+ describe '#get_message' do
26
+ context 'when kind is miss' do
27
+ it 'should return the correct message' do
28
+ sms = Minicron::SMS.new
29
+ time = Time.now.utc
30
+ options = {
31
+ :job_id => 1,
32
+ :expected_at => time,
33
+ :execution_id => 2,
34
+ :kind => 'miss'
35
+ }
36
+ message = "minicron alert - job missed!\nJob #1 failed to execute at its expected time: #{time}"
37
+
38
+ expect(sms.get_message(options)).to eq message
39
+ end
40
+ end
41
+
42
+ context 'when kind is fail' do
43
+ it 'should return the correct message' do
44
+ sms = Minicron::SMS.new
45
+ options = {
46
+ :job_id => 1,
47
+ :execution_id => 2,
48
+ :kind => 'fail'
49
+ }
50
+ message = "minicron alert - job failed!\nExecution #2 of Job #1 failed"
51
+
52
+ expect(sms.get_message(options)).to eq message
53
+ end
54
+ end
55
+
56
+ context 'when kind is not supported' do
57
+ it 'should raise an Exception' do
58
+ sms = Minicron::SMS.new
59
+ options = {
60
+ :kind => 'derp'
61
+ }
62
+
63
+ expect do
64
+ sms.get_message(options)
65
+ end.to raise_error Exception
66
+ end
67
+ end
68
+ end
69
+ end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Minicron::CLI do
4
- let(:server) { Minicron::Transport::Server.new }
4
+ let(:server) { Minicron::Transport::Server }
5
5
  let(:thin_server) { Thin::Server }
6
6
 
7
7
  describe '#server' do
@@ -23,7 +23,7 @@ describe Minicron::CLI do
23
23
 
24
24
  context 'when in --dry-run mode' do
25
25
  it 'should run a simple command and print the output to stdout' do
26
- Minicron::CLI.new.run(['run', 'echo hello', '--dry-run', '--trace']) do |output|
26
+ Minicron::CLI.run(['run', 'echo hello', '--dry-run', '--trace']) do |output|
27
27
  expect(output.clean).to eq 'hello'
28
28
  end
29
29
  end
@@ -31,7 +31,7 @@ describe Minicron::CLI do
31
31
  it 'should run a simple multi-line command and print the output to stdout' do
32
32
  command_output = ''
33
33
 
34
- Minicron::CLI.new.run(['run', 'echo "hello\nworld"', '--dry-run', '--trace']) do |output|
34
+ Minicron::CLI.run(['run', 'echo "hello\nworld"', '--dry-run', '--trace']) do |output|
35
35
  command_output += output
36
36
  end
37
37
 
@@ -42,7 +42,7 @@ describe Minicron::CLI do
42
42
  it 'should return an error' do
43
43
  Minicron.capture_output :type => :stderr do
44
44
  expect do
45
- Minicron::CLI.new.run(%w(lol --trace))
45
+ Minicron::CLI.run(%w(gfdjgfdlgj --trace))
46
46
  end.to raise_error SystemExit
47
47
  end
48
48
  end
@@ -52,7 +52,7 @@ describe Minicron::CLI do
52
52
  it 'should raise ArgumentError' do
53
53
  Minicron.capture_output :type => :stderr do
54
54
  expect do
55
- Minicron::CLI.new.run(%w(run --dry-run --trace))
55
+ Minicron::CLI.run(%w(run --dry-run --trace))
56
56
  end.to raise_error ArgumentError
57
57
  end
58
58
  end
@@ -61,7 +61,7 @@ describe Minicron::CLI do
61
61
 
62
62
  context 'when in --dry-run mode with a valid --config file passed' do
63
63
  it 'should run a simple command and print the output to stdout' do
64
- Minicron::CLI.new.run(['run', 'echo hello', '--dry-run', '--trace', '--config', './default.config.toml']) do |output|
64
+ Minicron::CLI.run(['run', 'echo hello', '--dry-run', '--trace', '--config', './default.config.toml']) do |output|
65
65
  expect(output.clean).to eq 'hello'
66
66
  end
67
67
  end
@@ -69,7 +69,7 @@ describe Minicron::CLI do
69
69
  it 'should run a simple multi-line command and print the output to stdout' do
70
70
  command_output = ''
71
71
 
72
- Minicron::CLI.new.run(['run', 'echo "hello\nworld"', '--dry-run', '--trace', '--config', './default.config.toml']) do |output|
72
+ Minicron::CLI.run(['run', 'echo "hello\nworld"', '--dry-run', '--trace', '--config', './default.config.toml']) do |output|
73
73
  command_output += output
74
74
  end
75
75
 
@@ -80,7 +80,7 @@ describe Minicron::CLI do
80
80
  it 'should return an error' do
81
81
  Minicron.capture_output :type => :stderr do
82
82
  expect do
83
- Minicron::CLI.new.run(%w(lol --trace))
83
+ Minicron::CLI.run(['dfsfsdfsdfs', '--trace'])
84
84
  end.to raise_error SystemExit
85
85
  end
86
86
  end
@@ -90,28 +90,45 @@ describe Minicron::CLI do
90
90
  it 'should raise ArgumentError' do
91
91
  Minicron.capture_output :type => :stderr do
92
92
  expect do
93
- Minicron::CLI.new.run(['run', '--dry-run', '--trace', '--config', './default.config.toml'])
93
+ Minicron::CLI.run(['run', '--dry-run', '--trace', '--config', './default.config.toml'])
94
94
  end.to raise_error ArgumentError
95
95
  end
96
96
  end
97
97
  end
98
98
  end
99
+
100
+ context 'when run in --verbose mode' do
101
+ it 'should set the verbose to true' do
102
+ Minicron.capture_output :type => :stderr do
103
+ Minicron::CLI.run(['run', '--dry-run', '--trace', '--verbose', 'echo 1']) {}
104
+
105
+ expect(Minicron.config['verbose']).to eq true
106
+ end
107
+ end
108
+ end
99
109
  end
100
110
 
101
111
  describe '#run_command' do
102
112
  context 'when in verbose mode' do
103
113
  it 'a one line command should result in 7 total line' do
104
- minicron = Minicron::CLI.new
105
- minicron.disable_coloured_output!
114
+ Minicron::CLI.disable_coloured_output!
106
115
  output = ''
107
116
 
108
- minicron.run_command('echo 1', :verbose => true) do |line|
117
+ Minicron::CLI.run_command('echo 1', :verbose => true) do |line|
109
118
  output += line[:output]
110
119
  end
111
120
 
112
121
  expect(output.split("\n").length).to eq 7
113
122
  end
114
123
  end
124
+
125
+ context 'when a non-existent command is run' do
126
+ it 'should raise an Exception' do
127
+ expect do
128
+ Minicron::CLI.run_command('fdsfsdfsd') {}
129
+ end.to raise_error Exception
130
+ end
131
+ end
115
132
  end
116
133
 
117
134
  describe '#coloured_output?' do
@@ -119,7 +136,7 @@ describe Minicron::CLI do
119
136
  it 'should return true' do
120
137
  Rainbow.enabled = true
121
138
 
122
- expect(Minicron::CLI.new.coloured_output?).to eq true
139
+ expect(Minicron::CLI.coloured_output?).to eq true
123
140
  end
124
141
  end
125
142
 
@@ -127,28 +144,26 @@ describe Minicron::CLI do
127
144
  it 'should return false' do
128
145
  Rainbow.enabled = false
129
146
 
130
- expect(Minicron::CLI.new.coloured_output?).to eq false
147
+ expect(Minicron::CLI.coloured_output?).to eq false
131
148
  end
132
149
  end
133
150
  end
134
151
 
135
152
  describe '#enable_coloured_output!' do
136
153
  it 'should set Rainbow.enabled to true' do
137
- minicron = Minicron::CLI.new
138
- minicron.enable_coloured_output!
154
+ Minicron::CLI.enable_coloured_output!
139
155
 
140
156
  expect(Rainbow.enabled).to eq true
141
- expect(minicron.coloured_output?).to eq true
157
+ expect(Minicron::CLI.coloured_output?).to eq true
142
158
  end
143
159
  end
144
160
 
145
161
  describe '#disable_coloured_output!' do
146
162
  it 'should set Rainbow.enabled to false' do
147
- minicron = Minicron::CLI.new
148
- minicron.disable_coloured_output!
163
+ Minicron::CLI.disable_coloured_output!
149
164
 
150
165
  expect(Rainbow.enabled).to eq false
151
- expect(minicron.coloured_output?).to eq false
166
+ expect(Minicron::CLI.coloured_output?).to eq false
152
167
  end
153
168
  end
154
169
  end