module DayPlanner

Constants

BUILD
MAJOR
MINOR
PRE
TINY
VERSION

Public Class Methods

activate() click to toggle source
# File lib/day_planner/base.rb, line 82
def activate
        @@master.kill if defined?(@@master)
        @@status = "running"

        if defined?(Rails) && Rails.logger
                Rails.logger.info("DayPlanner activated at #{Time.now.inspect}.")
        else
                puts "DayPlanner activated at #{Time.now.inspect}."
        end

        if ActiveRecord::Base.connection.table_exists?('day_planner_log')
                DayPlanner::Log.create(name: "Activating DayPlanner", interval: @@interval, datetime: Time.now)
        end

        @@master = Thread.new do
                begin
                        while true
                                check_schedule
                                sleep(interval)
                        end
                ensure
                        Rails.logger.flush
                end
        end
end
cancel(task) click to toggle source
# File lib/day_planner/base.rb, line 61
def cancel(task)
        task = DayPlanner::Task.find_by_name(task) if task.is_a?(String) || task.is_a?(Symbol)
        task = DayPlanner::Task.find(task) if task.is_a?(Integer)

        raise ArgumentError, "DayPlanner couldn't find this task" if task.nil? || !task.is_a?(DayPlanner::Task)

        @@tasks.select!{ |t| t.id != task.id }

        task.destroy
end
clear_log() click to toggle source
# File lib/day_planner/base.rb, line 41
def clear_log
        ActiveRecord::Base.connection.execute("DELETE FROM #{DayPlanner::Log.table_name} WHERE true;")
        DayPlanner::Log.reset_table_sequence
end
clear_tasks() click to toggle source
# File lib/day_planner/base.rb, line 35
def clear_tasks
        @@tasks = []
        ActiveRecord::Base.connection.execute("DELETE FROM #{DayPlanner::Task.table_name} WHERE true;")
        DayPlanner::Task.reset_table_sequence
end
deactivate() click to toggle source
# File lib/day_planner/base.rb, line 72
def deactivate
        @@master.kill if defined?(@@master)
        @@status = "stopped"
        @@tasks = []

        clear_tasks

        true
end
interval() click to toggle source
# File lib/day_planner/base.rb, line 27
def interval
        defined?(@@interval) ? @@interval : 60
end
interval=(value) click to toggle source
# File lib/day_planner/base.rb, line 31
def interval=(value)
        @@interval = value
end
schedule(options, &block) click to toggle source
# File lib/day_planner/base.rb, line 46
def schedule(options, &block)
        raise ArgumentError, "Failed to pass an options hash" unless options.is_a?(Hash)

        task = DayPlanner::Task.schedule(options)

        if !task.id.nil?
                task.block = block
                @@tasks.push(task)
        else
                raise ArgumentError, "Task creation failed. If you specified a name, was it unique?"
        end

        task
end
status() click to toggle source
# File lib/day_planner/base.rb, line 23
def status
        @@status
end
task(task) click to toggle source
# File lib/day_planner/base.rb, line 13
def task(task)
        if task.is_a?(Integer)
                @@tasks.select{ |t| t.id == task }.first
        elsif task.is_a?(String)
                @@task.select{ |t| t.name == task }.first
        elsif task.is_a?(DayPlanner::Task)
                @@task.select{ |t| t.id == task.id }.first
        end
end
tasks() click to toggle source
# File lib/day_planner/base.rb, line 9
def tasks
        @@tasks
end

Private Class Methods

check_schedule() click to toggle source
# File lib/day_planner/base.rb, line 109
def check_schedule
        begin
                DayPlanner.tasks.each_with_index do |t, idx|
                        time = Time.now
                        task = DayPlanner::Task.find(t.id)

                        if task.nil?
                                @@tasks.select!{ |item| item.id != t.id }
                        else
                                if task.last_execution.nil? || task.next_execution.nil? || (time > task.next_execution && time > task.last_execution + (task.interval / 2))
                                        last = task.last_execution
                                        task.last_execution = time

                                        if !task.next_execution.nil?
                                                task.next_execution += task.interval #move it up by interval, regardless of if it's getting executed late
                                        else
                                                task.next_execution = time + task.interval
                                        end

                                        task.save!

                                        t.log(last, time)
                                        t.block.call
                                end
                        end
                end
        rescue => e
                Rails.logger.error("DayPlanner: task threw error:\n#{e.inspect}")
        end
end