Skip to content

Commit

Permalink
version 0.3.0
Browse files Browse the repository at this point in the history
got rid of unused daemonization code

worked out some kinks in Homebus::State

autmoatically load state
  • Loading branch information
romkey committed Aug 8, 2022
1 parent 1d1bdab commit 9a2d3f1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 110 deletions.
105 changes: 3 additions & 102 deletions lib/homebus/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ def initialize(options)
@options = options
@quit = false

# daemonization will change CWD so expand relative paths now
options[:logfile] = File.expand_path(logfile) if logfile?
options[:pidfile] = File.expand_path(pidfile) if pidfile?

@config = Homebus::Config.new
@config.load

@state = Homebus::State.new
@state.load!

login = @config.default_login
unless login
raise Homebus::App::NoDefaultLogin
Expand All @@ -36,17 +35,6 @@ def setup!
end

def run!
check_pid
daemonize if daemonize?
write_pid
trap_signals

if logfile?
redirect_output
elsif daemonize?
suppress_output
end

setup!

begin
Expand Down Expand Up @@ -159,93 +147,6 @@ def listen!
@provision_request.broker.listen!(-> (msg) { self.receive!(msg) })
end

def daemonize?
options[:daemonize]
end

def config_filename
options[:config_filename] || '.homebus.json'
end

def logfile
options[:logfile]
end

def pidfile
options[:pidfile]
end

def logfile?
!logfile.nil?
end

def pidfile?
!pidfile.nil?
end

def trap_signals
trap(:QUIT) do # graceful shutdown of run! loop
@quit = true
end
end

def suppress_output
$stderr.reopen('/dev/null', 'a')
$stdout.reopen($stderr)
end

def redirect_output
FileUtils.mkdir_p(File.dirname(logfile), :mode => 0755)
FileUtils.touch logfile
File.chmod(0644, logfile)
$stderr.reopen(logfile, 'a')
$stdout.reopen($stderr)
$stdout.sync = $stderr.sync = true
end

def daemonize
exit if fork
Process.setsid
exit if fork
Dir.chdir "/"
end

def check_pid
if pidfile?
case pid_status(pidfile)
when :running, :not_owned
puts "A server is already running. Check #{pidfile}"
exit(1)
when :dead
File.delete(pidfile)
end
end
end

def pid_status(pidfile)
return :exited unless File.exists?(pidfile)
pid = ::File.read(pidfile).to_i
return :dead if pid == 0
Process.kill(0, pid) # check process status
:running
rescue Errno::ESRCH
:dead
rescue Errno::EPERM
:not_owned
end

def write_pid
if pidfile?
begin
File.open(pidfile, ::File::CREAT | ::File::EXCL | ::File::WRONLY){|f| f.write("#{Process.pid}") }
at_exit { File.delete(pidfile) if File.exists?(pidfile) }
rescue Errno::EEXIST
check_pid
retry
end
end
end

def devices
if device
[ device ]
Expand Down
18 changes: 12 additions & 6 deletions lib/homebus/state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@
require 'json'

class Homebus::State
attr_accessor :store, :filename
attr_accessor :state, :filename

DEFAULT_STATE_FILENAME = '.homebus-state.json'

def initialize
@store = {}
@state = {}
@filename = DEFAULT_STATE_FILENAME

load!
if File.exist? @filename
load!
else
commit!
end
end

def clear!
@state = {}
end

def load!
@store = JSON.parse(File.read(@filename), symbolize_names: true)
if File.exists? @filename
@state = JSON.parse(File.read(@filename), symbolize_names: true)
end
end

def load
Expand All @@ -31,8 +37,8 @@ def load
end

def commit!
File.write(@filename, JSON.pretty_generate(@store))
FileUtils.chmod(0600, @login_config_filename)
File.write(@filename, JSON.pretty_generate(@state))
FileUtils.chmod(0600, @filename)
end

def commit
Expand Down
4 changes: 2 additions & 2 deletions lib/homebus/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Homebus
VERSION='0.23.1'
class Homebus
VERSION='0.3.0'
end

0 comments on commit 9a2d3f1

Please sign in to comment.