mirror of
https://github.com/plashchynski/crono.git
synced 2026-07-06 21:05:19 +02:00
Add Job class
This commit is contained in:
@@ -0,0 +1,15 @@
|
|||||||
|
# cronotab.rb — Crono configuration example file
|
||||||
|
#
|
||||||
|
# Here you can specify periodic jobs and their schedule.
|
||||||
|
# You can specify a periodic job as a ActiveJob class in `app/jobs/`
|
||||||
|
# Actually you can use any class. The only requirement is that
|
||||||
|
# the class should implement a method `perform` without arguments.
|
||||||
|
#
|
||||||
|
|
||||||
|
class TestJob
|
||||||
|
def perform
|
||||||
|
puts "Test!"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Crono.perform(TestJob).every 5.second
|
||||||
@@ -4,6 +4,7 @@ end
|
|||||||
require "active_support/all"
|
require "active_support/all"
|
||||||
require "crono/version.rb"
|
require "crono/version.rb"
|
||||||
require "crono/period.rb"
|
require "crono/period.rb"
|
||||||
|
require "crono/job.rb"
|
||||||
require "crono/schedule.rb"
|
require "crono/schedule.rb"
|
||||||
require "crono/config.rb"
|
require "crono/config.rb"
|
||||||
require "crono/performer_proxy.rb"
|
require "crono/performer_proxy.rb"
|
||||||
|
|||||||
+8
-15
@@ -3,17 +3,15 @@ require 'optparse'
|
|||||||
|
|
||||||
module Crono
|
module Crono
|
||||||
mattr_accessor :schedule
|
mattr_accessor :schedule
|
||||||
|
mattr_accessor :logger
|
||||||
|
|
||||||
class CLI
|
class CLI
|
||||||
include Singleton
|
include Singleton
|
||||||
attr_accessor :config
|
attr_accessor :config
|
||||||
attr_accessor :schedule
|
|
||||||
attr_accessor :logger
|
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
self.config = Config.new
|
self.config = Config.new
|
||||||
self.schedule = Schedule.new
|
Crono.schedule = Schedule.new
|
||||||
Crono.schedule = schedule
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def run
|
def run
|
||||||
@@ -44,12 +42,12 @@ module Crono
|
|||||||
|
|
||||||
def init_logger
|
def init_logger
|
||||||
logfile = config.daemonize ? config.logfile : STDOUT
|
logfile = config.daemonize ? config.logfile : STDOUT
|
||||||
self.logger = Logger.new(logfile)
|
Crono.logger = Logger.new(logfile)
|
||||||
end
|
end
|
||||||
|
|
||||||
def print_banner
|
def print_banner
|
||||||
logger.info "Loading Crono #{Crono::VERSION}"
|
Crono.logger.info "Loading Crono #{Crono::VERSION}"
|
||||||
logger.info "Running in #{RUBY_DESCRIPTION}"
|
Crono.logger.info "Running in #{RUBY_DESCRIPTION}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def load_rails
|
def load_rails
|
||||||
@@ -60,16 +58,11 @@ module Crono
|
|||||||
require File.expand_path(config.cronotab)
|
require File.expand_path(config.cronotab)
|
||||||
end
|
end
|
||||||
|
|
||||||
def run_job(klass)
|
|
||||||
logger.info "Perform #{klass}"
|
|
||||||
Thread.new { klass.new.perform }
|
|
||||||
end
|
|
||||||
|
|
||||||
def start_working_loop
|
def start_working_loop
|
||||||
loop do
|
loop do
|
||||||
klass, time = schedule.next
|
job = Crono.schedule.next
|
||||||
sleep(time - Time.now)
|
sleep(job.next - Time.now)
|
||||||
run_job(klass)
|
job.perform
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
module Crono
|
||||||
|
class Job
|
||||||
|
attr_accessor :performer
|
||||||
|
attr_accessor :period
|
||||||
|
|
||||||
|
def initialize(performer, period)
|
||||||
|
self.performer, self.period = performer, period
|
||||||
|
end
|
||||||
|
|
||||||
|
def next
|
||||||
|
period.next
|
||||||
|
end
|
||||||
|
|
||||||
|
def perform
|
||||||
|
Crono.logger.info "Perform #{performer}"
|
||||||
|
Thread.new { performer.new.perform }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -6,7 +6,8 @@ module Crono
|
|||||||
end
|
end
|
||||||
|
|
||||||
def every(period, *args)
|
def every(period, *args)
|
||||||
@schedule.add(@performer, Period.new(period, *args))
|
job = Job.new(@performer, Period.new(period, *args))
|
||||||
|
@schedule.add(job)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,20 +1,22 @@
|
|||||||
module Crono
|
module Crono
|
||||||
class Schedule
|
class Schedule
|
||||||
|
attr_accessor :schedule
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@schedule = []
|
self.schedule = []
|
||||||
end
|
end
|
||||||
|
|
||||||
def add(peformer, period)
|
def add(job)
|
||||||
@schedule << [peformer, period]
|
schedule << job
|
||||||
end
|
end
|
||||||
|
|
||||||
def next
|
def next
|
||||||
[queue.first[0], queue.first[1].next]
|
queue.first
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def queue
|
def queue
|
||||||
@schedule.sort { |a,b| a[1].next <=> b[1].next }
|
schedule.sort { |a,b| a.next <=> b.next }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -18,13 +18,6 @@ describe Crono::CLI do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#run_job" do
|
|
||||||
it "should run job in separate thread" do
|
|
||||||
thread = cli.send(:run_job, TestJob).join
|
|
||||||
expect(thread).to be_stop
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "#start_working_loop" do
|
describe "#start_working_loop" do
|
||||||
it "should start working loop"
|
it "should start working loop"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
require "spec_helper"
|
||||||
|
|
||||||
|
class TestJob
|
||||||
|
def perform;end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe Crono::Job do
|
||||||
|
let(:period) { Crono::Period.new(2.day) }
|
||||||
|
let(:job) { Crono::Job.new(TestJob, period) }
|
||||||
|
|
||||||
|
it "should contain performer and period" do
|
||||||
|
expect(job.performer).to be TestJob
|
||||||
|
expect(job.period).to be period
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#perform" do
|
||||||
|
it "should run performer in separate thread" do
|
||||||
|
thread = job.perform.join
|
||||||
|
expect(thread).to be_stop
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -5,8 +5,8 @@ class TestJob
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe Crono::PerformerProxy do
|
describe Crono::PerformerProxy do
|
||||||
it "should add job and period to schedule" do
|
it "should add job to schedule" do
|
||||||
expect(Crono.schedule).to receive(:add).with(TestJob, kind_of(Crono::Period))
|
expect(Crono.schedule).to receive(:add).with(kind_of(Crono::Job))
|
||||||
Crono.perform(TestJob).every(2.days, at: "15:30")
|
Crono.perform(TestJob).every(2.days, at: "15:30")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
+14
-7
@@ -5,16 +5,23 @@ class TestJob
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe Crono::Schedule do
|
describe Crono::Schedule do
|
||||||
|
before(:each) do
|
||||||
|
@schedule = Crono::Schedule.new
|
||||||
|
@jobs = [
|
||||||
|
Crono::Period.new(3.day, at: "18:55"),
|
||||||
|
Crono::Period.new(1.day, at: "15:30"),
|
||||||
|
Crono::Period.new(7.day, at: "06:05")
|
||||||
|
].map { |period| Crono::Job.new(TestJob, period) }
|
||||||
|
@schedule.schedule = @jobs
|
||||||
|
end
|
||||||
|
|
||||||
describe "#next" do
|
describe "#next" do
|
||||||
it "should return next job in schedule" do
|
it "should return next job in schedule" do
|
||||||
@schedule = Crono::Schedule.new
|
expect(@schedule.next).to be @jobs[1]
|
||||||
[
|
end
|
||||||
Crono::Period.new(3.day, at: "18:55"),
|
|
||||||
Crono::Period.new(1.day, at: "15:30"),
|
|
||||||
Crono::Period.new(7.day, at: "06:05")
|
|
||||||
].each { |period| @schedule.add(TestJob, period) }
|
|
||||||
|
|
||||||
expect(@schedule.next).to be_eql([TestJob, 1.day.from_now.change(hour: 15, min: 30)])
|
it "should return next based on last" do
|
||||||
|
expect(@schedule.next)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user