File Coverage

blib/lib/Games/Lacuna/Task/TaskRunner.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             package Games::Lacuna::TaskRunner;
2              
3 1     1   1401 use 5.010;
  1         4  
  1         53  
4             our $VERSION = $Games::Lacuna::Task::VERSION;
5              
6 1     1   424 use Moose;
  0            
  0            
7             extends qw(Games::Lacuna::Task);
8             with qw(MooseX::Getopt);
9              
10             use Games::Lacuna::Task::Utils qw(class_to_name name_to_class);
11             use Try::Tiny;
12              
13             has 'exclude' => (
14             is => 'rw',
15             isa => 'ArrayRef[Str]',
16             documentation => 'Select which tasks NOT to run [Multiple]',
17             predicate => 'has_exclude',
18             );
19              
20             has 'task' => (
21             is => 'rw',
22             isa => 'ArrayRef[Str]',
23             documentation => 'Select which tasks to run [Multiple, Default all]',
24             traits => ['Array'],
25             default => sub { [] },
26             handles => {
27             has_task => 'count',
28             },
29             );
30              
31             has '+configdir' => (
32             required => 1,
33             );
34              
35             sub run {
36             my ($self) = @_;
37            
38             my $client = $self->client();
39            
40             # Call lazy builder
41             $client->client;
42            
43             my $empire_name = $self->empire_name;
44            
45             $self->log('notice',("=" x ($Games::Lacuna::Task::Constants::SCREEN_WIDTH - 8)));
46             $self->log('notice',"Running tasks for empire %s",$empire_name);
47            
48             my $global_config = $client->config->{global};
49            
50             $self->task($global_config->{task})
51             if (defined $global_config->{task}
52             && ! $self->has_task);
53             $self->exclude($global_config->{exclude})
54             if (defined $global_config->{exclude}
55             && ! $self->has_exclude);
56            
57             my (@tasks,@tmp_tasks);
58             if (! $self->has_task
59             || 'all' ~~ $self->task) {
60             @tmp_tasks = $self->all_actions;
61             } else {
62             foreach my $action_class ($self->all_actions) {
63             my $action_name = class_to_name($action_class);
64             push(@tmp_tasks,$action_class)
65             if $action_name ~~ $self->task;
66             }
67             }
68            
69             foreach my $task_class (@tmp_tasks) {
70             my ($ok,$error) = $self->load_action($task_class);
71             push(@tasks,$task_class)
72             if ($ok);
73             }
74            
75             # Loop all tasks
76             TASK:
77             foreach my $task_class (@tasks) {
78             my $task_name = class_to_name($task_class);
79            
80             next
81             if $self->has_exclude && $task_name ~~ $self->exclude;
82            
83             next
84             if $task_class->meta->can('no_automatic')
85             && $task_class->meta->no_automatic;
86            
87             $self->log('notice',("-" x ($Games::Lacuna::Task::Constants::SCREEN_WIDTH - 8)));
88             $self->log('notice',"Running action %s",$task_name);
89             try {
90             my $task_config = $client->task_config($task_name);
91             my $task = $task_class->new(
92             %{$task_config}
93             );
94             $task->execute;
95             } catch {
96             $self->log('error',"An error occured while processing %s: %s",$task_class,$_);
97             }
98             }
99             $self->log('notice',("=" x ($Games::Lacuna::Task::Constants::SCREEN_WIDTH - 8)));
100             }
101              
102             __PACKAGE__->meta->make_immutable;
103             no Moose;
104             1;