0

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;
    }
}
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
hydar_akbar
  • 55
  • 1
  • 8

2 Answers2

2

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.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • Thanks for your help. I'm doing continue because I want to skip all accounts where connection is failed to establish. – hydar_akbar Jun 20 '22 at 13:56
  • But where to configue this? :) I'm not experienced developer. I'm just wondering how I can skip the failed connections and go to next one. I don't want it to stop execution on some exception. – hydar_akbar Jun 20 '22 at 13:58
  • See [phpseclib](https://phpseclib.com/docs/diagnosis) for the exceptions it may throw... this is for the config, while the name-space in string `SftpConnectivityChecker` obviously needs to match. Just see the links provided; they show where/how. It's even completely unclear what `$cred` even is. – Martin Zeitler Jun 20 '22 at 14:01
  • where can I override that configuration? – DeveloperX Aug 10 '23 at 23:52
0

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;
        }
    }
hydar_akbar
  • 55
  • 1
  • 8