/**
 * Copyright 2020 LinkedIn Corp. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */
package com.github.ambry.cloud;

import com.codahale.metrics.Timer;
import com.github.ambry.account.AccountService;
import com.github.ambry.account.AccountUtils;
import com.github.ambry.account.Container;
import java.util.Set;
import org.apache.helix.task.Task;
import org.apache.helix.task.TaskResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Helix task to get the deprecated containers information from {@link AccountService} and update
 * it to cloud.
 */
public class DeprecatedContainerCloudSyncTask implements Task {
  private static final Logger logger = LoggerFactory.getLogger(DeprecatedContainerCloudSyncTask.class);
  public static String COMMAND = DeprecatedContainerCloudSyncTask.class.getSimpleName();
  private final AccountService accountService;
  private final long containerDeletionRetentionDays;
  private final CloudDestination cloudDestination;
  private final VcrMetrics vcrMetrics;

  /**
   * Constructor for {@link DeprecatedContainerCloudSyncTask}.
   * @param accountService {@link AccountService} object.
   * @param containerDeletionRetentionDays retention days for a deprecated container.
   * @param cloudDestination the {@link CloudDestination} object where deprecated container information will be updated.
   */
  public DeprecatedContainerCloudSyncTask(AccountService accountService, long containerDeletionRetentionDays,
      CloudDestination cloudDestination, VcrMetrics vcrMetrics) {
    this.accountService = accountService;
    this.containerDeletionRetentionDays = containerDeletionRetentionDays;
    this.cloudDestination = cloudDestination;
    this.vcrMetrics = vcrMetrics;
  }

  @Override
  public TaskResult run() {
    Timer.Context deprecationTaskRunTimer = vcrMetrics.deprecationTaskRunTime.time();
    try {
      logger.info("DeprecatedContainerCloudSyncTask run started.");
      Timer.Context accountServiceFetchTimer = vcrMetrics.accountServiceFetchTime.time();
      Set<Container> deprecatedContainers =
          AccountUtils.getDeprecatedContainers(accountService, containerDeletionRetentionDays);
      accountServiceFetchTimer.stop();
      logger.info("Attempting deprecation of {} containers.", deprecatedContainers.size());
      cloudDestination.deprecateContainers(deprecatedContainers);
    } catch (CloudStorageException cloudStorageException) {
      logger.error("Error in updating deprecated containers from account service to cloud: ", cloudStorageException);
    } finally {
      logger.info("DeprecatedContainerCloudSyncTask done.");
      deprecationTaskRunTimer.stop();
    }
    return null;
  }

  @Override
  public void cancel() {

  }
}

@@ -2189,24 +2189,58 @@
 ult run() %7B%0A
+    TaskResult taskResult = null;%0A
     Timer.Co
@@ -2309,24 +2309,24 @@
 ime.time();%0A
-
     try %7B%0A  
@@ -2814,24 +2814,158 @@
 ontainers);%0A
+      taskResult =%0A          new TaskResult(TaskResult.Status.COMPLETED, %22DeprecatedContainerCloudSyncTask completed successfully.%22);%0A
     %7D catch 
@@ -3130,16 +3130,176 @@
 ption);%0A
+      taskResult = new TaskResult(TaskResult.Status.FAILED,%0A          %22DeprecatedContainerCloudSyncTask failed due to .%22 + cloudStorageException.getMessage());%0A
     %7D fi
@@ -3422,20 +3422,26 @@
  return 
-null
+taskResult
 ;%0A  %7D%0A%0A 

Return a task result object from container deletion sync task. (#1901)