Class: MoSQL::CLI
Constant Summary collapse
- BATCH =
1000
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#tailer ⇒ Object
readonly
Returns the value of attribute tailer.
Class Method Summary collapse
Instance Method Summary collapse
- #connect_mongo ⇒ Object
- #connect_sql ⇒ Object
-
#initialize(args) ⇒ CLI
constructor
A new instance of CLI.
- #load_collections ⇒ Object
- #parse_args ⇒ Object
- #run ⇒ Object
- #setup_signal_handlers ⇒ Object
Methods included from Logging
Constructor Details
#initialize(args) ⇒ CLI
Returns a new instance of CLI.
19 20 21 22 23 24 |
# File 'lib/mosql/cli.rb', line 19 def initialize(args) @args = args @options = [] @done = false setup_signal_handlers end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
12 13 14 |
# File 'lib/mosql/cli.rb', line 12 def args @args end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
12 13 14 |
# File 'lib/mosql/cli.rb', line 12 def @options end |
#tailer ⇒ Object (readonly)
Returns the value of attribute tailer.
12 13 14 |
# File 'lib/mosql/cli.rb', line 12 def tailer @tailer end |
Class Method Details
Instance Method Details
#connect_mongo ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/mosql/cli.rb', line 119 def connect_mongo @mongo = Mongo::MongoClient.from_uri([:mongo]) config = @mongo['admin'].command(:ismaster => 1) if !config['setName'] && ![:skip_tail] log.warn("`#{[:mongo]}' is not a replset.") log.warn("Will run the initial import, then stop.") log.warn("Pass `--skip-tail' to suppress this warning.") [:skip_tail] = true end [:service] ||= config['setName'] end |
#connect_sql ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/mosql/cli.rb', line 131 def connect_sql @sql = MoSQL::SQLAdapter.new(@schema, [:sql], [:schema]) if [:verbose] >= 2 @sql.db.sql_log_level = :debug @sql.db.loggers << Logger.new($stderr) end end |
#load_collections ⇒ Object
139 140 141 142 143 144 145 146 147 148 |
# File 'lib/mosql/cli.rb', line 139 def load_collections collections = YAML.load_file(@options[:collections]) begin @schema = MoSQL::Schema.new(collections) rescue MoSQL::SchemaError => e log.error("Error parsing collection map `#{@options[:collections]}':") log.error(e.to_s) exit(1) end end |
#parse_args ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/mosql/cli.rb', line 35 def parse_args @options = { :collections => 'collections.yml', :sql => 'postgres:///', :mongo => 'mongodb://localhost', :verbose => 0 } optparse = OptionParser.new do |opts| opts. = "Usage: #{$0} [options] " opts.on('-h', '--help', "Display this message") do puts opts exit(0) end opts.on('-v', "Increase verbosity") do @options[:verbose] += 1 end opts.on("-c", "--collections [collections.yml]", "Collection map YAML file") do |file| @options[:collections] = file end opts.on("--sql [sqluri]", "SQL server to connect to") do |uri| @options[:sql] = uri end opts.on("--mongo [mongouri]", "Mongo connection string") do |uri| @options[:mongo] = uri end opts.on("--schema [schema]", "PostgreSQL 'schema' to namespace tables") do |schema| @options[:schema] = schema end opts.on("--ignore-delete", "Ignore delete operations when tailing") do @options[:ignore_delete] = true end opts.on("--only-db [dbname]", "Don't scan for mongo dbs, just use the one specified") do |dbname| @options[:dbname] = dbname end opts.on("--tail-from [timestamp]", "Start tailing from the specified UNIX timestamp") do |ts| @options[:tail_from] = ts end opts.on("--service [service]", "Service name to use when storing tailing state") do |service| @options[:service] = service end opts.on("--skip-tail", "Don't tail the oplog, just do the initial import") do @options[:skip_tail] = true end opts.on("--reimport", "Force a data re-import") do @options[:reimport] = true end opts.on("--no-drop-tables", "Don't drop the table if it exists during the initial import") do @options[:no_drop_tables] = true end opts.on("--unsafe", "Ignore rows that cause errors on insert") do @options[:unsafe] = true end # eg, --oplog-filter '{"ns": {"$regex": "^somedb[0-9]*\\.collection$"}}' opts.on("--oplog-filter [filter]", "An additional JSON filter for the oplog query") do |filter| @options[:oplog_filter] = JSON.parse(filter) end end optparse.parse!(@args) log = Log4r::Logger.new('Stripe') log.outputters = Log4r::StdoutOutputter.new(STDERR) if [:verbose] >= 1 log.level = Log4r::DEBUG else log.level = Log4r::INFO end end |
#run ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/mosql/cli.rb', line 150 def run parse_args load_collections connect_sql connect_mongo = MoSQL::Tailer.create_table(@sql.db, 'mosql_tailers') @tailer = MoSQL::Tailer.new([@mongo], :existing, , :service => [:service]) @streamer = Streamer.new(:options => @options, :tailer => @tailer, :mongo => @mongo, :sql => @sql, :schema => @schema) @streamer.import unless [:skip_tail] @streamer.optail end end |
#setup_signal_handlers ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/mosql/cli.rb', line 26 def setup_signal_handlers %w[TERM INT USR2].each do |sig| Signal.trap(sig) do log.info("Got SIG#{sig}. Preparing to exit...") @streamer.stop end end end |