Skip to content

Commit

Permalink
change the default headless option keepAlive value to false to pr…
Browse files Browse the repository at this point in the history
…event chrome processes from failing to exit properly when script terminates or phpcreeper process exit
  • Loading branch information
blogdaren committed Oct 28, 2024
1 parent f06531c commit 7d0b889
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
16 changes: 12 additions & 4 deletions src/Downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -805,13 +805,14 @@ public function download($task)
}
}
}catch(\Throwable $e){
$error_code = $e->getCode() < 0 ? $e->getCode() : '-205';
$extra = array(
'url' => $task['url'],
'exception_code' => $e->getCode(),
'exception_code' => $error_code,
'exception_msg' => $e->getMessage(),
);

return Tool::throwback('-205', $this->langConfig['http_transfer_exception'], $extra);
return Tool::throwback($error_code, $this->langConfig['http_transfer_exception'], $extra);
}
}

Expand Down Expand Up @@ -871,13 +872,14 @@ public function useHeadlessBrowser($args = [])
$extra = ['content' => $content];
return Tool::throwback('0', $this->langConfig['downloader_download_task_yes'], $extra);
}catch(\Throwable $e){
$error_code = $e->getCode() < 0 ? $e->getCode() : '-205';
$extra = array(
'url' => $args['url'],
'exception_code' => $e->getCode(),
'exception_code' => $error_code,
'exception_msg' => $e->getMessage(),
);

return Tool::throwback('-205', $this->langConfig['http_transfer_exception'], $extra);
return Tool::throwback($error_code, $this->langConfig['http_transfer_exception'], $extra);
}
}

Expand Down Expand Up @@ -975,6 +977,12 @@ public function rebuildTaskArguments($args = [])
});
}

//force to keep option `headless_browser.headless`
if(empty($args['context']['headless_browser']['headless']))
{
$args['headless_browser']['headless'] = false;
}

//unset ditry data
$unset_array = ['id', 'rule_name', 'rule', 'depth', 'create_time', 'referer', 'context'];
array_walk($unset_array, function($v, $k)use(&$args){
Expand Down
7 changes: 4 additions & 3 deletions src/Kernel/Middleware/HeadlessBrowser/Chrome.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ static public function getDefaultArguments()
return [
'headless' => true,
'noSandbox' => true,
'keepAlive' => true,
'keepAlive' => false,
'ignoreCertificateErrors' => true,
'sendSyncDefaultTimeout' => 10000,
];
Expand Down Expand Up @@ -262,7 +262,6 @@ public function request($method, $url, $args = [])
]));
}

//issue http request
$page = self::getPage();

try{
Expand All @@ -271,7 +270,9 @@ public function request($method, $url, $args = [])
$page->close();
}catch(\Throwable $e){
$page->close();
throw new \Exception($e->getMessage());
//since the headless lib don't define an exception code,
//so we have to define a uniform exception code here....
throw new \Exception($e->getMessage(), -400);
}

return $html;
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/PHPCreeper.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class PHPCreeper extends Worker
*
* @var string
*/
public const CURRENT_VERSION = '1.9.7';
public const CURRENT_VERSION = '1.9.8';

/**
* engine name
Expand Down

0 comments on commit 7d0b889

Please sign in to comment.