Module: TxCatcher::Initializer
- Defined in:
- lib/txcatcher/initializer.rb
Defined Under Namespace
Modules: ConfigFile
Constant Summary collapse
- GEM_ROOT =
File.('../..', File.dirname(__FILE__))
- MIGRATIONS_ROOT =
GEM_ROOT + '/db/migrations/'
Instance Method Summary collapse
- #add_route(path, &block) ⇒ Object
- #connect_to_db ⇒ Object
-
#connect_to_rpc_node ⇒ Object
Connects to all bitcoin daemons listed in config.
- #create_config_files ⇒ Object
- #migrations_pending? ⇒ Boolean
- #prepare ⇒ Object
- #read_config_file ⇒ Object
- #run_migrations ⇒ Object
- #set_goliath_args ⇒ Object
Instance Method Details
#add_route(path, &block) ⇒ Object
56 57 58 |
# File 'lib/txcatcher/initializer.rb', line 56 def add_route(path, &block) @routes[path] = block end |
#connect_to_db ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/txcatcher/initializer.rb', line 92 def connect_to_db # symbolize keys for convenience db_config = TxCatcher::Config.db.keys_to_sym db_name = if db_config[:adapter] == 'sqlite' if db_config[:db_path].start_with?("/") db_config[:db_path] else "#{File.dirname(ConfigFile.path)}/#{db_config[:db_path]}" end else db_config[:db_name] end TxCatcher.db_connection = Sequel.connect( "#{db_config[:adapter]}://" + "#{db_config[:user]}#{(":" if db_config[:user])}" + "#{db_config[:password]}#{("@" if db_config[:user] || db_config[:password])}" + "#{db_config[:host]}#{(":" if db_config[:port])}" + "#{db_config[:port]}#{("/" if db_config[:host] || db_config[:port])}" + "#{db_name}" ) end |
#connect_to_rpc_node ⇒ Object
Connects to all bitcoin daemons listed in config
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/txcatcher/initializer.rb', line 118 def connect_to_rpc_node n = TxCatcher::Config.rpcnode print "Checking #{n["name"]} RPC connection... " TxCatcher.rpc_node = BitcoinRPC.new("http://#{n["user"]}:#{n["password"]}@#{n["host"]}:#{n["port"]}") i = 0 # try to connect to RPC 100 times before exiting with error until TxCatcher.current_block_height begin TxCatcher.current_block_height = TxCatcher.rpc_node.getblockcount rescue Errno::ECONNREFUSED, BitcoinRPC::JSONRPCError => e if i > 100 print "ERROR, cannot connect using connection data #{n["name"]}\n" exit else print "Bitcoin RPC connection error: #{e.to_s}; will try again in 1 sec...\n" i += 1 sleep 1 end end end print "current block height: #{TxCatcher.current_block_height}\n" end |
#create_config_files ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/txcatcher/initializer.rb', line 60 def create_config_files FileUtils.mkdir_p(ConfigFile.path) unless File.exist?(ConfigFile.path) unless File.exist?(ConfigFile.path) puts "\e[1;33mWARNING!\e[0m \e[33mNo file #{ConfigFile.path} was found. Created a sample one for you.\e[0m" puts "You should edit it and try starting the server again.\n" FileUtils.cp(GEM_ROOT + '/templates/config.yml', ConfigFile.path) config_contents = File.read(ConfigFile.path) config_contents.sub!("$home", File.('~')) File.open(ConfigFile.path, "w") { |f| f.write(config_contents) } puts "Shutting down now.\n\n" exit end end |
#migrations_pending? ⇒ Boolean
148 149 150 |
# File 'lib/txcatcher/initializer.rb', line 148 def migrations_pending? !Sequel::Migrator.is_current?(TxCatcher.db_connection, MIGRATIONS_ROOT) end |
#prepare ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/txcatcher/initializer.rb', line 46 def prepare ConfigFile.set! create_config_files read_config_file connect_to_db connect_to_rpc_node run_migrations if migrations_pending? set_goliath_args end |
#read_config_file ⇒ Object
77 78 79 80 81 |
# File 'lib/txcatcher/initializer.rb', line 77 def read_config_file YAML.load_file(ConfigFile.path).each do |k,v| TxCatcher::Config.send(k + '=', v) end end |
#run_migrations ⇒ Object
141 142 143 144 145 146 |
# File 'lib/txcatcher/initializer.rb', line 141 def run_migrations Sequel.extension :migration print "\nPending migrations for the database detected. Migrating..." Sequel::Migrator.run(TxCatcher.db_connection, MIGRATIONS_ROOT) print "done\n\n" end |
#set_goliath_args ⇒ Object
83 84 85 86 87 88 89 90 |
# File 'lib/txcatcher/initializer.rb', line 83 def set_goliath_args # Setting default port to 9498 unless ARGV.include?("-p") ARGV.push "-p" Config.server_port ? ARGV.push(Config.server_port.to_s) : ARGV.push("9498") Config.daemonize ? ARGV.push("-d") : "" end end |