line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package CloudCron::Workers::System; |
2
|
1
|
|
|
1
|
|
106013
|
use Moose; |
|
1
|
|
|
|
|
378077
|
|
|
1
|
|
|
|
|
11
|
|
3
|
1
|
|
|
1
|
|
6929
|
use namespace::autoclean; |
|
1
|
|
|
|
|
6363
|
|
|
1
|
|
|
|
|
4
|
|
4
|
1
|
|
|
1
|
|
519
|
use Data::Dumper; |
|
1
|
|
|
|
|
4754
|
|
|
1
|
|
|
|
|
47
|
|
5
|
1
|
|
|
1
|
|
394
|
use POSIX; |
|
1
|
|
|
|
|
4107
|
|
|
1
|
|
|
|
|
5
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
8
|
|
|
|
|
|
|
#ABSTRACT: A simple distributed cloud friendly cron for the masses |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
with 'SQS::Worker', 'SQS::Worker::DecodeJson'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub process_message { |
13
|
|
|
|
|
|
|
my ($self, $message) = @_; |
14
|
|
|
|
|
|
|
if ($self->_can_execute($message)) { |
15
|
|
|
|
|
|
|
my $env = $self->_prepare_env($message) || {}; |
16
|
|
|
|
|
|
|
my $cmd = $self->_prepare_command($message); |
17
|
|
|
|
|
|
|
$self->execute($env, $cmd); |
18
|
|
|
|
|
|
|
$self->_log_command($message); |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub _can_execute { |
23
|
0
|
|
|
0
|
|
|
my ($self, $message) = @_; |
24
|
0
|
0
|
|
|
|
|
if ($message->{ type } ne 'shell') { |
25
|
0
|
|
|
|
|
|
$self->log->error('Command not of type "shell". Will not be executed.'); |
26
|
|
|
|
|
|
|
} |
27
|
0
|
|
|
|
|
|
return 1; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub _prepare_env { |
31
|
0
|
|
|
0
|
|
|
my ($self, $message) = @_; |
32
|
0
|
0
|
|
|
|
|
return defined $message->{ env } ? $message->{ env } : {}; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub _prepare_command { |
36
|
0
|
|
|
0
|
|
|
my ($self, $message) = @_; |
37
|
0
|
|
|
|
|
|
return $message->{ command }; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub _log_command { |
41
|
0
|
|
|
0
|
|
|
my ($self, $message) = @_; |
42
|
0
|
|
|
|
|
|
map { $self->log->debug($_) } ( |
|
0
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
"Executed command", |
44
|
|
|
|
|
|
|
" type: $message->{ type }", |
45
|
|
|
|
|
|
|
" cmd: $message->{ command }", |
46
|
|
|
|
|
|
|
#" envs: " . Dumper($message->{ env }) |
47
|
|
|
|
|
|
|
); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub execute { |
51
|
0
|
|
|
0
|
0
|
|
my ($self, $env_ref, $cmd) = @_; |
52
|
0
|
|
|
|
|
|
my %env = %$env_ref; |
53
|
0
|
|
|
|
|
|
foreach my $var (keys %env) { |
54
|
0
|
|
|
|
|
|
$ENV{$var} = $env{$var}; |
55
|
|
|
|
|
|
|
} |
56
|
0
|
|
|
|
|
|
eval { |
57
|
0
|
|
|
|
|
|
$self->log->debug("about to execute $cmd"); |
58
|
0
|
|
|
|
|
|
$0 = "cloudcron-worker for $cmd"; |
59
|
0
|
0
|
|
|
|
|
if (system($cmd) != 0) { |
60
|
0
|
|
|
|
|
|
my $status = $?; |
61
|
0
|
0
|
|
|
|
|
if (WIFEXITED($status)){ |
|
|
0
|
|
|
|
|
|
62
|
0
|
|
|
|
|
|
my $exitcode = WEXITSTATUS($status); |
63
|
0
|
|
|
|
|
|
$self->log->error("Exit status: $exitcode for $cmd"); |
64
|
|
|
|
|
|
|
} elsif (WIFSIGNALED($status)) { |
65
|
0
|
|
|
|
|
|
my $sig = WTERMSIG($status); |
66
|
0
|
|
|
|
|
|
$self->log->error("Exited by signal: $sig on $cmd"); |
67
|
|
|
|
|
|
|
} else { |
68
|
0
|
|
|
|
|
|
$self->log->error("Uncontrolled exit: $status for $cmd"); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
} else { |
71
|
0
|
|
|
|
|
|
$self->log->info("Command $cmd executed successfully"); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
}; |
74
|
0
|
0
|
|
|
|
|
if ($@) { |
75
|
0
|
|
|
|
|
|
$self->log->error('Unknown error occurred: ' . $@); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
80
|
|
|
|
|
|
|
1; |