Jelajahi Sumber

RE windows: everythang's workin'

George Baugh 3 bulan lalu
induk
melakukan
fa5d4714fb
4 mengubah file dengan 26 tambahan dan 8 penghapusan
  1. 4 0
      bin/reap_playwright_servers
  2. 6 0
      example.pl
  3. 11 6
      lib/Playwright.pm
  4. 5 2
      lib/Playwright/Util.pm

+ 4 - 0
bin/reap_playwright_servers

@@ -9,9 +9,13 @@ use Proc::ProcessTable;
 use Playwright::Util;
 use Playwright::Util;
 use LWP::UserAgent;
 use LWP::UserAgent;
 
 
+use constant IS_WIN => $^O eq 'MSWin32';
+
 exit main() unless caller;
 exit main() unless caller;
 
 
 sub main {
 sub main {
+    die "Reaping unsupported on Windows." if IS_WIN;
+
     my $t = Proc::ProcessTable->new;
     my $t = Proc::ProcessTable->new;
     my @matches = grep { $_->cmndline =~ m/playwright_server --port/ } @{$t->table};
     my @matches = grep { $_->cmndline =~ m/playwright_server --port/ } @{$t->table};
 
 

+ 6 - 0
example.pl

@@ -7,6 +7,8 @@ use Try::Tiny;
 use Net::EmptyPort;
 use Net::EmptyPort;
 use Carp::Always;
 use Carp::Always;
 
 
+use constant IS_WIN => $^O eq 'MSWin32';
+
 NORMAL: {
 NORMAL: {
     my $handle = Playwright->new( debug => 1 );
     my $handle = Playwright->new( debug => 1 );
 
 
@@ -177,6 +179,8 @@ OPEN: {
 
 
 }
 }
 
 
+goto DONE if IS_WIN;
+
 # Example of connecting to remote CDP sessions
 # Example of connecting to remote CDP sessions
 CDP: {
 CDP: {
     local $SIG{HUP} = 'IGNORE';
     local $SIG{HUP} = 'IGNORE';
@@ -234,4 +238,6 @@ CDP: {
 # Clean up, since we left survivors
 # Clean up, since we left survivors
 require './bin/reap_playwright_servers';
 require './bin/reap_playwright_servers';
 Playwright::ServerReaper::main();
 Playwright::ServerReaper::main();
+
+DONE:
 0;
 0;

+ 11 - 6
lib/Playwright.pm

@@ -699,21 +699,26 @@ sub DESTROY ($self) {
     $self->quit();
     $self->quit();
 }
 }
 
 
-sub _wait_port( $port ) {
+sub _wait_port( $port, $timeout, $debug ) {
     # Check if the port is already live, and short-circuit if this is the case.
     # Check if the port is already live, and short-circuit if this is the case.
     if (IS_WIN) {
     if (IS_WIN) {
-        sleep 5;
-        my $result = qx{netstat -na | findstr "$port"};
+        my $result;
+        for (0..$timeout) {
+            my $result = qx{netstat -na | findstr "$port"};
+            print "Waiting on port $port: $result\n" if $debug;
+            last if $result;
+            sleep 1;
+        }
         return !!$result;
         return !!$result;
     }
     }
-    return Net::EmptyPort::wait_port( $port, 1 )
+    return Net::EmptyPort::wait_port( $port, $timeout )
 }
 }
 
 
 sub _start_server ( $port, $cdp_uri, $timeout, $debug, $cleanup ) {
 sub _start_server ( $port, $cdp_uri, $timeout, $debug, $cleanup ) {
     $debug = $debug ? '--debug' : '';
     $debug = $debug ? '--debug' : '';
 
 
     # Check if the port is already live, and short-circuit if this is the case.
     # Check if the port is already live, and short-circuit if this is the case.
-    if ( _wait_port( $port ) ) {
+    if ( _wait_port( $port, 1, $debug ) ) {
         print "Re-using playwright server on port $port...\n" if $debug;
         print "Re-using playwright server on port $port...\n" if $debug;
         # Set the PID as something bogus, we don't really care as we won't kill it
         # Set the PID as something bogus, we don't really care as we won't kill it
         return "REUSE";
         return "REUSE";
@@ -765,7 +770,7 @@ sub _start_server_windows ( $port, $timeout, $debug, $cleanup, @args) {
         my $cmdstring = join(' ', @cmdprefix, @args );
         my $cmdstring = join(' ', @cmdprefix, @args );
         print "$cmdstring\n" if $debug;
         print "$cmdstring\n" if $debug;
         system($cmdstring);
         system($cmdstring);
-        _wait_port( $port );
+        _wait_port( $port, $timeout, $debug );
         return $pid;
         return $pid;
 }
 }
 
 

+ 5 - 2
lib/Playwright/Util.pm

@@ -17,6 +17,8 @@ use POSIX();
 no warnings 'experimental';
 no warnings 'experimental';
 use feature qw{signatures};
 use feature qw{signatures};
 
 
+use constant IS_WIN => $^O eq 'MSWin32';
+
 =head2 request(STRING method, STRING url, STRING host, INTEGER port, LWP::UserAgent ua, HASH args) = HASH
 =head2 request(STRING method, STRING url, STRING host, INTEGER port, LWP::UserAgent ua, HASH args) = HASH
 
 
 De-duplicates request logic in the Playwright Modules.
 De-duplicates request logic in the Playwright Modules.
@@ -71,8 +73,9 @@ sub async ($subroutine) {
 
 
 sub _child ($filename,$subroutine) {
 sub _child ($filename,$subroutine) {
     Sereal::Encoder->encode_to_file($filename,$subroutine->());
     Sereal::Encoder->encode_to_file($filename,$subroutine->());
-    # Prevent destructors from firing due to exiting instantly
-    POSIX::_exit(0);
+    # Prevent destructors from firing due to exiting instantly...unless we are on windows, where they won't.
+    POSIX::_exit(0) unless IS_WIN;
+    exit 0;
 }
 }
 
 
 sub await ($to_wait) {
 sub await ($to_wait) {