bin/diru in diru-0.0.1 vs bin/diru in diru-0.0.2

- old
+ new

@@ -199,13 +199,16 @@ @fav = {} # History limit. @histlimit = 20 - # Sync period. - @sync = 5 + # Sync period for options (and data if no @dsync). + @sync = 10 + # Sync period for data. + @dsync = nil + # Options File. @opts_file = opts_file # Temporary storage (scratch pad). @scratch = nil @@ -216,19 +219,26 @@ @logging = false # if Opt['log'].given # @logging = true # end - # Initial data update. - update_data - update_conf + # Data update thread. + @th_data = nil + # Options update thread. + @th_opts = nil + # Lock for DB access. @datalock = Mutex.new + @optslock = Mutex.new - # Start periodic DB updates. - periodic_update + # Initial data update. + update_conf + update_data + + start_th_data + start_th_opts end def opts_file @opts_file @@ -331,14 +341,27 @@ def update_conf if @opts_file load_conf( @opts_file ) + if @conf[:dsync] && @conf[:dsync] >= 0 + @dsync = @conf[:dsync] + end + if @conf[:sync] && @conf[:sync] >= 1 @sync = @conf[:sync] + @dsync = @sync unless @dsync end + if @dsync == 0 + kill_th_data + else + unless @th_data + start_th_data + end + end + if @conf[:hist] && ( @conf[:hist] >= 2 ) @histlimit = @conf[:hist] else @histlimit = 20 end @@ -411,38 +434,77 @@ # Update DB with MT sync. def update_data_sync @datalock.synchronize do update_data + end + end + + + # Update DB with MT sync. + def update_opts_sync + @optslock.synchronize do update_conf end end + # Kill search and cleanup resources. + def kill + kill_th_data + kill_th_opts + end + + # Reset dynamic state. def reset log "reset" @old = [] @book = [] @hist = [] update_data_sync end + # Start data thread. + def start_th_data + unless @dsync == 0 + @th_data = Thread.new do + loop do + # puts "data update..." + sleep @dsync + update_data_sync + end + end + end + end - # Update DB periodically with background thread. - def periodic_update - # Spawn More data updates. - @th = Thread.new do + + # Kill data thread. + def kill_th_data + Thread.kill( @th_data ) if @th_data + @th_data = nil + end + + + # Start opts thread. + def start_th_opts + @th_opts = Thread.new do loop do - # puts "data update..." sleep @sync - update_data_sync + update_opts_sync end end end + # Kill opts thread. + def kill_th_opts + Thread.kill( @th_opts ) if @th_opts + @th_opts = nil + end + + # Enable logging. def logon @logging = true end @@ -473,15 +535,25 @@ # Start in daemon or direct mode. def Hub.start( port, daemon = true ) if daemon p = fork do - Hub.new( port ) + begin + Hub.new( port ) + rescue + Diru.error "Could not start Hub!" + exit( false ) + end end Process.detach( p ) else - Hub.new( port ) + begin + Hub.new( port ) + rescue + Diru.error "Could not start Hub!" + exit( false ) + end end end # Server collection. @@ -572,12 +644,13 @@ self end # Kill service. def kill - @drb.stop_service + @search.kill Thread.kill( @th ) + @drb.stop_service end # Start service. def start @th = Thread.new do @@ -594,12 +667,13 @@ end if Opt['template'].given puts %q{--- -:hist: 20 -:sync: 10 +:hist: 20 +:sync: 10 +:dsync: 0 :favs: f: dir_0/dir_0_4/dir_0_4_0 g: dir_1/dir_1_2 :peers: - - "(.*/dir_0/.*)/dir_0_2_0" @@ -642,12 +716,16 @@ @search = DRbObject.new( nil, "druby://localhost:#{@port}" ) end if Opt['hkill'].given - hub = DRbObject.new( nil, "druby://localhost:#{hport}" ) - hub.kill + begin + hub = DRbObject.new( nil, "druby://localhost:#{hport}" ) + hub.kill + rescue + Diru.error "Could not kill Hub!" + end exit( true ) end if Opt['server'].given @@ -956,9 +1034,11 @@ when 'reset'; @search.reset when 'abook'; @search.abook( Dir.pwd ) when 'lbook'; lbook( @search.book ) when 'rbook'; @search.rbook when 'doc'; doc + when 'sync'; @search.update_opts_sync + when 'dsync'; @search.update_data_sync else return no_cd end no_cd end