lib/cpee/implementation.rb in cpee-2.0.19 vs lib/cpee/implementation.rb in cpee-2.0.20
- old
+ new
@@ -14,10 +14,11 @@
require 'fileutils'
require 'redis'
require 'riddl/server'
require 'riddl/client'
+require_relative 'redis'
require_relative 'message'
require_relative 'persistence'
require_relative 'statemachine'
require_relative 'implementation_properties'
require_relative 'implementation_notifications'
@@ -68,16 +69,26 @@
opts[:backend_opts] ||= 'opts.yaml'
opts[:watchdog_frequency] ||= 7
opts[:watchdog_start_off] ||= false
opts[:backend_instance] ||= 'instance.rb'
opts[:infinite_loop_stop] ||= 10000
- opts[:redis_path] ||= '/tmp/redis.sock'
- opts[:redis_db] ||= 3
- opts[:sse_keepalive_frequency] ||= 10
+ ### set redis_cmd to nil if you want to do global
+ ### at least redis_path or redis_url and redis_db have to be set if you do global
+ opts[:redis_path] ||= 'redis.sock' # use e.g. /tmp/redis.sock for global stuff. Look it up in your redis config
+ opts[:redis_db] ||= 0
+ ### optional redis stuff
+ opts[:redis_url] ||= nil
+ opts[:redis_cmd] ||= 'redis-server --port 0 --unixsocket #redis_path# --unixsocketperm 600 --pidfile #redis_pid# --dir #redis_db_dir# --dbfilename #redis_db_name# --databases 1 --save 900 1 --save 300 10 --save 60 10000 --rdbcompression yes --daemonize yes'
+ opts[:redis_pid] ||= 'redis.pid' # use e.g. /var/run/redis.pid if you do global. Look it up in your redis config
+ opts[:redis_db_name] ||= 'redis.rdb' # use e.g. /var/lib/redis.rdb for global stuff. Look it up in your redis config
+
+ CPEE::redis_connect opts
+
+ opts[:sse_keepalive_frequency] ||= 10
opts[:sse_connections] = {}
- opts[:redis] = Redis.new(path: opts[:redis_path], db: opts[:redis_db])
+
opts[:statemachine] = CPEE::StateMachine.new opts[:states], %w{running simulating replaying finishing stopping abandoned finished} do |id|
opts[:redis].get("instance:#{id}/state")
end
opts[:runtime_cmds] << [
@@ -88,13 +99,13 @@
}
]
Proc.new do
parallel do
- CPEE::watch_services(opts[:watchdog_start_off])
+ CPEE::watch_services(opts[:watchdog_start_off],opts[:redis_url],File.join(opts[:basepath],opts[:redis_path]),opts[:redis_db])
EM.add_periodic_timer(opts[:watchdog_frequency]) do ### start services
- CPEE::watch_services(opts[:watchdog_start_off])
+ CPEE::watch_services(opts[:watchdog_start_off],opts[:redis_url],File.join(opts[:basepath],opts[:redis_path]),opts[:redis_db])
end
EM.defer do ### catch all sse connections
CPEE::Notifications::sse_distributor(opts)
end
EM.add_periodic_timer(opts[:sse_keepalive_frequency]) do
@@ -130,17 +141,21 @@
use CPEE::Callbacks::implementation(id.to_i, opts)
end
end
end
- def self::watch_services(watchdog_start_off)
+ def self::watch_services(watchdog_start_off,url,path,db)
return if watchdog_start_off
EM.defer do
Dir[File.join(__dir__,'..','..','server','routing','*.rb')].each do |s|
s = s.sub(/\.rb$/,'')
pid = (File.read(s + '.pid').to_i rescue nil)
if (pid.nil? || !(Process.kill(0, pid) rescue false)) && !File.exist?(s + '.lock')
- system "#{s}.rb restart 1>/dev/null 2>&1"
+ if url.nil?
+ system "#{s}.rb -p \"#{path}\" -d #{db} restart 1>/dev/null 2>&1"
+ else
+ system "#{s}.rb -u \"#{url}\" -d #{db} restart 1>/dev/null 2>&1"
+ end
puts "➡ Service #{File.basename(s,'.rb')} started ..."
end
end
end
end