Skip to content

Commit

Permalink
Merge pull request #1480 from dasantiago/25958
Browse files Browse the repository at this point in the history
Log Uniformization
  • Loading branch information
coolo authored Nov 10, 2017
2 parents 9575846 + 4f54690 commit 31ec90c
Show file tree
Hide file tree
Showing 21 changed files with 489 additions and 263 deletions.
72 changes: 0 additions & 72 deletions lib/OpenQA/FakeApp.pm

This file was deleted.

9 changes: 4 additions & 5 deletions lib/OpenQA/ResourceAllocator.pm
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ use Scalar::Util 'blessed';

use OpenQA::IPC;
use OpenQA::Utils qw(log_debug wakeup_scheduler exists_worker safe_call);
use OpenQA::ServerStartup;
use OpenQA::Resource::Jobs ();
use OpenQA::Resource::Locks ();
use OpenQA::FakeApp;
use OpenQA::Setup;
use sigtrap handler => \&normal_signals_handler, 'normal-signals';

my $singleton;
Expand Down Expand Up @@ -59,9 +58,9 @@ sub new {

sub run {
my $self = shift;
my $fakeapp = OpenQA::FakeApp->new(log_name => 'resource-allocator');
OpenQA::ServerStartup::read_config($fakeapp);
OpenQA::ServerStartup::setup_logging($fakeapp);
my $setup = OpenQA::Setup->new(log_name => 'resource-allocator');
OpenQA::Setup::read_config($setup);
OpenQA::Setup::setup_log($setup);
log_debug("Resource allocator started");
$self->{reactor}->run()
if exists $self->{reactor} && blessed($self->{reactor}) && $self->{reactor}->isa("Net::DBus::Reactor");
Expand Down
11 changes: 5 additions & 6 deletions lib/OpenQA/Scheduler.pm
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ use Net::DBus::Reactor;
use Data::Dump 'pp';

use OpenQA::IPC;
use OpenQA::FakeApp;
use OpenQA::Setup;

use OpenQA::Utils 'log_debug';
use OpenQA::ServerStartup;

# How many jobs to allocate in one tick. Defaults to 50 ( set it to 0 for as much as possible)
use constant MAX_JOB_ALLOCATION => $ENV{OPENQA_SCHEDULER_MAX_JOB_ALLOCATION} // 50;
Expand Down Expand Up @@ -75,11 +74,11 @@ sub _is_method_allowed {
return $ret;
}

our $fakeapp;
our $setup;
sub run {
$fakeapp = OpenQA::FakeApp->new;
OpenQA::ServerStartup::read_config($fakeapp);
OpenQA::ServerStartup::setup_logging($fakeapp);
$setup = OpenQA::Setup->new(log_name => 'scheduler');
OpenQA::Setup::read_config($setup);
OpenQA::Setup::setup_log($setup);

OpenQA::Scheduler->new();
log_debug("Scheduler started");
Expand Down
146 changes: 104 additions & 42 deletions lib/OpenQA/ServerStartup.pm → lib/OpenQA/Setup.pm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2015-2016 SUSE LLC
# Copyright (C) 2017 SUSE LLC
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand All @@ -13,20 +13,117 @@
# You should have received a copy of the GNU General Public License along
# with this program; if not, see <http://www.gnu.org/licenses/>.

# This package contains shared functions between WebAPI and WebSockets
package OpenQA::ServerStartup;

package OpenQA::Setup;
use Mojo::Log;
use Mojo::Home;
use strict;
use warnings;
use Mojo::Base -base;
use Sys::Hostname;
use File::Spec::Functions 'catfile';
use Mojo::File 'path';
use Config::IniFiles;
use db_profiler;
use db_helpers;
use OpenQA::Utils;
use Mojo::File 'path';
use File::Path 'make_path';

sub read_config {
my $app = shift;
has config => sub { {} };

has log => sub { Mojo::Log->new(handle => \*STDOUT, level => "info"); };

has home => sub { Mojo::Home->new($ENV{MOJO_HOME} || '/') };

has mode => 'production';

has 'log_name';

has level => 'info';

has 'instance';

has 'log_dir';

has schema => sub { OpenQA::Schema::connect_db() };

sub setup_log {
my ($self) = @_;
my ($logfile, $logdir, $level, $log);

if ($self->isa('OpenQA::Setup')) {
$logdir = $self->log_dir;
$level = $self->level;
if ($logdir && !-e $logdir) {
make_path($logdir);
}
elsif ($logdir && !-d $logdir) {
die "Please point the logs to a valid folder!";
}
}
else {
$log = $self->log;
$level = $self->config->{logging}->{level} || 'debug';
}
$logfile = $ENV{OPENQA_LOGFILE} || $self->config->{logging}->{file};

if ($logfile && $logdir) {
$logfile = catfile($logdir, $logfile);
$log = Mojo::Log->new(
handle => path($logfile)->open('>>'),
level => $self->level,
format => sub { return log_format($self->log_name, @_); });
}
elsif ($logfile) {
$log = Mojo::Log->new(
handle => path($logfile)->open('>>'),
level => $level,
format => sub { return log_format($self->log_name, @_); });
}
elsif ($logdir) {
# So each worker from each host get it's own log (as the folder can be shared). Hopefully the machine hostname
# is already sanitized. Otherwise we need to check
$logfile
= catfile($logdir, hostname() . (defined $self->instance ? "-${\$self->instance}" : '') . ".log");
$log = Mojo::Log->new(
handle => path($logfile)->open('>>'),
level => $self->level,
format => sub { return log_format($self->log_name, @_); });
}
else {
$log = Mojo::Log->new(
handle => \*STDOUT,
level => $level,
format => sub {
my ($time, $level, @lines) = @_;
return "[${\$self->log_name}:$level] " . join "\n", @lines, '';
});
}

$self->log($log);
unless ($self->isa('OpenQA::Setup')) {
if ($ENV{OPENQA_SQL_DEBUG} // $self->config->{logging}->{sql_debug} // 'false' eq 'true') {
# avoid enabling the SQL debug unless we really want to see it
# it's rather expensive
db_profiler::enable_sql_debugging($self);
}
}

$OpenQA::Utils::app = $self;
return $log;
}

sub log_format {
my ($logname, $time, $level, @lines) = @_;
return '[' . localtime($time) . "] [$logname:$level] " . join "\n", @lines, '';
}

sub emit_event {
my ($self, $event, $data) = @_;
# nothing to see here, move along
}

sub read_config {
my $app = shift;
my %defaults = (
global => {
appname => 'openQA',
Expand Down Expand Up @@ -123,41 +220,6 @@ sub read_config {
$app->config->{auth}->{method} =~ s/\s//g;
}

sub setup_logging {
my ($app) = @_;

my $config = $app->config;
my $logfile = $ENV{OPENQA_LOGFILE} || $config->{logging}->{file};

if ($logfile) {
$app->log->handle(path($logfile)->open('>>'));
$app->log->path($logfile);
$app->log->format(
sub {
my ($time, $level, @lines) = @_;
return '[' . localtime($time) . "] [" . $app->log_name . ":$level] " . join "\n", @lines, '';
});
}
else {
$app->log->format(
sub {
my (undef, $level, @lines) = @_;
return '[' . $app->log_name . ":$level] " . join "\n", @lines, '';
});
}

if ($config->{logging}->{level}) {
$app->log->level($config->{logging}->{level});
}
if ($ENV{OPENQA_SQL_DEBUG} // $config->{logging}->{sql_debug} // 'false' eq 'true') {
# avoid enabling the SQL debug unless we really want to see it
# it's rather expensive
db_profiler::enable_sql_debugging($app);
}

$OpenQA::Utils::app = $app;
}

# Update config definition from plugin requests
sub update_config {
my ($config, @namespaces) = @_;
Expand Down
Loading

0 comments on commit 31ec90c

Please sign in to comment.