class SBCP::Starbound

Constants

SESSION

Public Class Methods

new() click to toggle source
# File lib/sbcp/starbound.rb, line 30
def initialize
        @config = YAML.load_file(File.expand_path('../../../config.yml', __FILE__))
        SESSION[:info] = {
                :started    => nil,
                :uptime             => nil,
                :restart_in => nil,
        }
        SESSION[:players] = {}
        SESSION[:info][:restart_in] = 'Never' if @config['restart_schedule'] == 'disabled'
        backup_schedule = @config['backup_schedule']
        restart_schedule = @config['restart_schedule']
        scheduler = Rufus::Scheduler.new
        unless ['restart', 'none'].include? backup_schedule # Only run backups if they're not set to run at restart or aren't disabled
                if backup_schedule == 'hourly'
                        scheduler.cron "0 */1 * * *" do
                                Backup.create_backup
                        end
                elsif backup_schedule == 'daily'
                        scheduler.cron "0 0 * * *" do
                                Backup.create_backup
                        end
                else
                        scheduler.cron "0 */#{backup_schedule} * * *" do
                                Backup.create_backup
                        end
                end
        end
        unless restart_schedule == 'none' # Only schedule restarts if enabled
                if restart_schedule == 'hourly'
                        scheduler.cron "0 */1 * * *" do
                                pid = `pidof starbound_server`
                                system("kill -15 #{pid.to_i}") if not pid.empty?
                        end
                elsif restart_schedule == 'daily'
                        scheduler.cron "0 0 * * *" do
                                pid = `pidof starbound_server`
                                system("kill -15 #{pid.to_i}") if not pid.empty?
                        end
                else
                        scheduler.cron "0 */#{restart_schedule} * * *" do
                                pid = `pidof starbound_server`
                                system("kill -15 #{pid.to_i}") if not pid.empty?
                        end
                end
        end
end

Public Instance Methods

start() click to toggle source
# File lib/sbcp/starbound.rb, line 77
def start
        parser = Parser.new

        SESSION[:info][:started] = Time.now

        IO.popen("#{@config['starbound_directory']}/linux64/starbound_server", :chdir=>"#{@config['starbound_directory']}/linux64", :err=>[:child, :out]) do |output|
                while line = output.gets
                        parser.async.parse(line)
                end
        end

ensure
        parser.log("---------- Starbound has successfully shut down ----------\n")
        parser.log("\n") # Adds a newline space at the end of the log. Helpful for separating restarts in daily logs.
        parser.close
        parser.terminate
end