class SmartMachine::Grids::Scheduler

Public Class Methods

new() click to toggle source
# File lib/smart_machine/grids/scheduler.rb, line 6
def initialize
end
running!() click to toggle source
# File lib/smart_machine/grids/scheduler.rb, line 181
def self.running!
        unless system("docker inspect -f '{{.State.Running}}' 'scheduler'", [:out, :err] => File::NULL)
                raise "Scheduler is not running. Please start scheduler before scheduling"
        end
end

Public Instance Methods

backup_crontabs() click to toggle source
# File lib/smart_machine/grids/scheduler.rb, line 172
def backup_crontabs
        print "-----> Backing up latest crontabs ... "
        if system("crontab -l > #{SmartMachine.config.user_home_path}/.smartmachine/grids/scheduler/crontabs/`id -un`", out: File::NULL)
                puts "done"
        else
                puts "error"
        end
end
create() click to toggle source
# File lib/smart_machine/grids/scheduler.rb, line 34
def create
        unless system("docker image inspect #{scheduler_image_name}", [:out, :err] => File::NULL)
                print "-----> Creating image #{scheduler_image_name} ... "
                if system("docker image build -t #{scheduler_image_name} \
                                                --build-arg SMARTMACHINE_VERSION=#{SmartMachine.version} \
                                                #{SmartMachine.config.root_path}/lib/smart_machine/grids/scheduler", out: File::NULL)
                        puts "done"

                        up
                end
        end
end
destroy() click to toggle source
# File lib/smart_machine/grids/scheduler.rb, line 47
def destroy
        down

        if system("docker image inspect #{scheduler_image_name}", [:out, :err] => File::NULL)
                print "-----> Removing image #{scheduler_image_name} ... "
                if system("docker image rm #{scheduler_image_name}", out: File::NULL)
                        puts "done"
                end
        end
end
down() click to toggle source
# File lib/smart_machine/grids/scheduler.rb, line 88
def down
        if SmartMachine::Docker.running?
                # Stopping & Removing containers - in reverse order
                if system("docker inspect -f '{{.State.Running}}' 'scheduler'", [:out, :err] => File::NULL)
                        print "-----> Stopping container scheduler with image #{scheduler_image_name} ... "
                        if system("docker stop 'scheduler'", out: File::NULL)
                                puts "done"

                                print "-----> Removing container scheduler with image #{scheduler_image_name} ... "
                                if system("docker rm 'scheduler'", out: File::NULL)
                                        puts "done"
                                end
                        end
                else
                        puts "-----> Container 'scheduler' is currently not running."
                end
        end
end
install() click to toggle source
# File lib/smart_machine/grids/scheduler.rb, line 9
def install
        puts "-----> Installing Scheduler"

        ssh = SmartMachine::SSH.new
        commands = ["smartmachine runner scheduler create"]
        ssh.run commands

        puts "-----> Scheduler Installation Complete"
end
mysql(*args) click to toggle source
# File lib/smart_machine/grids/scheduler.rb, line 130
def mysql(*args)
        args.flatten!
        action = args.empty? ? '' : args.shift

        return unless ['start', 'stop'].include? action

        command = "whenever --set 'output=#{SmartMachine.config.user_home_path}/.smartmachine/grids/scheduler/crontabs/crontabs.log' --load-file #{SmartMachine.config.user_home_path}/.smartmachine/config/mysql/schedule.rb"

        if action == 'start'
                command += " --update-crontab"
                action_text = "Starting"
        elsif action == 'stop'
                command += " --clear-crontab"
                action_text = "Stopping"
        end

        print "-----> #{action_text} automatic backup schedule for mysql ... "
        if system(command, out: File::NULL)
                puts "done"
        else
                puts "error"
        end
ensure
        backup_crontabs
end
restore_crontabs() click to toggle source
# File lib/smart_machine/grids/scheduler.rb, line 160
def restore_crontabs
        if system("docker exec scheduler sh -c 'exec test -f #{SmartMachine.config.user_home_path}/.smartmachine/grids/scheduler/crontabs/`id -un`'")
                print "-----> Restoring latest crontabs ... "

                if system("docker exec scheduler sh -c 'exec crontab - < #{SmartMachine.config.user_home_path}/.smartmachine/grids/scheduler/crontabs/`id -un`'")
                        puts "done"
                else
                        puts "error"
                end
        end
end
scheduler_image_name() click to toggle source
# File lib/smart_machine/grids/scheduler.rb, line 156
def scheduler_image_name
        "smartmachine/scheduler:#{SmartMachine.version}"
end
start(*args) click to toggle source
# File lib/smart_machine/grids/scheduler.rb, line 108
def start(*args)
        args.flatten!
        type = args.empty? ? '--all' : args.shift

        self.class.running!

        if type == '--mysql'
                system("docker exec -i scheduler sh -c 'exec scheduler start --mysql'")
        end
end
stop(*args) click to toggle source
# File lib/smart_machine/grids/scheduler.rb, line 119
def stop(*args)
        args.flatten!
        type = args.empty? ? '--all' : args.shift

        self.class.running!

        if type == '--mysql'
                system("docker exec -i scheduler sh -c 'exec scheduler stop --mysql'")
        end
end
uninstall() click to toggle source
# File lib/smart_machine/grids/scheduler.rb, line 19
def uninstall
        puts "-----> Uninstalling Scheduler"

        ssh = SmartMachine::SSH.new
        commands = ["smartmachine runner scheduler destroy"]
        ssh.run commands

        puts "-----> Scheduler Uninstallation Complete"
end
up() click to toggle source
# File lib/smart_machine/grids/scheduler.rb, line 58
def up
        if SmartMachine::Docker.running?
                if system("docker image inspect #{scheduler_image_name}", [:out, :err] => File::NULL)
                        print "-----> Creating container scheduler with image #{scheduler_image_name} ... "
                        if system("docker create \
                                --name='scheduler' \
                                --env MAILTO=#{SmartMachine.config.sysadmin_email} \
                                --user `id -u` \
                                --workdir /home/`id -un`/.smartmachine \
                                --volume='#{SmartMachine.config.user_home_path}/.smartmachine/config:#{SmartMachine.config.user_home_path}/.smartmachine/config' \
                                --volume='#{SmartMachine.config.user_home_path}/.smartmachine/grids/scheduler:#{SmartMachine.config.user_home_path}/.smartmachine/grids/scheduler' \
                                --volume='#{SmartMachine.config.user_home_path}/.smartmachine/bin/smartmachine.sh:/usr/local/bundle/bin/smartmachine' \
                                --volume='/var/run/docker.sock:/var/run/docker.sock:ro' \
                                --restart='always' \
                                #{scheduler_image_name}", out: File::NULL)
                                puts "done"

                                print "-----> Starting container scheduler with image #{scheduler_image_name} ... "
                                if system("docker start scheduler", out: File::NULL)
                                        puts "done"

                                        restore_crontabs
                                else
                                        puts "error"
                                end
                        end
                end
        end
end
update() click to toggle source
# File lib/smart_machine/grids/scheduler.rb, line 29
def update
        uninstall
        install
end