Use Rails plugin generator and adapt

This commit is contained in:
Chris Seelus
2021-02-10 22:10:43 +01:00
parent 9b9193e1c8
commit d53520bbf0
22 changed files with 385 additions and 106 deletions

View File

@@ -2,77 +2,170 @@ PATH
remote: .
specs:
crono (1.1.2)
activerecord (>= 4.0)
activesupport (>= 4.0)
rails (>= 4.0)
rspec (>= 3.0)
GEM
remote: https://rubygems.org/
specs:
activemodel (5.0.0.1)
activesupport (= 5.0.0.1)
activerecord (5.0.0.1)
activemodel (= 5.0.0.1)
activesupport (= 5.0.0.1)
arel (~> 7.0)
activesupport (5.0.0.1)
actioncable (6.1.2)
actionpack (= 6.1.2)
activesupport (= 6.1.2)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
actionmailbox (6.1.2)
actionpack (= 6.1.2)
activejob (= 6.1.2)
activerecord (= 6.1.2)
activestorage (= 6.1.2)
activesupport (= 6.1.2)
mail (>= 2.7.1)
actionmailer (6.1.2)
actionpack (= 6.1.2)
actionview (= 6.1.2)
activejob (= 6.1.2)
activesupport (= 6.1.2)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
actionpack (6.1.2)
actionview (= 6.1.2)
activesupport (= 6.1.2)
rack (~> 2.0, >= 2.0.9)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.2.0)
actiontext (6.1.2)
actionpack (= 6.1.2)
activerecord (= 6.1.2)
activestorage (= 6.1.2)
activesupport (= 6.1.2)
nokogiri (>= 1.8.5)
actionview (6.1.2)
activesupport (= 6.1.2)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
activejob (6.1.2)
activesupport (= 6.1.2)
globalid (>= 0.3.6)
activemodel (6.1.2)
activesupport (= 6.1.2)
activerecord (6.1.2)
activemodel (= 6.1.2)
activesupport (= 6.1.2)
activestorage (6.1.2)
actionpack (= 6.1.2)
activejob (= 6.1.2)
activerecord (= 6.1.2)
activesupport (= 6.1.2)
marcel (~> 0.3.1)
mimemagic (~> 0.3.2)
activesupport (6.1.2)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (~> 0.7)
minitest (~> 5.1)
tzinfo (~> 1.1)
arel (7.1.4)
byebug (9.0.6)
concurrent-ruby (1.0.2)
daemons (1.2.4)
diff-lcs (1.2.5)
haml (4.0.7)
tilt
i18n (0.7.0)
minitest (5.9.1)
rack (1.6.5)
rack-protection (1.5.3)
rack
rack-test (0.6.3)
rack (>= 1.0)
rake (11.3.0)
rspec (3.5.0)
rspec-core (~> 3.5.0)
rspec-expectations (~> 3.5.0)
rspec-mocks (~> 3.5.0)
rspec-core (3.5.4)
rspec-support (~> 3.5.0)
rspec-expectations (3.5.0)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
zeitwerk (~> 2.3)
builder (3.2.4)
byebug (11.1.3)
concurrent-ruby (1.1.8)
crass (1.0.6)
daemons (1.3.1)
diff-lcs (1.4.4)
erubi (1.10.0)
globalid (0.4.2)
activesupport (>= 4.2.0)
i18n (1.8.8)
concurrent-ruby (~> 1.0)
loofah (2.9.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
mail (2.7.1)
mini_mime (>= 0.1.1)
marcel (0.3.3)
mimemagic (~> 0.3.2)
method_source (1.0.0)
mimemagic (0.3.5)
mini_mime (1.0.2)
minitest (5.14.3)
nio4r (2.5.5)
nokogiri (1.11.1-arm64-darwin)
racc (~> 1.4)
racc (1.5.2)
rack (2.2.3)
rack-test (1.1.0)
rack (>= 1.0, < 3)
rails (6.1.2)
actioncable (= 6.1.2)
actionmailbox (= 6.1.2)
actionmailer (= 6.1.2)
actionpack (= 6.1.2)
actiontext (= 6.1.2)
actionview (= 6.1.2)
activejob (= 6.1.2)
activemodel (= 6.1.2)
activerecord (= 6.1.2)
activestorage (= 6.1.2)
activesupport (= 6.1.2)
bundler (>= 1.15.0)
railties (= 6.1.2)
sprockets-rails (>= 2.0.0)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.3.0)
loofah (~> 2.3)
railties (6.1.2)
actionpack (= 6.1.2)
activesupport (= 6.1.2)
method_source
rake (>= 0.8.7)
thor (~> 1.0)
rake (13.0.3)
rspec (3.10.0)
rspec-core (~> 3.10.0)
rspec-expectations (~> 3.10.0)
rspec-mocks (~> 3.10.0)
rspec-core (3.10.1)
rspec-support (~> 3.10.0)
rspec-expectations (3.10.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-mocks (3.5.0)
rspec-support (~> 3.10.0)
rspec-mocks (3.10.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-support (3.5.0)
sinatra (1.4.7)
rack (~> 1.5)
rack-protection (~> 1.4)
tilt (>= 1.3, < 3)
sqlite3 (1.3.12)
thread_safe (0.3.5)
tilt (2.0.5)
timecop (0.8.1)
tzinfo (1.2.2)
thread_safe (~> 0.1)
rspec-support (~> 3.10.0)
rspec-support (3.10.2)
sprockets (4.0.2)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (3.2.2)
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
sqlite3 (1.4.2)
thor (1.1.0)
timecop (0.9.4)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
websocket-driver (0.7.3)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
zeitwerk (2.4.2)
PLATFORMS
ruby
arm64-darwin-20
DEPENDENCIES
bundler (>= 1.0.0)
byebug
crono!
daemons
haml
rack-test
rake (>= 10.0)
rspec (>= 3.0)
sinatra
sqlite3
timecop (>= 0.7)
BUNDLED WITH
1.13.6
2.2.7

View File

@@ -171,19 +171,13 @@ There are "start", "stop", and "restart" commands.
## Web UI
Crono comes with a Sinatra application that can display the current state of Crono jobs.
Add `sinatra` and `haml` to your Gemfile
```ruby
gem 'haml'
gem 'sinatra', require: nil
```
Crono can display the current state of Crono jobs.
Add the following to your `config/routes.rb`:
```ruby
Rails.application.routes.draw do
mount Crono::Web, at: '/crono'
mount Crono::Engine, at: '/crono'
...
```

View File

@@ -1,6 +1,5 @@
require 'bundler/gem_tasks'
require "bundler/setup"
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new('spec')
load "rails/tasks/statistics.rake"
task default: :spec
require "bundler/gem_tasks"

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,26 @@
nav {
position: relative;
z-index: 2;
}
nav .brand-logo {
font-size: 1.75em;
letter-spacing: -0.05em;
}
main {
position: relative;
z-index: 1;
padding: 20px;
}
main .page-footer {
margin: 0 -20px -20px -20px;
}
.page-footer > .footer-copyright {
padding: 20px;
}
.responsive-overflow {
overflow-x: auto;
}

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,5 @@
module Crono
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
end
end

View File

@@ -0,0 +1,11 @@
module Crono
class JobsController < ApplicationController
def index
@jobs = Crono::CronoJob.all
end
def show
@job = Crono::CronoJob.find(params[:id])
end
end
end

View File

@@ -0,0 +1,54 @@
<header>
<h4>Running Jobs</h4>
</header>
<div class="responsive-overflow">
<table id="job_list">
<tr>
<th>Job</th>
<th>Last performed at</th>
<th>Status</th>
<th></th>
</tr>
<% @jobs.each do |job| %>
<tr>
<td>
<%= job.job_id %>
</td>
<td>
<%= job.last_performed_at || 'Never performed yet' %>
</td>
<td>
<% if job.last_performed_at.nil? %>
<span class="grey-text darken-3" title="This job has never been performed yet.">
<i class="mdi-device-access-time"></i>
Pending
</span>
<% elsif job.healthy %>
<a href="<%= job_path(job) %>">
<span class="green-text darken-3" title="This job was performed successfully.">
<i class="mdi-action-done"></i>
Success
</span>
</a>
<% else %>
<a href="<%= job_path(job) %>">
<span class="red-text" title="There were problems with this job. Follow the link to check the log.">
<i class="mdi-alert-warning"></i>
Error
</span>
</a>
<% end %>
</td>
<td>
<% if job.last_performed_at %>
<a href="<%= job_path(job) %>" class="right">
Log
<i class="mdi-navigation-chevron-right"></i>
</a>
<% end %>
</td>
</tr>
<% end %>
</table>
</div>

View File

@@ -0,0 +1,17 @@
<header class="blue-grey lighten-4 grey-text text-darken-4">
<a href="<%= root_url %>">
<i class="mdi-navigation-chevron-left"></i>
&lsaquo; Back to Home
</a>
<h4>Last log of <i><%= @job.job_id %></i>:</h4>
</header>
<% if @job.healthy == false %>
<div class="row red-text">
<div class="s12 center-align">
<i class="mdi-alert-warning"></i>
An error occurs during the last execution of this job.
Check the log below for details.
</div>
</div>
<% end %>
<pre class="responsive-overflow"><%= @job.log %></pre>

View File

@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta content="IE=edge" http-equiv="X-UA-Compatible"/>
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" name="viewport"/>
<title>Crono Dashboard</title>
<%= stylesheet_link_tag 'crono/materialize.min.css', media: "all" %>
<%= stylesheet_link_tag 'crono/application', media: "all" %>
<%= csrf_meta_tags %>
</head>
<body class="blue-grey darken-2">
<nav class="blue-grey white-text" role="navigation">
<a class="brand-logo center" href="<%= root_path %>">
<b>Crono</b> Dashboard
</a>
</nav>
<main class="container blue-grey lighten-4">
<%= yield %>
<footer class="page-footer blue-grey darken-2">
<div class="footer-copyright blue-grey">
Crono v<%= Crono::VERSION %>
<a class="right" href="https://github.com/plashchynski/crono" target="_blank">Documentation</a>
</div>
</footer>
</main>
</body>
</html>

24
bin/rails Executable file
View File

@@ -0,0 +1,24 @@
#!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails gems
# installed from the root of your application.
ENGINE_ROOT = File.expand_path('..', __dir__)
ENGINE_PATH = File.expand_path('../lib/crono/engine', __dir__)
# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
require 'rails'
# Pick the frameworks you want:
require 'active_model/railtie'
require 'active_job/railtie'
require 'active_record/railtie'
require 'active_storage/engine'
require 'action_controller/railtie'
require 'action_mailer/railtie'
require 'action_view/railtie'
require 'action_cable/engine'
# require "sprockets/railtie"
# require "rails/test_unit/railtie"
require 'rails/engine/commands'

4
config/routes.rb Normal file
View File

@@ -0,0 +1,4 @@
Crono::Engine.routes.draw do
resources :jobs
root 'jobs#index'
end

View File

@@ -1,7 +1,4 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'crono/version'
require_relative 'lib/crono/version'
Gem::Specification.new do |spec|
spec.name = 'crono'
@@ -14,21 +11,20 @@ Gem::Specification.new do |spec|
spec.homepage = 'https://github.com/plashchynski/crono'
spec.license = 'Apache-2.0'
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.files = Dir['{app,config,db,lib}/**/*', 'LICENSE', 'Rakefile', 'README.rdoc']
spec.bindir = 'exe' # http://bundler.io/blog/2015/03/20/moving-bins-to-exe.html
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.executables = ['crono']
spec.require_paths = ['lib']
spec.add_runtime_dependency 'activesupport', '>= 4.0'
spec.add_runtime_dependency 'activerecord', '>= 4.0'
spec.add_development_dependency 'rake', '>= 10.0'
spec.add_dependency 'rails', '>= 4.0'
spec.add_dependency 'rspec', '>= 3.0'
spec.add_development_dependency 'bundler', '>= 1.0.0'
spec.add_development_dependency 'rspec', '>= 3.0'
spec.add_development_dependency 'timecop', '>= 0.7'
spec.add_development_dependency 'sqlite3'
spec.add_development_dependency 'byebug'
spec.add_development_dependency 'sinatra'
spec.add_development_dependency 'haml'
spec.add_development_dependency 'rack-test'
spec.add_development_dependency 'daemons'
spec.add_development_dependency 'rack-test'
spec.add_development_dependency 'rake', '>= 10.0'
spec.add_development_dependency 'rspec', '>= 3.0'
spec.add_development_dependency 'sqlite3'
spec.add_development_dependency 'timecop', '>= 0.7'
end

View File

@@ -2,8 +2,10 @@
module Crono
end
require 'rails'
require 'active_support/all'
require 'crono/version'
require 'crono/engine'
require 'crono/logging'
require 'crono/period'
require 'crono/time_of_day'
@@ -15,5 +17,3 @@ require 'crono/performer_proxy'
require 'crono/cronotab'
require 'crono/orm/active_record/crono_job'
require 'crono/railtie' if defined?(Rails)
Crono.autoload :Web, 'crono/web'

9
lib/crono/engine.rb Normal file
View File

@@ -0,0 +1,9 @@
module Crono
class Engine < ::Rails::Engine
isolate_namespace Crono
initializer 'crono.assets.precompile' do |app|
app.config.assets.precompile += %w( crono/application.css crono/materialize.min.css )
end
end
end

View File

@@ -76,7 +76,7 @@ module Crono
end
def perform_job
performer.new.perform *JSON.parse(job_args)
performer.new.perform JSON.parse(job_args)
rescue StandardError => e
handle_job_fail(e)
else

View File

@@ -3,6 +3,7 @@ require 'active_record'
module Crono
# Crono::CronoJob is a ActiveRecord model to store job state
class CronoJob < ActiveRecord::Base
self.table_name = 'crono_jobs'
validates :job_id, presence: true, uniqueness: true
def self.outdated

View File

@@ -7,8 +7,8 @@ module Crono
@job_args = job_args
end
def every(period, *args)
@job = Job.new(@performer, Period.new(period, *args), @job_args, @options)
def every(period, **options)
@job = Job.new(@performer, Period.new(period, **options), @job_args, @options)
@scheduler.add_job(@job)
self
end

View File

@@ -1,22 +0,0 @@
require 'haml'
require 'sinatra/base'
require 'crono'
module Crono
# Web is a Web UI Sinatra app
class Web < Sinatra::Base
set :root, File.expand_path(File.dirname(__FILE__) + '/../../web')
set :public_folder, proc { "#{root}/assets" }
set :views, proc { "#{root}/views" }
get '/' do
@jobs = Crono::CronoJob.all
haml :dashboard, format: :html5
end
get '/job/:id' do
@job = Crono::CronoJob.find(params[:id])
haml :job
end
end
end

View File

View File