bin/lazylead
#!/usr/bin/env ruby
# frozen_string_literal: true
# The MIT License
#
# Copyright (c) 2019-2022 Yurii Dubinka
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom
# the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.
# When sync mode is true, all output is immediately flushed
# to the underlying operating system and is not buffered internally.
$stdout.sync = true
require "slop"
require "sqlite3"
require "colorize"
require "inifile"
require "logging"
require "backtrace"
require "memory_profiler"
require_relative "../lib/lazylead/log"
require_relative "../lib/lazylead/home"
require_relative "../lib/lazylead/schedule"
require_relative "../lib/lazylead/allocated"
require_relative "../lib/lazylead/cli/app"
log = Lazylead::Log.new
Thread.current.name = "main"
Logging.mdc["tid"] = Thread.current.name
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
# @todo #/DEV Decorate ARGV with custom methods in order to avoid code
# duplication, like { ARGV.include? "--trace" }
opts = Slop.parse(ARGV, strict: false, suppress_errors: true) do |o|
o.banner = "Usage: lazylead [options]
Available options:"
o.bool "-h", "--help", "Show these instructions"
o.bool "--trace", "Show full stack trace in case of a problem"
o.bool "--memory-dump", "Dump memory snapshot afterwards, to the console",
default: false
o.string "--home",
"Home directory (default #{Lazylead::Home.new.dir})",
default: Lazylead::Home.new.dir
o.string "--sqlite",
"SQLite database file name",
default: "lazylead.db"
o.string "--vcs4sql",
"Home directory with database version control(vcs) scripts",
default: "upgrades/sqlite"
o.integer "--max-connections",
"SQL connections pool size",
default: 5
o.bool "--testdata",
"Apply the database VCS migration with test data",
default: false
o.string "--log-file", "The path to the log file"
o.string "--rolling-age", "The maximum age (in seconds) of a log file before it is rolled. The age can also be given as 'daily', 'weekly', or 'monthly'"
o.string "--rolling-files", "The number of rolled log files to keep."
o.on "--verbose", "Enable extra logging information" do
log.verbose
end
o.on "-v", "--version", "Show current version" do
log.debug Lazylead::VERSION
exit
end
end
log.debug "Version: #{Lazylead::VERSION.colorize(:green)}"
log.debug "Memory footprint at start is " \
"#{Lazylead::Allocated.new.to_s.colorize(:light_blue)}."
cmd = lambda do
IniFile.new(filename: "lazylead.ini").each { |_, k, v| ENV[k] = v }
Lazylead::CLI::App.new(
log,
Lazylead::Schedule.new(log: log),
Lazylead::Smtp.new(
log, Lazylead::Salt.new("smtp_salt"),
smtp_host: ENV.fetch("smtp_host", nil),
smtp_port: ENV.fetch("smtp_port", nil),
smtp_user: ENV.fetch("smtp_user", nil),
smtp_pass: ENV.fetch("smtp_pass", nil)
)
).run(opts.to_h)
return 0
rescue StandardError => e
log.error("#{e.message} (#{e.class.name})")
log.error(Backtrace.new(e)) if ARGV.include? "--trace"
return -1
end
code = 0
if opts["memory-dump"]
MemoryProfiler.report(top: 20) { code = cmd.call }.pretty_print
else
code = cmd.call
end
log.debug "Memory footprint at the end is " \
"#{Lazylead::Allocated.new.to_s.colorize(:light_blue)}."
exit(code) unless code.zero?