Last active
June 11, 2018 09:55
-
-
Save masiuchi/a410f2d281528a6f378bcf989a933317 to your computer and use it in GitHub Desktop.
Call MT create_entry endpoint many time in parallel.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Data::Dumper; | |
use Furl; | |
use JSON; | |
use Parallel::Prefork; | |
my $data_api_url = $ARGV[0]; | |
my $username = $ARGV[1]; | |
my $password = $ARGV[2]; | |
unless ( $data_api_url && $username && $password ) { | |
"[usage]: perl create_entries.pl [Data API url] [username] [password]"; | |
exit 1; | |
} | |
my $access_token; | |
{ | |
my $furl = Furl->new; | |
my $res_auth = $furl->post( | |
"$data_api_url/v2/authentication", | |
undef, | |
[ username => $username, | |
password => $password, | |
clientId => 'perl', | |
], | |
); | |
die 'authentication failed: ' . $res_auth->body | |
unless $res_auth->is_success; | |
$access_token = decode_json( $res_auth->body )->{accessToken} | |
or die 'no accessToken'; | |
} | |
my $pm = Parallel::Prefork->new( | |
{ max_workers => 10, | |
trap_signals => { | |
TERM => 'TERM', | |
HUP => 'TERM', | |
USR1 => undef, | |
} | |
} | |
); | |
my $parent_pid = $$; | |
while ( $pm->signal_received ne 'TERM' ) { | |
$pm->start( | |
sub { | |
my $res_create_entry = Furl->new->post( | |
"$data_api_url/v3/sites/1/entries", | |
[ 'X-MT-Authorization' => | |
qq{MTAuth accessToken="$access_token"}, | |
], | |
[ entry => '{}', ], | |
); | |
unless ( $res_create_entry->is_success ) { | |
print 'creating entry failed: ' | |
. $res_create_entry->body . "\n"; | |
kill 'TERM', $parent_pid; | |
return; | |
} | |
print Dumper( $res_create_entry->body ) . "\n"; | |
} | |
); | |
} | |
$pm->wait_all_children(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment