Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added PID file for server

  • Loading branch information...
commit 3ed9490cacf641b1e84edb457bac57e7edd93a54 1 parent 79381b1
Alex Young authored
View
1  .gitignore
@@ -1,3 +1,4 @@
*.swp
+*.swo
.DS_Store
http/tmp
View
12 README.textile
@@ -43,7 +43,7 @@ The web app must be run alongside the server. The web app must be started in pr
The web app currently has no database dependencies, it's a wrapper that links cookies to JsChat server proxies. You can run it on port 80 by configuring Rack or an Apache proxy. I have Apache set up this way on "jschat.org":http://jschat.org.
-h3. Configuration files
+h3. Configuration Files
These are the default locations of the configuration files. You can override them with <code>--config=PATH</code>:
@@ -58,6 +58,16 @@ The file format is JSON, like this:
{ "port": 3001 }
</pre>
+h3. Server Configuration Options
+
+<pre>
+{
+ "port": integer,
+ "ip": "string: IP address to bind to",
+ "tmp_files": "string: path to tmp files (including PID file)"
+}
+</pre>
+
h3. Client Commands
* Change name or identify: <code>/nick name</code>
View
4 bin/jschat-server
@@ -4,6 +4,4 @@ require 'logger'
require 'jschat/server'
require 'jschat/server-options'
-EM.run do
- EM.start_server ServerConfig['ip'], ServerConfig['port'], JsChat
-end
+JsChat::Server.run!
View
BIN  lib/jschat/http/.jschat.rb.swo
Binary file not shown
View
19 lib/jschat/server-options.rb
@@ -1,4 +1,5 @@
require 'optparse'
+require 'tmpdir'
logger = nil
@@ -11,7 +12,8 @@
'port' => 6789,
'ip' => '0.0.0.0',
'logger' => logger,
- 'max_message_length' => 500
+ 'max_message_length' => 500,
+ 'tmp_files' => File.join(Dir::tmpdir, 'jschat')
}
# Command line options will overrides these
@@ -24,6 +26,17 @@ def load_options(path)
end
end
+def make_tmp_files
+ ServerConfig['use_tmp_files'] = false
+ if File.exists? ServerConfig['tmp_files']
+ ServerConfig['use_tmp_files'] = true
+ else
+ if Dir.mkdir ServerConfig['tmp_files']
+ ServerConfig['use_tmp_files'] = true
+ end
+ end
+end
+
options = {}
default_config_file = '/etc/jschat/config.json'
@@ -34,7 +47,8 @@ def load_options(path)
opts.separator ""
opts.on("-c", "--config=PATH", String, "Configuration file location (#{default_config_file}") { |o| options['config'] = o }
- opts.on("-p", "--port=port", String, "Port number") { |o| options['port'] = o }
+ opts.on("-p", "--port=PORT", String, "Port number") { |o| options['port'] = o }
+ opts.on("-t", "--tmp_files=PATH", String, "Temporary files location (including pid file)") { |o| options['tmp_files'] = o }
opts.on("--help", "-H", "This text") { puts opts; exit 0 }
opts.parse!
@@ -43,3 +57,4 @@ def load_options(path)
options = load_options(options['config'] || default_config_file).merge options
ServerConfig = ServerConfigDefaults.merge options
+make_tmp_files
View
31 lib/jschat/server.rb
@@ -11,6 +11,37 @@
module JsChat
STATELESS_TIMEOUT = 60
+ module Server
+ def self.pid_file_name
+ File.join(ServerConfig['tmp_files'], 'jschat.pid')
+ end
+
+ def self.write_pid_file
+ return unless ServerConfig['use_tmp_files']
+ File.open(pid_file_name, 'w') { |f| f << Process.pid }
+ end
+
+ def self.rm_pid_file
+ FileUtils.rm pid_file_name
+ end
+
+ def self.stop
+ rm_pid_file
+ end
+
+ def self.run!
+ write_pid_file
+
+ at_exit do
+ stop
+ end
+
+ EM.run do
+ EM.start_server ServerConfig['ip'], ServerConfig['port'], JsChat
+ end
+ end
+ end
+
class User
include JsChat::FloodProtection
Please sign in to comment.
Something went wrong with that request. Please try again.