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

Upgrade: Only check reactors/channels specified by config #107

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions lib/Synergy/Hub.pm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ use URI;
use Scalar::Util qw(blessed);
use Defined::KV;

has config_file => (
is => 'ro',
isa => 'Maybe[Str]',
);

has name => (
is => 'ro',
isa => 'Str',
Expand Down Expand Up @@ -295,11 +300,10 @@ sub set_loop ($self, $loop) {
return $loop;
}

sub synergize {
my $class = shift;
my ($loop, $config) = @_ == 2 ? @_
: @_ == 1 ? (undef, @_)
: confess("weird arguments passed to synergize");
sub synergize ($class, $arg) {
my $loop = delete $arg->{loop};
my $config = delete $arg->{config};
my $config_file = delete $arg->{config_file};

$loop //= do {
require IO::Async::Loop;
Expand All @@ -321,6 +325,7 @@ sub synergize {
defined_kv(tls_cert_file => $config->{tls_cert_file}),
defined_kv(tls_key_file => $config->{tls_key_file}),
defined_kv(state_dbfile => $config->{state_dbfile}),
config_file => $config_file,
});

$directory->register_with_hub($hub);
Expand Down Expand Up @@ -384,10 +389,11 @@ sub synergize_file {
: $filename =~ /\.toml\z/ ? \&_slurp_toml_file
: confess "don't know how to synergize_file $filename";

return $class->synergize(
($loop ? $loop : ()),
$reader->($filename),
);
return $class->synergize({
loop => $loop,
config => $reader->($filename),
config_file => $filename,
});
}

has http_client => (
Expand Down
51 changes: 46 additions & 5 deletions lib/Synergy/Reactor/Upgrade.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use namespace::clean;
use File::pushd;
use File::Find;
use Path::Tiny;
use YAML::XS;
use Try::Tiny;

has git_dir => (
is => 'ro',
Expand Down Expand Up @@ -180,10 +182,48 @@ sub get_version_desc ($self) {
$output;
}

sub check_next {
sub check_next ($self, @) {
my $cf = $self->hub->config_file;

# No config file? Huh. Do normal check
return $self->check_next_file_find unless $cf;

my $reader = $cf =~ /\.ya?ml\z/ ? sub { YAML::XS::LoadFile($_[0]) }
: $cf =~ /\.json\z/ ? \&Synergy::Hub::_slurp_json_file
: $cf =~ /\.toml\z/ ? \&Synergy::Hub::_slurp_toml_file
: undef;

return $self->check_next_file_find unless $reader;

my ($config, $err);

try {
$config = $reader->($cf);
} catch {
$err = "Failed to parse config ($cf): $_";
};

return $err if $err;

my %allowed;

for my $thing (qw( channels reactors )) {
for my $thing_config (values %{ $config->{$thing} }) {
my $thing_class = delete $thing_config->{class};

next unless $thing_class;

$allowed{$thing_class} = 1;
}
}

return $self->check_next_file_find(\%allowed);
}

sub check_next_file_find ($self, $allowed = {}) {
my $data = "use lib qw(lib);\n";

find(sub { wanted(\$data) }, 'lib/');
find(sub { wanted($allowed, \$data) }, 'lib/');

my $f = Path::Tiny->tempfile;
$f->spew($data);
Expand All @@ -194,9 +234,7 @@ sub check_next {
return;
}

sub wanted {
my $data = shift;

sub wanted ($allowed, $data) {
return unless -f $_;
return unless /\.pm$/;

Expand All @@ -206,6 +244,9 @@ sub wanted {
$name =~ s/\//::/g;
$name =~ s/\.pm//;

# Only load channels/reactors referenced in config
return if %$allowed && $name =~ /Synergy::(Reactor|Channel)/ && ! $allowed->{$name};

$$data .= "use $name;\n";
}

Expand Down
6 changes: 3 additions & 3 deletions t/basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use Net::Async::HTTP;
use Synergy::Hub;

# Initialize Synergy.
my $synergy = Synergy::Hub->synergize(
{
my $synergy = Synergy::Hub->synergize({
config => {
user_directory => "t/data/users.yaml",
channels => {
'test-channel' => {
Expand All @@ -35,7 +35,7 @@ my $synergy = Synergy::Hub->synergize(
pref => { class => 'Synergy::Reactor::Preferences' },
}
}
);
});

# Tests begin here.
testing_loop($synergy->loop);
Expand Down
6 changes: 3 additions & 3 deletions t/dates.t
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use Net::Async::HTTP;
use Synergy::Hub;

# Initialize Synergy.
my $synergy = Synergy::Hub->synergize(
{
my $synergy = Synergy::Hub->synergize({
config => {
user_directory => "t/data/users.yaml",
time_zone_names => {
"America/New_York" => "🇺🇸",
Expand All @@ -38,7 +38,7 @@ my $synergy = Synergy::Hub->synergize(
echo => { class => 'Synergy::Reactor::Echo' },
}
}
);
});

sub from_epoch { DateTime->from_epoch(epoch => $_[0]) }

Expand Down
6 changes: 3 additions & 3 deletions t/exclusive.t
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use Net::Async::HTTP;
use Synergy::Hub;

# Initialize Synergy.
my $synergy = Synergy::Hub->synergize(
{
my $synergy = Synergy::Hub->synergize({
config => {
user_directory => "t/data/users.yaml",
channels => {
'test-channel' => {
Expand All @@ -33,7 +33,7 @@ my $synergy = Synergy::Hub->synergize(
echo => { class => 'Synergy::Reactor::Echo' },
}
}
);
});

# Tests begin here.
testing_loop($synergy->loop);
Expand Down
6 changes: 3 additions & 3 deletions t/httpendpoint.t
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ package Synergy::Channel::Test::HTTPEndpointAuth {
}

# Initialize Synergy.
my $synergy = Synergy::Hub->synergize(
{
my $synergy = Synergy::Hub->synergize({
config => {
user_directory => "t/data/users.yaml",
channels => {
'test-channel-endpoint' => {
Expand All @@ -55,7 +55,7 @@ my $synergy = Synergy::Hub->synergize(
},
},
}
);
});

# Tests begin here.
testing_loop($synergy->loop);
Expand Down
6 changes: 3 additions & 3 deletions t/https.t
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use Plack::Response;
use Synergy::Hub;

# Initialize Synergy.
my $synergy = Synergy::Hub->synergize(
{
my $synergy = Synergy::Hub->synergize({
config => {
user_directory => "t/data/users.yaml",
channels => {
'test-channel' => {
Expand All @@ -28,7 +28,7 @@ my $synergy = Synergy::Hub->synergize(
tls_cert_file => "t/data/synergy.crt",
tls_key_file => "t/data/synergy.key",
}
);
});

$synergy->server->register_path('/ok', sub {
return Plack::Response->new(200)->finalize;
Expand Down
6 changes: 3 additions & 3 deletions t/httpserver.t
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ use Plack::Response;
use Synergy::Hub;

# Initialize Synergy.
my $synergy = Synergy::Hub->synergize(
{
my $synergy = Synergy::Hub->synergize({
config => {
user_directory => "t/data/users.yaml",
channels => {
'test-channel' => {
class => 'Synergy::Channel::Test',
}
},
}
);
});

$synergy->server->register_path('/ok', sub {
return Plack::Response->new(200)->finalize;
Expand Down
6 changes: 3 additions & 3 deletions t/lp-task-spec.t
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use Synergy::Logger::Test '$Logger';
use Synergy::Hub;

my $tmpfile = Path::Tiny->tempfile;
my $synergy = Synergy::Hub->synergize(
{
my $synergy = Synergy::Hub->synergize({
config => {
user_directory => "t/data/users-lp.yaml",
channels => {
'test-channel' => {
Expand All @@ -38,7 +38,7 @@ my $synergy = Synergy::Hub->synergize(
},
state_dbfile => "$tmpfile",
}
);
});

for my $to_set (
[ jetta => { lp => { 'default-project-shortcut' => 'pies' } } ],
Expand Down
6 changes: 3 additions & 3 deletions t/page.t
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ my $PAGE = "Hello\n\nfriend.";
my $tmpfile = Path::Tiny->tempfile;

# Initialize Synergy.
my $synergy = Synergy::Hub->synergize(
{
my $synergy = Synergy::Hub->synergize({
config => {
user_directory => "t/data/users-page.yaml",
channels => {
'test-1' => {
Expand Down Expand Up @@ -49,7 +49,7 @@ my $synergy = Synergy::Hub->synergize(
},
state_dbfile => "$tmpfile",
}
);
});

# Tests begin here.
testing_loop($synergy->loop);
Expand Down
6 changes: 3 additions & 3 deletions t/prometheus.t
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use Net::Async::HTTP;
use Synergy::Hub;

# Initialize Synergy.
my $synergy = Synergy::Hub->synergize(
{
my $synergy = Synergy::Hub->synergize({
config => {
user_directory => "t/data/users.yaml",
channels => {
'test-channel' => {
Expand All @@ -33,7 +33,7 @@ my $synergy = Synergy::Hub->synergize(
prometheus => { class => 'Synergy::Reactor::Prometheus' },
},
}
);
});

# Tests begin here.
testing_loop($synergy->loop);
Expand Down