<commit_before>require 'rubygems'
require 'bundler/setup'

require "json"
require "date"
require "digest/sha1"
require 'logger'
require 'ffi-rzmq'

class Server < Logger::Application
  def initialize(options=nil)
    super("Dax::Server")
    self.level = Logger::INFO
    raise ArgumentError, "Need :db, :port, and :key" unless options.respond_to? 'has_key?'
    raise ArgumentError, "db instance (:db) not provided." if ! options.has_key? :db
    raise ArgumentError, "Listen uri (:listen) not provided." if ! options.has_key? :listen
    raise ArgumentError, "AES key (:key) not provided." if ! options.has_key? :key
    @db = options[:db]
    @listen = options[:listen]
    @key = options[:key]

    @zmq = ZMQ::Context.new(50)
    @socket = @zmq.socket(ZMQ::REP)
    @socket.bind(@listen)
    Thread::abort_on_exception = true
    @zthread = Thread.new do
      server_loop
    end
  end

  def cleanup
  end

  def context
    @zmq
  end

  def error_check(rc)
    if ZMQ::Util.resultcode_ok?(rc) || ZMQ::Util.errno ==  ZMQ::EAGAIN
      false
    else
      log ERROR, "Operation failed, errno [#{ZMQ::Util.errno}] description [#{ZMQ::Util.error_string}]"
      true
    end
  end
  
  private

  def server_loop
    @socket.setsockopt(ZMQ::LINGER, 1000)
    @socket.setsockopt(ZMQ::RCVTIMEO, 1000)
    until @shutdown
      msg = ''
      begin 
        rc = @socket.recv_string msg
        log DEBUG, "Received: #{msg}" unless rc < 0
        break if @shutdown
      end while rc < 0 && ZMQ::Util.errno == ZMQ::EAGAIN
      raise "Could not receive message." if error_check(rc)
      rc = @socket.send_string msg
      raise "Could not send message." if error_check(rc)
    end
    rescue
      log FATAL, "Error while running server, shutting down. (#{$!.inspect})"
  end
end<commit_msg>Server will raise error on cleanup when needed.<commit_after>require 'rubygems'
require 'bundler/setup'

require "json"
require "date"
require "digest/sha1"
require 'logger'
require 'ffi-rzmq'

class Server < Logger::Application
  def initialize(options=nil)
    super("Dax::Server")
    self.level = Logger::INFO
    raise ArgumentError, "Need :db, :port, and :key" unless options.respond_to? 'has_key?'
    raise ArgumentError, "db instance (:db) not provided." if ! options.has_key? :db
    raise ArgumentError, "Listen uri (:listen) not provided." if ! options.has_key? :listen
    raise ArgumentError, "AES key (:key) not provided." if ! options.has_key? :key
    @db = options[:db]
    @listen = options[:listen]
    @key = options[:key]
    @zmq = ZMQ::Context.new(50)
    @socket = @zmq.socket(ZMQ::REP)
    @socket.bind(@listen)
    Thread::abort_on_exception = true

    @zthread = Thread.new do
      server_loop
    end
  end

  def cleanup
    raise @error if @error
    @shutdown = true
    @zthread.join
  end

  def context
    @zmq
  end

  def error_check(rc, errno)
    if ZMQ::Util.resultcode_ok?(rc) || errno ==  ZMQ::EAGAIN
      false
    else
      log ERROR, "Operation failed, errno [#{ZMQ::Util.errno}] description [#{ZMQ::Util.error_string}]"
      true
    end
  end
  
  private

  def server_loop
    @socket.setsockopt(ZMQ::LINGER, 1000)
    @socket.setsockopt(ZMQ::RCVTIMEO, 1000)
    until @shutdown
      msg = ''
      rc = 0
      errno = 0
      begin 
        rc = @socket.recv_string msg
        log DEBUG, "Received: #{msg}" unless rc < 0
        errno = ZMQ::Util.errno
        break if @shutdown
      end while rc < 0 && errno == ZMQ::EAGAIN
      break if @shutdown
      raise "Could not receive message." if error_check(rc, errno)
      rc = @socket.send_string msg
      errno = ZMQ::Util.errno
      raise "Could not send message." if error_check(rc, errno)
    end
    rescue
      @error = "Error while running server, shutting down. (#{$!.inspect})"
      log FATAL, @error
  end
end
{'fraction_non_alphanumeric': 0.1232054293918037, 'fraction_numerical': 0.007308796658835813, 'mean_word_length': 2.987513007284079, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3901111', 'n_tokens_mistral': 1340, 'n_tokens_neox': 1259, 'n_words': 363}