I'm using Flysystem SFTP in Laravel 8. I've multiple accounts for sftp and I'm looping on every one for making adapter and then reading files from server. This all is working through console command and is registered in Schedule. The issue is when any of the connection fails due to username or password issue, it stops the execution of schedule task and skips the remaining. How can I check if connection is successful or not and continue to my next sftp connection. Thanks in advance.
foreach ($credentials as $cred) {
try {
$driver = Storage::createSFtpDriver($cred);
if($driver->exists('/reports/')) {
//Other code
} else {
continue;
}
} catch (Exception $e) {
continue;
}
}
CodePudding user response:
See SFTP V3, there SftpConnectionProvider
reads:
connectivity checker (must be an implementation of
League\Flysystem\PhpseclibV2\ConnectivityChecker
to check if a connection can be established (optional, omit if you don't need some special handling for setting reliable connections)
So the answer is SftpConnectivityChecker implements
ConnectivityChecker
... to be passed into SftpConnectionProvider
constructor. That interface
only has one single method to override:
public class SftpConnectivityChecker implements ConnectivityChecker {
public function isConnected(SFTP $connection): bool {
$connected = false
// TODO: inspect the $connection status.
return $connected;
}
}
Likely to be configured alike this:
'sftp' => [
'connectivityChecker' => 'SftpConnectivityChecker'
]
And don't use continue
, but handle the exception instead of ignoring it.
CodePudding user response:
I don't know if it is good way or not but in my case, it is working fine. I just solved it by applying \
with Exception class and it is going fine.
foreach($credentials as $cred){
try {
$driver = Storage::createSFtpDriver($cred);
if($driver->exists('/report/')){
echo "Found for ".$cred["username"];
}
else{
continue;
}
}
catch (\Exception $e) {
continue;
}
}