#!/usr/bin/env ruby # encoding: utf-8 puts "#{Time.now}: Loading #{__FILE__}" STDOUT.sync = true $: << File.expand_path(File.join(File.dirname(__FILE__), 'lib')) require 'logger' require 'needle' require 'odba/id_server' require 'rrba/server' require 'ydim/autoinvoicer' require 'ydim/client' require 'ydim/currency_converter' require 'ydim/currency_updater' require 'ydim/factory' require 'ydim/root_user' require 'ydim/server' require 'ydim/util' require 'odba/18_19_loading_compatibility' require 'odba/connection_pool' require 'odba/drbwrapper' module YDIM class Server def migrate_to_utf8 @migrate_mutex = Mutex.new ODBA.cache.retire_age = 5 ODBA.cache.cleaner_step = 100000 system = @system.odba_instance table = { system.odba_id => true, :serialized => {} } table.store :finalizer, proc { |object_id| table[:serialized].delete object_id } queue = [ system ] last_size = 0 if @config puts "@config must be nil" exit 3 end while !queue.empty? if (queue.size - last_size).abs >= 10000 puts last_size = queue.size end puts "Migrating table #{table}" _migrate_to_utf8 queue, table, :all => true end end def _migrate_to_utf8 queue, table, opts={} @migrate_mutex.synchronize do obj = queue.shift if obj.is_a?(Numeric) begin obj = ODBA.cache.fetch obj rescue ODBA::OdbaError return end else obj = obj.odba_instance end puts " #{__LINE__}: Migrating #{obj.class}" return unless obj _migrate_obj_to_utf8 obj, queue, table, opts obj.odba_store unless obj.odba_unsaved? end end def _migrate_obj_to_utf8 obj, queue, table, opts={} obj.instance_variables.each do |name| child = obj.instance_variable_get name if child.respond_to?(:odba_unsaved?) && !child.odba_unsaved? \ && obj.respond_to?(:odba_serializables) \ && obj.odba_serializables.include?(name) child.instance_variable_set '@odba_persistent', nil end child = _migrate_child_to_utf8 child, queue, table, opts obj.instance_variable_set name, child end if obj.is_a?(Array) obj.collect! do |child| _migrate_child_to_utf8 child, queue, table, opts end end if obj.is_a?(Hash) obj.dup.each do |key, child| obj.store key, _migrate_child_to_utf8(child, queue, table, opts) end end obj end def _migrate_child_to_utf8 child, queue, table, opts={} @serialized ||= {} case child when ODBA::Persistable, ODBA::Stub if child = child.odba_instance if child.odba_unsaved? _migrate_to_utf8 [child], table, opts elsif opts[:all] odba_id = child.odba_id unless table[odba_id] table.store odba_id, true queue.push odba_id end end end when String # child = iconv.iconv(child) # iconv = ::Iconv.new 'UTF-8//TRANSLIT//IGNORE', 'ISO-8859-1' puts "child has incoding #{child.encoding}" # child = child.force_encoding when YDIM::AutoInvoice, YDIM::Debitor, YDIM::Debitor, YDIM::Invoice, YDIM::Invoice::Info, YDIM::Item child = _migrate_obj_to_utf8 child, queue, table, opts when Float, Fixnum, TrueClass, FalseClass, NilClass, Symbol, Time, Date, DateTime, YDIM::Factory, YDIM::CurrencyConverter, YDIM::MobileCurrencyConverter # do nothing else @ignored ||= {} unless @ignored[child.class] @ignored.store child.class, true warn "ignoring #{child.class}" end end child rescue SystemStackError puts child.class raise end end end puts "#{Time.now}: Calling migrate_to_utf8" @logger = Logger.new('migrate_to_utf8.log') @logger.level = Logger::DEBUG config = YDIM::Server.config ODBA.storage.dbi = ODBA::ConnectionPool.new(config.db_driver_url, config.db_user, config.db_auth, :client_encoding => 'LATIN1') ODBA.cache.setup DRb.install_id_conv ODBA::DRbIdConv.new if false server = YDIM::Server.new(config, logger) server.extend(DRbUndumped) puts config.inspect if(config.detach) pidfile = '/var/run/ydimd.pid' File.open(pidfile, 'w') { |fh| fh.puts $$ } at_exit { File.unlink(pidfile) } Process.fork and exit!(0) end begin url = config.server_url url.untaint DRb.start_service(url, server) $SAFE = 1 logger.info('start') { sprintf("starting ydim-server on %s", config.server_url) } DRb.thread.join rescue Exception => error logger.error('fatal') { error } raise end end @server = YDIM::Server.new(config, @logger) @server.extend(DRbUndumped) @server.migrate_to_utf8 puts "#{Time.now}: Finished migrate_to_utf8"