Lint the whole project

This commit is contained in:
Dzmitry Plashchynski
2015-03-13 20:32:27 +02:00
parent 1aa27baca8
commit a9798acb35
23 changed files with 203 additions and 172 deletions

View File

@@ -1,14 +1,15 @@
# Crono main module
module Crono
end
require "active_support/all"
require "crono/version"
require "crono/logging"
require "crono/period"
require "crono/job"
require "crono/scheduler"
require "crono/config"
require "crono/performer_proxy"
require "crono/orm/active_record/crono_job"
require 'active_support/all'
require 'crono/version'
require 'crono/logging'
require 'crono/period'
require 'crono/job'
require 'crono/scheduler'
require 'crono/config'
require 'crono/performer_proxy'
require 'crono/orm/active_record/crono_job'
Crono.autoload :Web, "crono/web"
Crono.autoload :Web, 'crono/web'

View File

@@ -4,6 +4,7 @@ require 'optparse'
module Crono
mattr_accessor :scheduler
# Crono::CLI - Main class for the crono daemon exacutable `bin/crono`
class CLI
include Singleton
include Logging
@@ -18,12 +19,7 @@ module Crono
def run
parse_options(ARGV)
if config.daemonize
set_log_to(config.logfile)
daemonize
else
set_log_to(STDOUT)
end
setup_log
write_pid
load_rails
@@ -33,7 +29,17 @@ module Crono
start_working_loop
end
private
private
def setup_log
if config.daemonize
self.logifile = config.logfile
daemonize
else
self.logfile = STDOUT
end
end
def daemonize
::Process.daemon(true, true)
@@ -42,7 +48,7 @@ module Crono
io.sync = true
end
$stdin.reopen("/dev/null")
$stdin.reopen('/dev/null')
end
def write_pid
@@ -54,16 +60,16 @@ module Crono
logger.info "Loading Crono #{Crono::VERSION}"
logger.info "Running in #{RUBY_DESCRIPTION}"
logger.info "Jobs:"
logger.info 'Jobs:'
Crono.scheduler.jobs.each do |job|
logger.info %{"#{job.performer}" with rule "#{job.period.description}" next time will perform at #{job.next}}
logger.info %("#{job.performer}" with rule "#{job.period.description}" next time will perform at #{job.next})
end
end
def load_rails
ENV['RACK_ENV'] = ENV['RAILS_ENV'] = config.environment
require 'rails'
require File.expand_path("config/environment.rb")
require File.expand_path('config/environment.rb')
::Rails.application.eager_load!
require File.expand_path(config.cronotab)
end
@@ -75,7 +81,7 @@ module Crono
end
def start_working_loop
while job = Crono.scheduler.next do
while (job = Crono.scheduler.next)
sleep(job.next - Time.now)
job.perform
end

View File

@@ -1,8 +1,9 @@
module Crono
# Crono::Config stores Crono configuration
class Config
CRONOTAB = "config/cronotab.rb"
LOGFILE = "log/crono.log"
PIDFILE = "tmp/pids/crono.pid"
CRONOTAB = 'config/cronotab.rb'
LOGFILE = 'log/crono.log'
PIDFILE = 'tmp/pids/crono.pid'
attr_accessor :cronotab, :logfile, :pidfile, :daemonize, :environment
@@ -11,7 +12,7 @@ module Crono
self.logfile = LOGFILE
self.pidfile = PIDFILE
self.daemonize = false
self.environment = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || "development"
self.environment = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
end
end
end

View File

@@ -2,10 +2,12 @@ require 'stringio'
require 'logger'
module Crono
# Crono::Job represents a Crono job
class Job
include Logging
attr_accessor :performer, :period, :last_performed_at, :job_log, :job_logger, :healthy
attr_accessor :performer, :period, :last_performed_at, :job_log,
:job_logger, :healthy
def initialize(performer, period)
self.performer, self.period = performer, period
@@ -36,10 +38,14 @@ module Crono
def save
@semaphore.synchronize do
log = model.reload.log || ""
log = model.reload.log || ''
log << job_log.string
job_log.truncate(job_log.rewind)
model.update(last_performed_at: last_performed_at, log: log, healthy: healthy)
model.update(
last_performed_at: last_performed_at,
log: log,
healthy: healthy
)
end
end
@@ -47,20 +53,21 @@ module Crono
self.last_performed_at = model.last_performed_at
end
private
private
def perform_job
begin
performer.new.perform
rescue Exception => e
log_error "Finished #{performer} in %.2f seconds with error: #{e.message}" % (Time.now - last_performed_at)
log_error e.backtrace.join("\n")
self.healthy = false
else
self.healthy = true
log "Finished #{performer} in %.2f seconds" % (Time.now - last_performed_at)
ensure
save
end
performer.new.perform
finished_time_sec = format('%.2f', Time.now - last_performed_at)
rescue StandardError => e
log_error "Finished #{performer} in #{finished_time_sec} seconds"\
"with error: #{e.message}"
log_error e.backtrace.join("\n")
self.healthy = false
else
self.healthy = true
log "Finished #{performer} in #{finished_time_sec} seconds"
ensure
save
end
def log_error(message)

View File

@@ -1,8 +1,9 @@
module Crono
mattr_accessor :logger
# Crono::Logging is a standart Ruby logger wrapper
module Logging
def set_log_to(logfile)
def logfile=(logfile)
Crono.logger = Logger.new(logfile)
end

View File

@@ -1,8 +1,9 @@
require 'active_record'
module Crono
# Crono::CronoJob is a ActiveRecord model to store job state
class CronoJob < ActiveRecord::Base
self.table_name = "crono_jobs"
self.table_name = 'crono_jobs'
validates :job_id, presence: true, uniqueness: true
end
end

View File

@@ -1,4 +1,5 @@
module Crono
# PerformerProxy is a proxy used in cronotab.rb semantic
class PerformerProxy
def initialize(performer, scheduler)
@performer = performer

View File

@@ -1,4 +1,5 @@
module Crono
# Period describe frequency of performing a task
class Period
def initialize(period, at: nil)
@period = period
@@ -17,7 +18,7 @@ module Crono
def description
desc = "every #{@period.inspect}"
desc += " at %.2i:%.2i" % [@at_hour, @at_min] if @at_hour && @at_min
desc += format(' at %.2i:%.2i', @at_hour, @at_min) if @at_hour && @at_min
desc
end
@@ -29,13 +30,14 @@ module Crono
when Hash
return at[:hour], at[:min]
else
raise "Unknown 'at' format"
fail "Unknown 'at' format"
end
end
private
private
def time_atts
{hour: @at_hour, min: @at_min}.compact
{ hour: @at_hour, min: @at_min }.compact
end
end
end

View File

@@ -1,4 +1,5 @@
module Crono
# Scheduler is a container for job list and queue
class Scheduler
attr_accessor :jobs
@@ -15,7 +16,8 @@ module Crono
queue.first
end
private
private
def queue
jobs.sort_by(&:next)
end

View File

@@ -2,10 +2,11 @@ require 'haml'
require 'sinatra/base'
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.new { "#{root}/assets" }
set :views, Proc.new { "#{root}/views" }
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

View File

@@ -11,8 +11,8 @@ module Crono
ActiveRecord::Generators::Base.next_migration_number(path)
end
desc "Installs crono and generates the necessary configuration files"
source_root File.expand_path("../templates", __FILE__)
desc 'Installs crono and generates the necessary configuration files'
source_root File.expand_path('../templates', __FILE__)
def copy_config
template 'cronotab.rb.erb', 'config/cronotab.rb'

View File

@@ -7,9 +7,9 @@
#
# class TestJob
# def perform
# puts "Test!"
# puts 'Test!'
# end
# end
#
# Crono.perform(TestJob).every 2.days, at: "15:30"
# Crono.perform(TestJob).every 2.days, at: '15:30'
#