require 'serverengine' require 'flydata-core/logger' require 'flydata/helper/worker' # Require all helper files - lib_dir = File.absolute_path(File.join(__FILE__, '../../..')) FileUtils.cd(lib_dir) do Dir["flydata/helper/**/*.rb"].each { |file| require file } end module Flydata module Helper module Server include FlydataCore::Logger OVERWRITE_PARAMETERS = { :worker_type => 'thread', :supervisor => true, } def self.run(opts={}) # See ServerEngine documents for details: # https://github.com/frsyuki/serverengine ServerEngine.create(Server, Worker) do ConfigParser.parse_file(opts[:config_path]). merge(opts).merge(OVERWRITE_PARAMETERS) end.run end def initialize super $log = logger end attr_reader :action_ownership_channel # ServerEngine hook point def reload_config super setup_log_format log_info "reload_config" if @scheduler @scheduler.reload(config[:helper]) end end # ServerEngine hook point def before_run log_debug "before_run" helper_config = config[:helper] @action_ownership_channel = ActionOwnershipChannel.new @scheduler = Scheduler.new(helper_config, self) @scheduler.start end # ServerEngine hook point def after_run log_debug "after_run" @scheduler.shutdown if @scheduler if @action_ownership_channel && @action_ownership_channel.remote_actions_exist? log_error("[error] Unprocessed actions are left in action_ownership_channel queue #{@action_ownership_channel.dump}") end end def custom_log_items super.merge(prefix: '[server]') end def setup_log_format logger.datetime_format = "%Y-%m-%d %H:%M:%S %z " logger.formatter = proc do |severity, datetime, progname, msg| "#{datetime} helper.#{severity.to_s.downcase}: #{msg}\n" end end end end end