Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

cancel not work #3

Open
insinfo opened this issue Oct 4, 2021 · 0 comments
Open

cancel not work #3

insinfo opened this issue Oct 4, 2021 · 0 comments

Comments

@insinfo
Copy link

insinfo commented Oct 4, 2021

I followed your example , but cancel is not working, cancelToken is always null in handleCancel()

class BackupTask implements FileTask<Future<bool>> {
  final BackupRoutineModel rotinaBackup;
  LibsshWrapper libssh;
  LibsshCancelToken cancelToken;

  BackupTask(this.rotinaBackup, {this.taskId}) {}
  @override
  Future<bool> execute() {
    return _doExecute();
  }

  Future<bool> _doExecute() async {
    var completer = Completer<bool>();
    cancelToken = LibsshCancelToken();
    try {
     
      var server = rotinaBackup.servers.first;
      libssh = LibsshWrapper(
        server.host,
        username: server.user,
        password: server.password,
        port: server.port,
        verbosity: false,
      );
      libssh.connect();

      List<DirectoryItem> fileObjects = server.fileObjects;
      int totalSize = 0;
      int totalLoaded = 0;

      totalSize = fileObjects
          .map((d) => libssh.getSizeOfFileSystemItem(d))
          .toList()
          .reduce((value, element) => value + element);

      final start = DateTime.now();
      for (var item in fileObjects) {
        if (item.type == DirectoryItemType.directory) {
          await libssh.scpDownloadDirectory(
            item.path,
            rotinaBackup.destinationDirectory,
            printLog: (v) {
              tasklogCallback(v);
            },
            callbackStats: (int total, int loaded, int currentFileSize,
                int countDirectory, int countFiles) {
              print('i');
              totalLoaded += currentFileSize;
              taskProgressCallback(totalSize, totalLoaded, 'a');
            },
            cancelToken: cancelToken,
          );
        } else if (item.type == DirectoryItemType.file) {
          var currentFileSize = 0;
          await libssh.scpDownloadFileTo(
            item.path,
            '${rotinaBackup.destinationDirectory}/${item.name}',
            callbackStats: (int total, int loaded) {
              currentFileSize = loaded;
            },
            recursive: false,
          );

          totalLoaded += currentFileSize;
          taskProgressCallback(totalSize, totalLoaded, 'a');
        }
        if (cancelToken.isCancelled) {
          break;
        }
      }

      tasklogCallback('${DateTime.now().difference(start)}');
      completer.complete(true);
    } catch (e, s) {
      tasklogCallback('BackupTask ${rotinaBackup.name} error: $e $s');
      completer.completeError(e);
    } finally {
     
      libssh.dispose();
    }

    return completer.future;
  }

  @override
  String taskId;

  @override
  ActionType actionType = ActionType.download;

  ///Function(int total, int loaded, String status);
  @override
  ProgressCallback taskProgressCallback;

  @override
  void handleCancel(String taskId) {
//cancelToken is null
    print('BackupTask@handleCancel $cancelToken');
    cancelToken?.cancel('Cancel backup $taskId');
  }

  @override
  LogCallback tasklogCallback;
}
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant