Permalink
Browse files

Refactor base class

  • Loading branch information...
maros committed Feb 18, 2012
1 parent 1e783e4 commit 2501c38e8402ef8dde0ff39fed5e0289565e1f8d
View
@@ -4,9 +4,9 @@ use strict;
use warnings;
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;
=encoding utf8
View
@@ -7,104 +7,74 @@ use 5.010;
our $AUTHORITY = 'cpan:MAROS';
our $VERSION = "2.01";
+use 5.010;
+
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 Try::Tiny;
+use Games::Lacuna::Task::Types;
+use Games::Lacuna::Task::Meta::Class::Trait::NoAutomatic;
+use Games::Lacuna::Task::Constants;
-has 'exclude' => (
- is => 'rw',
- isa => 'ArrayRef[Str]',
- documentation => 'Select which tasks NOT to run [Multiple]',
- predicate => 'has_exclude',
-);
+with qw(Games::Lacuna::Task::Role::Client
+ Games::Lacuna::Task::Role::Logger);
-has 'task' => (
- is => 'rw',
- isa => 'ArrayRef[Str]',
- documentation => 'Select which tasks to run [Multiple, Default all]',
- predicate => 'has_task',
-);
+use Module::Pluggable
+ search_path => ['Games::Lacuna::Task::Action'],
+ sub_name => '_all_actions';
-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 $client = $self->client();
-
- # Call lazy builder
- $client->client;
-
- 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);
+ return $self->configdir->file('lacuna.pid');
+}
+
+sub BUILD {
+ my ($self) = @_;
- my @tasks;
- if (! $self->has_task
- || '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;
- }
- }
+ my $lockcounter = 0;
+ my $lockfile = $self->lockfile;
- # Loop all tasks
- TASK:
- foreach my $task_class (@tasks) {
- 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;
- };
+ # Check for lockfile
+ while (-e $lockfile) {
+ my ($pid) = $lockfile->slurp(chomp => 1);
- next
- if $task_class->meta->can('no_automatic')
- && $task_class->meta->no_automatic;
-
- 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,$_);
- }
+ if ($lockcounter > 10) {
+ $self->abort('Could not aquire lock (%s)',$lockfile);
+ } else {
+ $self->log('warn','Another process is currently running. Waiting until it has finished');
}
+ $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
@@ -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
it under the same terms as Perl itself.
-=cut
-
-__PACKAGE__->meta->make_immutable;
-no Moose;
-1;
+=cut
@@ -1,11 +1,9 @@
-# ============================================================================
package Games::Lacuna::Task::ActionProto;
-# ============================================================================
use 5.010;
use Moose;
-extends qw(Games::Lacuna::Task::Base);
+extends qw(Games::Lacuna::Task);
use List::Util qw(max);
use Try::Tiny;
@@ -1,74 +0,0 @@
-# ============================================================================
-package Games::Lacuna::Task::Base;
-# ============================================================================
-
-use 5.010;
-
-use Moose;
-
-use Games::Lacuna::Task::Types;
-use Games::Lacuna::Task::Meta::Class::Trait::NoAutomatic;
-use Games::Lacuna::Task::Constants;
-
-with qw(Games::Lacuna::Task::Role::Client
- Games::Lacuna::Task::Role::Logger);
-
-use Module::Pluggable
- search_path => ['Games::Lacuna::Task::Action'],
- sub_name => '_all_actions';
-
-
-has 'lockfile' => (
- is => 'rw',
- isa => 'Path::Class::File',
- traits => ['NoGetopt'],
- lazy_build => 1,
-);
-
-sub _build_lockfile {
- my ($self) = @_;
-
- return $self->configdir->file('lacuna.pid');
-}
-
-sub BUILD {
- my ($self) = @_;
-
- my $lockcounter = 0;
- my $lockfile = $self->lockfile;
-
- # Check for lockfile
- while (-e $lockfile) {
- my ($pid) = $lockfile->slurp(chomp => 1);
-
- if ($lockcounter > 10) {
- $self->abort('Could not aquire lock');
- } else {
- $self->log('warn','Another process is currently running. Waiting until it has finished');
- }
- $lockcounter++;
- sleep 60;
- }
-
- # 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;
@@ -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;
Oops, something went wrong.

0 comments on commit 2501c38

Please sign in to comment.