Class: Aniview::Daemon
- Inherits:
-
Object
- Object
- Aniview::Daemon
- Defined in:
- lib/daemon.rb
Instance Method Summary collapse
- #checkFeeds ⇒ Object
- #help ⇒ Object
-
#initialize(args) ⇒ Daemon
constructor
A new instance of Daemon.
- #parseArgs(args) ⇒ Object
- #quit ⇒ Object
- #run ⇒ Object
- #uptime ⇒ Object
Constructor Details
#initialize(args) ⇒ Daemon
Returns a new instance of Daemon.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/daemon.rb', line 17 def initialize(args) @pref = Interface::Pref.new @c = Client::AniClient.new @pref args << "start" if args.length < 1 parseArgs args @schedule = Interface::Schedule.new @pref @delugec = Interface::DelugeC.new(@pref) @subscription = Interface::Subscription.new(@pref, @schedule, @delugec, @c) begin @s = TCPServer.new @pref.get("daemon")["port"] rescue Errno::EADDRINUSE puts "error, something is already listening on #{@pref.get("daemon")["port"]}, maybe the \nserver is already running?" quit end @looped = 0 @matches = 0 @lastchecked = "never" @nextcheck = 0 $stdout.reopen(@pref.parseDir(@pref.get("daemon_log_file")), "w") $stderr.reopen(@pref.parseDir(@pref.get("daemon_log_file")), "w") $stdout.sync = true $stderr.sync = true end |
Instance Method Details
#checkFeeds ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/daemon.rb', line 106 def checkFeeds Thread.abort_on_exception = true Thread.new do while true @subscription.updateFeed if @subscription.feed == nil @lastchecked = "offline" else @lastchecked = Time.now.to_s @schedule.load @matches += @subscription.matchAll(verbose: true) end @looped += 1 r_interval = Integer(@pref.get("rss_feed")["refresh_interval"]) (0..r_interval).each { |i| @nextcheck = r_interval - i sleep 1 } end end end |
#help ⇒ Object
100 101 102 103 104 |
# File 'lib/daemon.rb', line 100 def help cmds = ["help", "start", "stop", "info", "up?", "nani"] puts "commands:" puts cmds.sort end |
#parseArgs(args) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/daemon.rb', line 47 def parseArgs args args.each { |a| case a when "help" help quit when "start" if @c.server? puts "already running" quit end when "stop" if @c.server? puts "quitting" @c.stopDaemon quit else puts "server isn't running" quit end when "restart" if @c.server? @c.stopDaemon else puts "server not running, just starting it" end when "info" if @c.server? puts @c.info quit else puts "server isn't running" quit end when "up?" puts @c.server? quit when "nani" puts @c.sendMsg("what?") quit when "items" p @c.getItems quit when "log" puts @pref.parseDir(@pref.get("daemon_log_file")) quit else puts "unknown command, dude!" quit end } end |
#quit ⇒ Object
129 130 131 132 |
# File 'lib/daemon.rb', line 129 def quit @checkThread.exit unless @checkThread == nil exit end |
#run ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/daemon.rb', line 138 def run puts "--------" puts "server started" puts "server listening on port " + @pref.get("daemon")["port"] trap(:QUIT) do quit end @checkThread = checkFeeds @started = Time.now loop do lastloop = false client = @s.accept = client.gets.chomp.split(" ") #puts "got msg #{message} from client" response = "okay." .each_with_index { |m, i| case m when "up?" response = "true" when "quit" lastloop = true response = "quitting" when "info" response = "uptime: #{uptime}, loops: #{@looped}, matches, #{@matches}" when "lastchecked" response = @lastchecked when "items" response = Util.encode_object(@schedule.schedule) when "itemshash" response = @schedule.getAllCereal when "nextcheck" response = "#{@nextcheck}" else response = "what?" end } response += "\n" #puts "responding with #{response}" client.puts response client.close quit if lastloop end end |
#uptime ⇒ Object
134 135 136 |
# File 'lib/daemon.rb', line 134 def uptime Util.format_duration(Time.now - @started) end |