Skip to content

Commit 71703d5

Browse files
author
Prashanth Rajagopal
committed
initial
0 parents  commit 71703d5

22 files changed

+598
-0
lines changed

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*.db
2+
*.sqlite3
3+
.bundle/*
4+
log/*
5+
*.swp
6+
*.swo
7+
.env

.ruby-gemset

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cron-monitor

.ruby-version

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ruby-2.3.1

Gemfile

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
source 'https://rubygems.org'
2+
gem 'sinatra'
3+
gem 'json', '>= 1.8.3'
4+
gem 'dotenv'
5+
gem 'rake'
6+
gem 'data_mapper'
7+
gem 'dm-core'
8+
gem 'dm-mysql-adapter'
9+
gem 'dm-timestamps'
10+
gem 'dm-validations'
11+
gem 'dm-aggregates'
12+
gem 'dm-migrations'
13+
gem 'time_difference'
14+
gem 'pony'
15+
16+
group :development do
17+
gem 'foreman'
18+
gem "awesome_print", require:"ap"
19+
gem 'bullet'
20+
gem 'hirb'
21+
end
22+
23+
group :test do
24+
gem 'rspec', :require => 'spec'
25+
gem 'rack-test'
26+
end

Gemfile.lock

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
activesupport (5.0.0.1)
5+
concurrent-ruby (~> 1.0, >= 1.0.2)
6+
i18n (~> 0.7)
7+
minitest (~> 5.1)
8+
tzinfo (~> 1.1)
9+
addressable (2.4.0)
10+
awesome_print (1.7.0)
11+
bcrypt (3.1.11)
12+
bcrypt-ruby (3.1.5)
13+
bcrypt (>= 3.1.3)
14+
bullet (5.4.0)
15+
activesupport (>= 3.0.0)
16+
uniform_notifier (~> 1.10.0)
17+
concurrent-ruby (1.0.2)
18+
data_mapper (1.2.0)
19+
dm-aggregates (~> 1.2.0)
20+
dm-constraints (~> 1.2.0)
21+
dm-core (~> 1.2.0)
22+
dm-migrations (~> 1.2.0)
23+
dm-serializer (~> 1.2.0)
24+
dm-timestamps (~> 1.2.0)
25+
dm-transactions (~> 1.2.0)
26+
dm-types (~> 1.2.0)
27+
dm-validations (~> 1.2.0)
28+
data_objects (0.10.17)
29+
addressable (~> 2.1)
30+
diff-lcs (1.2.5)
31+
dm-aggregates (1.2.0)
32+
dm-core (~> 1.2.0)
33+
dm-constraints (1.2.0)
34+
dm-core (~> 1.2.0)
35+
dm-core (1.2.1)
36+
addressable (~> 2.3)
37+
dm-do-adapter (1.2.0)
38+
data_objects (~> 0.10.6)
39+
dm-core (~> 1.2.0)
40+
dm-migrations (1.2.0)
41+
dm-core (~> 1.2.0)
42+
dm-mysql-adapter (1.2.0)
43+
dm-do-adapter (~> 1.2.0)
44+
do_mysql (~> 0.10.6)
45+
dm-serializer (1.2.2)
46+
dm-core (~> 1.2.0)
47+
fastercsv (~> 1.5)
48+
json (~> 1.6)
49+
json_pure (~> 1.6)
50+
multi_json (~> 1.0)
51+
dm-timestamps (1.2.0)
52+
dm-core (~> 1.2.0)
53+
dm-transactions (1.2.0)
54+
dm-core (~> 1.2.0)
55+
dm-types (1.2.2)
56+
bcrypt-ruby (~> 3.0)
57+
dm-core (~> 1.2.0)
58+
fastercsv (~> 1.5)
59+
json (~> 1.6)
60+
multi_json (~> 1.0)
61+
stringex (~> 1.4)
62+
uuidtools (~> 2.1)
63+
dm-validations (1.2.0)
64+
dm-core (~> 1.2.0)
65+
do_mysql (0.10.17)
66+
data_objects (= 0.10.17)
67+
dotenv (2.1.1)
68+
fastercsv (1.5.5)
69+
foreman (0.82.0)
70+
thor (~> 0.19.1)
71+
hirb (0.7.3)
72+
i18n (0.7.0)
73+
json (1.8.3)
74+
json_pure (1.8.3)
75+
mail (2.6.4)
76+
mime-types (>= 1.16, < 4)
77+
mime-types (3.1)
78+
mime-types-data (~> 3.2015)
79+
mime-types-data (3.2016.0521)
80+
minitest (5.9.0)
81+
multi_json (1.12.1)
82+
pony (1.11)
83+
mail (>= 2.0)
84+
rack (1.6.4)
85+
rack-protection (1.5.3)
86+
rack
87+
rack-test (0.6.3)
88+
rack (>= 1.0)
89+
rake (11.3.0)
90+
rspec (3.5.0)
91+
rspec-core (~> 3.5.0)
92+
rspec-expectations (~> 3.5.0)
93+
rspec-mocks (~> 3.5.0)
94+
rspec-core (3.5.3)
95+
rspec-support (~> 3.5.0)
96+
rspec-expectations (3.5.0)
97+
diff-lcs (>= 1.2.0, < 2.0)
98+
rspec-support (~> 3.5.0)
99+
rspec-mocks (3.5.0)
100+
diff-lcs (>= 1.2.0, < 2.0)
101+
rspec-support (~> 3.5.0)
102+
rspec-support (3.5.0)
103+
sinatra (1.4.7)
104+
rack (~> 1.5)
105+
rack-protection (~> 1.4)
106+
tilt (>= 1.3, < 3)
107+
stringex (1.5.1)
108+
thor (0.19.1)
109+
thread_safe (0.3.5)
110+
tilt (2.0.5)
111+
time_difference (0.4.2)
112+
activesupport
113+
tzinfo (1.2.2)
114+
thread_safe (~> 0.1)
115+
uniform_notifier (1.10.0)
116+
uuidtools (2.1.5)
117+
118+
PLATFORMS
119+
ruby
120+
121+
DEPENDENCIES
122+
awesome_print
123+
bullet
124+
data_mapper
125+
dm-aggregates
126+
dm-core
127+
dm-migrations
128+
dm-mysql-adapter
129+
dm-timestamps
130+
dm-validations
131+
dotenv
132+
foreman
133+
hirb
134+
json (>= 1.8.3)
135+
pony
136+
rack-test
137+
rake
138+
rspec
139+
sinatra
140+
time_difference
141+
142+
BUNDLED WITH
143+
1.13.1

MIT-LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2015 Nick Plante
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Procfile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: bundle exec rackup -p $PORT

README.md

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Sinatra Application Template
2+
3+
A base Sinatra application template. Just fork and build. Yay!
4+
Includes Bundler, DataMapper, and RSpec, all ready to go.
5+
6+
## Configuration
7+
8+
Dependencies and all configuration is done in <tt>environment.rb</tt>. Your
9+
database is also set up here. DataMapper will use sqlite3 by default. Tests
10+
use the sqlite3-memory adapter (no configuration needed).
11+
12+
Add your controller actions in <tt>application.rb</tt>. Views for these actions
13+
are placed in the <tt>views</tt> directory. Static files, including a stock
14+
stylesheet, go in the <tt>public</tt> directory. Models go in the <tt>lib</tt>
15+
directory and are auto-loaded.
16+
17+
Environment variables that you want to expose to your application can be added
18+
in <tt>.env</tt>
19+
20+
## Testing
21+
22+
Add your specs in <tt>spec</tt>; just require <tt>spec_helper.rb</tt> to
23+
pre-configure the test environment. A number of samples are provided (including
24+
a sample model, which can be removed). To run the specs:
25+
26+
bundle exec rake spec
27+
28+
## Getting Started
29+
30+
bundle install
31+
bundle exec foreman start
32+
33+
## Extras
34+
35+
We've included a handy <tt>console</tt> script that fires up irb with your
36+
environment loaded. To load it, use the Rake task:
37+
38+
bundle exec rake console
39+
40+
## Thanks
41+
42+
This project includes contributions from [several awesome developers](https://github.com/zapnap/sinatra-template/graphs/contributors).
43+
44+
(c) 2015 Nick Plante. This code is distributed under the MIT license.

Rakefile

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require 'rubygems'
2+
require 'bundler/setup'
3+
require 'rspec/core/rake_task'
4+
require 'dotenv/tasks'
5+
6+
task :default => :test
7+
task :test => :spec
8+
9+
if !defined?(RSpec)
10+
puts "spec targets require RSpec"
11+
else
12+
desc "Run all examples"
13+
RSpec::Core::RakeTask.new(:spec) do |t|
14+
t.pattern = Dir['spec/**/*_spec.rb']
15+
end
16+
end
17+
18+
task :environment => :dotenv do
19+
require File.join(File.dirname(__FILE__), 'environment')
20+
end
21+
22+
task :console do
23+
ruby 'script/console'
24+
end

application.rb

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
require "rubygems"
2+
require "bundler/setup"
3+
require "sinatra"
4+
require 'time_difference'
5+
require File.join(File.dirname(__FILE__), "environment")
6+
7+
configure do
8+
set :views, "#{File.dirname(__FILE__)}/views"
9+
set :show_exceptions, :after_handler
10+
end
11+
12+
configure :production, :development do
13+
enable :logging
14+
end
15+
16+
def _log msg
17+
f = File.open("/tmp/sinatra.log", "a")
18+
f.puts msg
19+
f.close
20+
end
21+
22+
def populate_cron_runs(action,cron_name)
23+
if action == 'start'
24+
CronRun.create(:cron_id => Cron.first(:name => cron_name).id,:start_time => DateTime.now, :alert => 0)
25+
elsif action == 'complete'
26+
cr = CronRun.last(:cron_id => Cron.first(:name => cron_name).id)
27+
cr.update(:end_time => DateTime.now)
28+
end
29+
end
30+
31+
def alert cron_id, error_type, time, freq
32+
if error_type == 1
33+
_log "Cron #{Cron.first(:id => cron_id).name} started at #{time.strftime("%d-%m-%Y - %H:%M:%S")} but did not complete in #{freq} minutes"
34+
elsif error_type == 2
35+
_log "Cron #{Cron.first(:id => cron_id).name} did not start. It was supposed to run every #{freq} minutes. Last started at #{time.strftime("%d-%m-%Y - %H:%M:%S")}"
36+
end
37+
end
38+
39+
Thread.new do
40+
while true do
41+
_log "#{Time.now} --- running thread"
42+
Cron.each do |cron|
43+
begin
44+
CronRun.all(:cron_id => cron.id).each do |cr|
45+
if ( cr.end_time == nil && cr.alert == 0 )
46+
cron_exec_time = Cron.first(:id => cr.cron_id).exec_time
47+
if TimeDifference.between(cr.start_time,DateTime.now).in_minutes.to_i > cron_exec_time
48+
cr.update(:alert => 1)
49+
alert(cr.cron_id, 1, cr.start_time, cron_exec_time)
50+
end
51+
end
52+
end
53+
alert(cron.id, 2, CronRun.last(:cron_id => cron.id).start_time, cron.ping_freq) if TimeDifference.between(CronRun.last(:cron_id => cron.id).start_time, DateTime.now).in_minutes.to_i > cron.ping_freq
54+
rescue Exception => e
55+
_log e.message
56+
_log e.backtrace
57+
ensure
58+
next
59+
end
60+
end
61+
sleep 60
62+
end
63+
end
64+
65+
get "/" do
66+
@cron_hash = {}
67+
Cron.each do |cron|
68+
cr = CronRun.last(:cron_id => cron.id)
69+
@cron_hash[cron.name] = {:start_time => cr.start_time, :end_time => cr.end_time, :ping_freq => cron.ping_freq, :exec_freq => cron.exec_time}
70+
end
71+
erb :root
72+
end
73+
74+
get "/cron" do
75+
@cron = Cron.first(:name => params[:name])
76+
cron_run = CronRun.all(:cron_id => @cron.id, :order => [ :id.desc ], :limit => 20)
77+
@cron_hash = { @cron.name => [] }
78+
cron_run.each do |cr|
79+
@cron_hash[@cron.name] << {:start_time => cr.start_time, :end_time => cr.end_time, :ping_freq => @cron.ping_freq, :exec_freq => @cron.exec_time}
80+
end
81+
erb :cron
82+
end
83+
84+
post "/ping" do
85+
begin
86+
if Cron.first(:name => params[:name])
87+
populate_cron_runs params[:action], params[:name]
88+
else
89+
Cron.create(:name => params[:name], :exec_time => params[:et], :ping_freq => params[:pf])
90+
populate_cron_runs params[:action], params[:name]
91+
end
92+
rescue Exception => e
93+
puts e.message
94+
puts e.backtrace
95+
end
96+
end

config.ru

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require File.join(File.dirname(__FILE__), 'application')
2+
3+
set :run, false
4+
set :environment, :production
5+
6+
run Sinatra::Application

0 commit comments

Comments
 (0)