File Coverage

blib/lib/Games/Lacuna/Task.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             # ============================================================================
2             package Games::Lacuna::Task;
3             # ============================================================================
4              
5 1     1   21955 use 5.010;
  1         4  
  1         66  
6             our $AUTHORITY = 'cpan:MAROS';
7             our $VERSION = "2.05";
8              
9 1     1   526 use Moose;
  0            
  0            
10              
11             use Games::Lacuna::Task::Types;
12             use Games::Lacuna::Task::Meta::Class::Trait::NoAutomatic;
13             use Games::Lacuna::Task::Meta::Class::Trait::Deprecated;
14             use Games::Lacuna::Task::Constants;
15              
16             with qw(Games::Lacuna::Task::Role::Client
17             Games::Lacuna::Task::Role::Logger
18             Games::Lacuna::Task::Role::Actions);
19              
20             has 'lockfile' => (
21             is => 'rw',
22             isa => 'Path::Class::File',
23             traits => ['NoGetopt'],
24             lazy_build => 1,
25             );
26              
27             sub _build_lockfile {
28             my ($self) = @_;
29            
30             return $self->configdir->file('lacuna.pid');
31             }
32              
33             sub BUILD {
34             my ($self) = @_;
35            
36             my $lockcounter = 0;
37             my $lockfile = $self->lockfile;
38            
39             # Check for configdir
40             unless (-e $self->configdir) {
41             $self->log('notice','Creating Games-Lacuna-Task config directory at %s',$self->configdir);
42             $self->configdir->mkpath();
43             }
44            
45             # Check for lockfile
46             while (-e $lockfile) {
47             my ($pid) = $lockfile->slurp(chomp => 1);
48            
49             if ($lockcounter > 10) {
50             $self->abort('Could not aquire lock (%s)',$lockfile);
51             } else {
52             $self->log('warn','Another process is currently running. Waiting until it has finished');
53             }
54             $lockcounter++;
55             sleep 15;
56             }
57            
58             # Write lock file
59             my $lockfh = $lockfile->openw();
60             print $lockfh $$;
61             $lockfh->close;
62            
63             return $self;
64             }
65              
66             sub DEMOLISH {
67             my ($self) = @_;
68            
69             $self->lockfile->remove
70             if -e $self->lockfile;
71             return;
72             }
73              
74              
75             __PACKAGE__->meta->make_immutable;
76             no Moose;
77             1;
78              
79             =encoding utf8
80              
81             =head1 NAME
82              
83             Games::Lacuna::Task - Automation framework for the Lacuna Expanse MMPOG
84              
85             =head1 SYNOPSIS
86              
87             my $task = Games::Lacuna::Task->new(
88             task => ['recycle','repair'],
89             config => {
90             recycle => ...
91             },
92             );
93             $task->run();
94              
95             or via commandline (see L<bin/lacuna_task> and L<bin/lacuna_run>)
96              
97             =head1 DESCRIPTION
98              
99             This module provides a framework for implementing various automation tasks for
100             the Lacuna Expanse MMPOG. It provides
101              
102             =over
103              
104             =item * a way of customizing which tasks to run in which order
105              
106             =item * a convinient command line interface
107              
108             =item * a logging mechanism
109              
110             =item * configuration handling
111              
112             =item * cache for increasing speed and reducing rpc calls
113              
114             =item * simple access to the Lacuna API (via Games::Lacuna::Client)
115              
116             =item * many useful helper methods and roles
117              
118             =item * implements several common tasks
119              
120             =back
121              
122             =head1 CONFIGURATION
123              
124             Games::Lacuna::Task uses a yaml configuration file which is loaded from the
125             database directory (defaults to ~/.lacuna). The filename should be config.yml
126             or lacuna.yml.
127              
128             If you run C<lacuna_task> for the first time the programm will guide you
129             through the setup process and create a basic config file.
130              
131             Example config.yml
132              
133             ---
134             connect:
135             name: "empire_name"
136             password: "empire_password"
137             uri: "http://..." # optional
138             api_key: "a1f9...." # optional
139             global:
140             task:
141             - excavate
142             - bleeder
143             - repair
144             - dispose
145             dispose_percentage: 80
146             excavate:
147             excavator_count: 3
148              
149             The data of the configuration file must be a hash with hash keys corresponding
150             to the lowecase task names. The hash key 'global' should be used for
151             global settings.
152              
153             global.task specifies which tasks should be run by default and is only used
154             if no tasks have been set explicitly (e.g. via command line).
155              
156             global.exclude specifies which tasks should be skipped default and is only
157             used if no tasks have been set explicitly or via config.
158              
159             global.exclude_planet and *.exclude_planet can be used to exclude certain
160             bodies from being processed.
161              
162             All other values in the global section are used as default values for tasks.
163             (e.g. the 'dispose_percentage' setting can be used by the WasteMonument and
164             the WasteDispose task)
165              
166             Username, password, empire name, api key and server url must be stored under
167             the connect key in the config file.
168              
169             =head1 AUTHOR
170              
171             MaroÅ¡ Kollár
172             CPAN ID: MAROS
173             maros [at] k-1.com
174            
175             L<http://www.revdev.at>
176              
177             =head1 COPYRIGHT
178              
179             Games-Lacuna-Task is Copyright (c) 2012 MaroÅ¡ Kollár
180             - L<http://www.k-1.com>
181              
182             =head1 LICENCE
183              
184             This library is free software, you can redistribute it and/or modify
185             it under the same terms as Perl itself.
186              
187             =cut