| 1 | package Blitz::Exercise; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Blitz; | ||
| 7 | use Blitz::API; | ||
| 8 | use Blitz::Validate; | ||
| 9 | use MIME::Base64; | ||
| 10 | use JSON::XS; | ||
| 11 | use LWP; | ||
| 12 | use Storable qw(dclone); | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | Blitz::Exercise - Superclass for Sprint and Rush modules | ||
| 17 | |||
| 18 | =head1 SUBROUTINES/METHODS | ||
| 19 | |||
| 20 | |||
| 21 | =head2 new | ||
| 22 | |||
| 23 | Create a blitz exercise object for executing sprints or rushes | ||
| 24 | |||
| 25 | Required parameters are a set of options to run the test, and a callback | ||
| 26 | closure to execute after each communication with the blitz.io servers | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | sub new { | ||
| 31 | my ($name, $blitzObj, $options, $callback) = @_; | ||
| 32 | # convenience vars | ||
| 33 | my $self = { | ||
| 34 | blitzObj => $blitzObj, | ||
| 35 | options => $options, | ||
| 36 | callback => $callback, | ||
| 37 | job_id => 0, | ||
| 38 | }; | ||
| 39 | |||
| 40 | bless $self; | ||
| 41 | return $self; | ||
| 42 | } | ||
| 43 | |||
| 44 | =head2 blitzObj | ||
| 45 | |||
| 46 | Returns the blitzObj for a given exercise | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | sub blitzObj { | ||
| 51 | my $self = shift; | ||
| 52 | return $self->{blitzObj}; | ||
| 53 | } | ||
| 54 | |||
| 55 | =head2 execute | ||
| 56 | |||
| 57 | Executes an exercise (a sprint or a rush) | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | sub execute { | ||
| 62 | my $self = shift; | ||
| 63 | my $blitz = $self->blitzObj(); | ||
| 64 | my $client = Blitz::get_client($self->blitzObj()); | ||
| 65 | |||
| 66 | my $clone = dclone($self->{options}); | ||
| 67 | my ($valid, $result) = Blitz::Validate::validate($clone, $self->{test_type}); | ||
| 68 | |||
| 69 | if (!$valid) { | ||
| 70 | &{$self->{callback}}($result, $result->{error}); | ||
| 71 | } | ||
| 72 | else { | ||
| 73 | # send execute request to host | ||
| 74 | |||
| 75 | my $response = $client->start_job($self->{options}, $self->{callback}); | ||
| 76 | |||
| 77 | if ($response->{job_id}) { | ||
| 78 | my $job_id = $client->job_id($response->{job_id}); | ||
| 79 | my $status = $client->status; | ||
| 80 | # wait 2 secs, then get status | ||
| 81 | until ($status eq 'completed' or | ||
| 82 | $status eq 'fail') { | ||
| 83 | sleep 2; | ||
| 84 | $response = $client->job_status; | ||
| 85 | $status = $client->status; | ||
| 86 | |||
| 87 | my $error = 0; | ||
| 88 | if ($response->{error}) { | ||
| 89 | $error = $response->{error}; | ||
| 90 | } | ||
| 91 | elsif ( | ||
| 92 | $response->{result} && | ||
| 93 | $response->{result}{error} | ||
| 94 | ) { | ||
| 95 | $error = $response->{result}{error}; | ||
| 96 | } | ||
| 97 | if ($error) { | ||
| 98 | $status = 'fail'; | ||
| 99 | &{$self->{callback}}($response, $error); | ||
| 100 | } | ||
| 101 | else { | ||
| 102 | $status = $response->{status}; | ||
| 103 | &{$self->{callback}}($response, $error); | ||
| 104 | } | ||
| 105 | $client->status($status); | ||
| 106 | } | ||
| 107 | } | ||
| 108 | else { | ||
| 109 | # no id means failure | ||
| 110 | &{$self->{callback}}($response, 'No job id returned'); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | return $self; | ||
| 114 | } | ||
| 115 | |||
| 116 | |||
| 117 | return 1; |