Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert the notification to use notiffany gem #106

Merged
merged 10 commits into from
Oct 10, 2015
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ namespace :server do
end

desc 'Run the daemon in debugging mode'
task :daemon do
exec('bin/gitdocs start -D --port 9999')
task :debug do
exec('bin/gitdocs start --foreground --verbose --port 9999')
end

task default: 'test:integration'
32 changes: 31 additions & 1 deletion lib/gitdocs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,46 @@ def self.stop
@manager.stop
end

# @param [String] message
# @return [void]
def self.log_debug(message)
logger.debug(message)
end

# @param [String] message
# @return [void]
def self.log_info(message)
logger.info(message)
end

# @param [String] message
# @return [void]
def self.log_warn
logger.warn(message)
end

# @param [String] message
# @return [void]
def self.log_error
logger.error(message)
end

##############################################################################

private_class_method

# @return [Logger]
def self.logger
return @logger if @logger

output =
if Initializer::debug
if Initializer.foreground
STDOUT
else
File.expand_path('log', Initializer.root_dirname)
end
@logger = Logger.new(output)
@logger.level = Initializer.verbose ? Logger::DEBUG : Logger::INFO
@logger
end
end
19 changes: 13 additions & 6 deletions lib/gitdocs/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,28 @@ def self.source_root
end

desc 'start', 'Starts a daemonized gitdocs process'
method_option :debug, type: :boolean, aliases: '-D'
method_option :port, type: :string, aliases: '-p'
method_option :pid, type: :string, aliases: '-P'
method_option :foreground, type: :boolean, aliases: '-fg'
method_option :verbose, type: :boolean, aliases: '-v'
method_option :port, type: :string, aliases: '-p'
method_option :pid, type: :string, aliases: '-P'
def start
unless stopped?
say 'Gitdocs is already running, please use restart', :red
return
end

if options[:debug]
say 'Starting in debug mode', :yellow
Gitdocs::Initializer.debug = true
Gitdocs::Initializer.verbose = options[:verbose]

if options[:foreground]
say 'Run in the foreground', :yellow
Gitdocs::Initializer.foreground = true
Gitdocs.start(options[:port])
else
# Clear the arguments so that they will not be processed by the
# Dante execution.
ARGV.clear
runner.execute { Gitdocs.start(options[:port]) }

if running?
say 'Started gitdocs', :green
else
Expand Down
18 changes: 14 additions & 4 deletions lib/gitdocs/initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,23 @@ def self.database=(value)
end

# @return [Boolean]
def self.debug
@debug ||= false
def self.foreground
@foreground ||= false
end

# @param [Boolean] value
def self.debug=(value)
@debug = value
def self.foreground=(value)
@foreground = value
end

# @return [Boolean]
def self.verbose
@verbose ||= false
end

# @param [Boolean] value
def self.verbose=(value)
@verbose = !!value
end
end
end
29 changes: 10 additions & 19 deletions lib/gitdocs/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,33 @@ module Gitdocs
class Manager
# @param [nil, #to_i] override_web_port
def start(override_web_port)
log("Starting Gitdocs v#{VERSION}...")
log("Using configuration root: '#{Initializer.root_dirname}'")
Gitdocs.log_info("Starting Gitdocs v#{VERSION}...")
Gitdocs.log_info("Using configuration root: '#{Initializer.root_dirname}'")

shares = Share.all
log("Shares: (#{shares.length}) #{shares.map(&:inspect).join(', ')}")
Gitdocs.log_info("Monitoring shares(#{shares.length})")
shares.each { |share| Gitdocs.log_debug("* #{share.inspect}") }

begin
EM.run do
log('Starting EM loop...')

@runners = Runner.start_all(shares)
Server.start_and_wait(override_web_port)
end
rescue Restart
retry
end
rescue Exception => e # rubocop:disable RescueException
# Report all errors in log
log("#{e.class.inspect} - #{e.inspect} - #{e.message.inspect}", :error)
log(e.backtrace.join("\n"), :error)
Gitdocs.log_error(
"#{e.class.inspect} - #{e.inspect} - #{e.message.inspect}"
)
Gitdocs.log_error(e.backtrace.join("\n"))
Notifier.error(
'Unexpected exit',
'Something went wrong. Please see the log for details.'
)
raise
ensure
log("Gitdocs is terminating...goodbye\n\n")
Gitdocs.log_info("Gitdocs is terminating...goodbye\n\n")
end

def restart
Expand All @@ -45,15 +46,5 @@ def restart
def stop
EM.stop
end

############################################################################

private

# @param [String] msg
# @return [void]
def log(msg)
Gitdocs.logger.info(msg)
end
end
end
3 changes: 3 additions & 0 deletions lib/gitdocs/notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def initialize(show_notifications)
# @param [String] title
# @param [String] message
def info(title, message)
Gitdocs.log_info("#{title}: #{message}")
if @show_notifications
Guard::Notifier.notify(message, title: title, image: INFO_ICON)
else
Expand All @@ -31,6 +32,7 @@ def info(title, message)
# @param [String] title
# @param [String] message
def warn(title, message)
Gitdocs.log_warn("#{title}: #{message}")
if @show_notifications
Guard::Notifier.notify(message, title: title)
else
Expand All @@ -43,6 +45,7 @@ def warn(title, message)
# @param [String] title
# @param [String] message
def error(title, message)
Gitdocs.log_error("#{title}: #{message}")
if @show_notifications
Guard::Notifier.notify(message, title: title, image: :failure)
else
Expand Down
7 changes: 6 additions & 1 deletion lib/gitdocs/repository/committer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ def commit

# FIXME: Consider a more appropriate location for the dirty check.
return false unless Gitdocs::Repository.new(@root_dirname).dirty?
Gitdocs.log_debug("Repo #{@root_dirname} is dirty")

# Commit any changes in the working directory.
Dir.chdir(@root_dirname) do
@rugged.index.add_all
@rugged.index.update_all
end
@rugged.index.write
@grit.commit_index(message)
Gitdocs.log_debug("Index to be committed #{@rugged.index}")

commit_result = @grit.commit_index(message)
Gitdocs.log_debug("Commit result: <#{commit_result.inspect}>")

true
end

Expand Down
8 changes: 4 additions & 4 deletions lib/gitdocs/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def self.start_and_wait(override_port)
end

def start
Thin::Logging.debug = Initializer.debug
Thin::Logging.debug = Initializer.verbose
Thin::Server.start('127.0.0.1', @port) do
use Rack::Static,
urls: %w(/css /js /img /doc),
Expand All @@ -41,15 +41,15 @@ def wait_for_start
i = 0
begin
TCPSocket.open('127.0.0.1', @port).close
Gitdocs.logger.info('Web server running!')
Gitdocs.log_info('Web server running!')
rescue Errno::ECONNREFUSED
sleep 0.2
i += 1
if i <= 20
Gitdocs.logger.info('Retrying web server loop...')
Gitdocs.log_info('Retrying web server loop...')
retry
else
Gitdocs.logger.info('Web server failed to start')
Gitdocs.log_error('Web server failed to start')
end
end
end
Expand Down
25 changes: 8 additions & 17 deletions test/integration/browse_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,17 @@

describe 'browse and edit repository file through the UI' do
before do
git_init_local('repo1')
gitdocs_add('repo1')

git_init_local
gitdocs_add
gitdocs_add('local')

# Create the various commits, to be able to see revisions.
repository = Gitdocs::Repository.new(abs_current_dir('local'))
write_file('local/file1', 'fbadbeef')
repository.commit
write_file('local/file1', 'foobar')
repository.commit
write_file('local/file1', 'deadbeef')
repository.commit
write_file('local/file2', 'A5A5A5A5')
repository.commit
write_file('local/README.md', 'hello i am a README')
repository.commit

start_daemon
GitFactory.commit(:local, 'file1', 'fbadbeef')
GitFactory.commit(:local, 'file1', 'foobar')
GitFactory.commit(:local, 'file1', 'deadbeef')
GitFactory.commit(:local, 'file2', 'A5A5A5A5')
GitFactory.commit(:local, 'README.md', 'hello i am a README')

gitdocs_start

visit 'http://localhost:7777/'
click_link('Home')
Expand Down
18 changes: 9 additions & 9 deletions test/integration/full_sync_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

describe 'fully synchronizing repositories' do
before do
git_clone_and_gitdocs_add(git_init_remote, 'clone1', 'clone2', 'clone3')
start_daemon
gitdocs_create_from_remote('clone1', 'clone2', 'clone3')
gitdocs_start
end

it 'should sync new files' do
write_file('clone1/newfile', 'testing')
GitFactory.write(:clone1, 'newfile', 'testing')
wait_for_clean_workdir('clone1')

wait_for_exact_file_content('clone1/newfile', 'testing')
Expand All @@ -18,11 +18,11 @@
end

it 'should sync changes to an existing file' do
write_file('clone1/file', 'testing')
GitFactory.write(:clone1, 'file', 'testing')
wait_for_clean_workdir('clone1')

wait_for_exact_file_content('clone3/file', 'testing')
append_to_file('clone3/file', "\nfoobar")
GitFactory.append(:clone3, 'file', "\nfoobar")
wait_for_clean_workdir('clone3')

wait_for_exact_file_content('clone1/file', "testing\nfoobar")
Expand All @@ -31,7 +31,7 @@
end

it 'should sync empty directories' do
in_current_dir { _mkdir('clone1/empty_dir') }
GitFactory.mkdir(:clone1, 'empty_dir')
wait_for_clean_workdir('clone1')

wait_for_directory('clone1/empty_dir')
Expand All @@ -40,11 +40,11 @@
end

it 'should mark unresolvable conflicts' do
write_file('clone1/file', 'testing')
GitFactory.write(:clone1, 'file', 'testing')
wait_for_clean_workdir('clone1')

append_to_file('clone2/file', 'foobar')
append_to_file('clone3/file', 'deadbeef')
GitFactory.append(:clone2, 'file', 'foobar')
GitFactory.append(:clone3, 'file', 'deadbeef')
wait_for_clean_workdir('clone2')
wait_for_clean_workdir('clone3')

Expand Down