class SBCP::Daemon

Public Class Methods

new() click to toggle source
# File lib/sbcp/daemon.rb, line 28
def initialize
        @config = YAML.load_file(File.expand_path('../../../config.yml', __FILE__))
end

Public Instance Methods

start() click to toggle source
# File lib/sbcp/daemon.rb, line 32
def start
        # Quick check for invalid config values.
        raise('Please run setup first.') if @config['starbound_directory'].nil?
        raise('Error - Invalid starbound directory') if not Dir.exist?(@config['starbound_directory']) && Dir.exist?(@config['starbound_directory'] + '/giraffe_storage')
        raise('Error - Invalid backup directory') if not Dir.exist?(@config['backup_directory'])
        raise('Error - Invalid backup schedule') if not ['hourly', 2, 3, 4, 6, 8, 12, 'daily', 'restart'].include? @config['backup_schedule']
        raise('Error - Invalid backup history') if not  @config['backup_history'] == 'none' || @config['backup_history'] >= 1
        raise('Error - Invalid log directory') if not Dir.exist?(@config['log_directory'])
        raise('Error - Invalid log history') if not @config['log_history'].is_a?(Integer) && @config['log_history'] >= 1
        raise('Error - Invalid log style') if not ['daily', 'restart'].include? @config['log_style']
        raise('Error - Invalid restart schedule') if not ['none', 'hourly', 2, 3, 4, 6, 8, 12, 'daily'].include? @config['restart_schedule']

        # Require any present plugins
        plugins_directory = "#{@config['starbound_directory']}/sbcp/plugins"
        $LOAD_PATH.unshift(plugins_directory)
        Dir[File.join(plugins_directory, '*.rb')].each {|file| require File.basename(file) }

        # We create an infinite loop so we can easily restart the server.
        loop do
                # Next we invoke the Starbound class to create an instance of the server.
                # This class will spawn a sub-process containing the server.
                Starbound.new.start

                # We wait for the server process to conclude before moving on.
                # This normally occurs after a shutdown, crash, or restart.
                # The daemon process will do nothing until the server closes.

                # Once the server has finished running, we'll want to age our logfiles.
                # We'll also take backups here if they've been set to behave that way.
                # There's probably a better way than sending config values as arguements, but...
                Logs.age(@config['log_directory'], @config['log_history']) if @config['log_style'] == 'restart'
                Backup.create_backup if @config['backup_schedule'] == 'restart'

                # Now we must determine if the server was closed intentionally.
                # If the server was shut down on purpose, we don't want to automatically restart it.
                # If the shutdown file exists, it was an intentional shutdown.
                # We break the loop which ends the method and closes the Daemon process.
                if not Dir.glob('/tmp/sb-shutdown*').empty?
                        $daemon = nil
                        break
                end

                # This delay is needed or some commands don't report back correctly
                sleep 5
        end
end