Skip to content

Commit

Permalink
Refactor base class
Browse files Browse the repository at this point in the history
  • Loading branch information
maros committed Feb 18, 2012
1 parent 1e783e4 commit 2501c38
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 165 deletions.
4 changes: 2 additions & 2 deletions bin/lacuna_task
Expand Up @@ -4,9 +4,9 @@ use strict;
use warnings; use warnings;
use 5.010; use 5.010;


use Games::Lacuna::Task; use Games::Lacuna::Task::TaskRunner;


my $task = Games::Lacuna::Task->new_with_options(); my $task = Games::Lacuna::TaskRunner->new_with_options();
$task->run; $task->run;


=encoding utf8 =encoding utf8
Expand Down
138 changes: 52 additions & 86 deletions lib/Games/Lacuna/Task.pm
Expand Up @@ -7,104 +7,74 @@ use 5.010;
our $AUTHORITY = 'cpan:MAROS'; our $AUTHORITY = 'cpan:MAROS';
our $VERSION = "2.01"; our $VERSION = "2.01";


use 5.010;

use Moose; use Moose;
extends qw(Games::Lacuna::Task::Base);
with qw(MooseX::Getopt);


use Games::Lacuna::Task::Utils qw(class_to_name name_to_class); use Games::Lacuna::Task::Types;
use Try::Tiny; use Games::Lacuna::Task::Meta::Class::Trait::NoAutomatic;
use Games::Lacuna::Task::Constants;


has 'exclude' => ( with qw(Games::Lacuna::Task::Role::Client
is => 'rw', Games::Lacuna::Task::Role::Logger);
isa => 'ArrayRef[Str]',
documentation => 'Select which tasks NOT to run [Multiple]',
predicate => 'has_exclude',
);


has 'task' => ( use Module::Pluggable
is => 'rw', search_path => ['Games::Lacuna::Task::Action'],
isa => 'ArrayRef[Str]', sub_name => '_all_actions';
documentation => 'Select which tasks to run [Multiple, Default all]',
predicate => 'has_task',
);


has '+configdir' => (
required => 1, has 'lockfile' => (
is => 'rw',
isa => 'Path::Class::File',
traits => ['NoGetopt'],
lazy_build => 1,
); );


sub run { sub _build_lockfile {
my ($self) = @_; my ($self) = @_;


my $client = $self->client(); return $self->configdir->file('lacuna.pid');

}
# Call lazy builder
$client->client; sub BUILD {

my ($self) = @_;
my $empire_name = $self->empire_name;

$self->log('notice',("=" x ($Games::Lacuna::Task::Constants::SCREEN_WIDTH - 8)));
$self->log('notice',"Running tasks for empire %s",$empire_name);

my $global_config = $client->config->{global};

$self->task($global_config->{task})
if (defined $global_config->{task}
&& ! $self->has_task);
$self->exclude($global_config->{exclude})
if (defined $global_config->{exclude}
&& ! $self->has_exclude);


my @tasks; my $lockcounter = 0;
if (! $self->has_task my $lockfile = $self->lockfile;
|| 'all' ~~ $self->task) {
@tasks = $self->all_actions;

} else {
foreach my $task (@{$self->task}) {
my $class = name_to_class($task);
push(@tasks,$class)
unless $class ~~ \@tasks;
}
}


# Loop all tasks # Check for lockfile
TASK: while (-e $lockfile) {
foreach my $task_class (@tasks) { my ($pid) = $lockfile->slurp(chomp => 1);
my $task_name = class_to_name($task_class);

next
if $self->has_exclude && $task_name ~~ $self->exclude;

my $ok = 1;
try {
Class::MOP::load_class($task_class);
} catch {
$self->log('error',"Could not load task %s: %s",$task_class,$_);
$ok = 0;
};


next if ($lockcounter > 10) {
if $task_class->meta->can('no_automatic') $self->abort('Could not aquire lock (%s)',$lockfile);
&& $task_class->meta->no_automatic; } else {

$self->log('warn','Another process is currently running. Waiting until it has finished');
if ($ok) {
$self->log('notice',("-" x ($Games::Lacuna::Task::Constants::SCREEN_WIDTH - 8)));
$self->log('notice',"Running action %s",$task_name);
try {
my $task_config = $client->task_config($task_name);
my $task = $task_class->new(
%{$task_config}
);
$task->execute;
} catch {
$self->log('error',"An error occured while processing %s: %s",$task_class,$_);
}
} }
$lockcounter++;
sleep 15;
} }
$self->log('notice',("=" x ($Games::Lacuna::Task::Constants::SCREEN_WIDTH - 8)));
# Write lock file
my $lockfh = $lockfile->openw();
print $lockfh $$;
$lockfh->close;
}

sub DEMOLISH {
my ($self) = @_;

$self->lockfile->remove
if -e $self->lockfile;
} }


sub all_actions {
_all_actions()
}


__PACKAGE__->meta->make_immutable;
no Moose;
1;


=encoding utf8 =encoding utf8
Expand Down Expand Up @@ -211,8 +181,4 @@ Games-Lacuna-Task is Copyright (c) 2012 Maroš Kollár
This library is free software, you can redistribute it and/or modify This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself. it under the same terms as Perl itself.
=cut =cut

__PACKAGE__->meta->make_immutable;
no Moose;
1;
4 changes: 1 addition & 3 deletions lib/Games/Lacuna/Task/ActionProto.pm
@@ -1,11 +1,9 @@
# ============================================================================
package Games::Lacuna::Task::ActionProto; package Games::Lacuna::Task::ActionProto;
# ============================================================================


use 5.010; use 5.010;


use Moose; use Moose;
extends qw(Games::Lacuna::Task::Base); extends qw(Games::Lacuna::Task);


use List::Util qw(max); use List::Util qw(max);
use Try::Tiny; use Try::Tiny;
Expand Down
74 changes: 0 additions & 74 deletions lib/Games/Lacuna/Task/Base.pm

This file was deleted.

99 changes: 99 additions & 0 deletions lib/Games/Lacuna/Task/Storage.pm
@@ -0,0 +1,99 @@
package Games::Lacuna::Task::Upgrade;

use 5.010;

use Moose;
with qw(Games::Lacuna::Task::Role::Logger);

our $VERSION = "2.02";

has 'storage' => (
is => 'ro',
isa => 'DBI::db',
required => 1,
);

has 'current_version' => (
is => 'rw',
isa => 'Num',
lazy_build => 1,
required => 1,
);

has 'latest_version' => (
is => 'ro',
isa => 'Num',
default => $VERSION,
required => 1,
);

sub _build_current_version {
my ($self) = @_;

my ($current_version) = $self->storage->selectrow_array('SELECT value FROM meta WHERE key = ?',{},'database_version');
$current_version ||= 2.00;
return $current_version;
}

sub run {
my ($self) = @_;

return
if $self->current_version == $self->latest_version;

my $storage = $self->storage;

$self->log('info',"Upgrading storage from version %.2f to %.2f",$self->current_version(),$self->latest_version);

my @sql;

if ($self->current_version() < 2.01) {
$self->log('debug','Upgrade for 2.00->2.01');

push(@sql,'ALTER TABLE star RENAME TO star_old');

push(@sql,'CREATE TABLE IF NOT EXISTS star (
id INTEGER NOT NULL PRIMARY KEY,
x INTEGER NOT NULL,
y INTEGER NOT NULL,
name TEXT NOT NULL,
zone TEXT NOT NULL,
last_checked INTEGER,
is_probed INTEGER,
is_known INTEGER
)');

push(@sql,'INSERT INTO star (id,x,y,name,zone,last_checked,is_probed,is_known) SELECT id,x,y,name,zone,last_checked,probed,probed FROM star_old');

push(@sql,'DROP TABLE star_old');

push(@sql,'DELETE FROM cache');
}

if ($self->current_version() < 2.02) {
$self->log('debug','Upgrade for 2.01->2.02');
push(@sql,'ALTER TABLE empire ADD COLUMN alliance INTEGER');
push(@sql,'ALTER TABLE empire ADD COLUMN colony_count INTEGER');
push(@sql,'ALTER TABLE empire ADD COLUMN level INTEGER');
push(@sql,'ALTER TABLE empire ADD COLUMN date_founded INTEGER');
push(@sql,'ALTER TABLE empire ADD COLUMN affinity TEXT');
push(@sql,'ALTER TABLE empire ADD COLUMN last_checked INTEGER');
}

if (scalar @sql) {
foreach my $sql (@sql) {
$storage->do($sql)
or $self->abort('Could not excecute sql %s: %s',$sql,$storage->errstr);
}
}

$self->current_version($self->latest_version);
$storage->do('INSERT OR REPLACE INTO meta (key,value) VALUES (?,?)',{},'database_version',$self->latest_version);

return;
}


__PACKAGE__->meta->make_immutable;
no Moose;
1;

0 comments on commit 2501c38

Please sign in to comment.