Skip to content

cancel not work #3

Description

@insinfo

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;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions