All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.carousell.aws.WaitMojo Maven / Gradle / Ivy

package io.github.carousell.aws;

import com.amazonaws.services.devicefarm.AWSDeviceFarm;
import com.amazonaws.services.devicefarm.model.GetRunRequest;
import com.amazonaws.services.devicefarm.model.Run;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Mojo for waiting until a set of scheduled task has been completed on AWS Device Farm. */
@Mojo(name = "wait")
public class WaitMojo extends AbstractMojo {

  private static final Logger LOG = LoggerFactory.getLogger(RunMojo.class);

  @Parameter(defaultValue = "${project}", readonly = true, required = true)
  private MavenProject project;

  @Parameter(required = true)
  private String awsDeviceFarmAccessKey;

  @Parameter(required = true)
  private String awsDeviceFarmSecretKey;

  @Parameter(defaultValue = "us-east-1")
  private String awsRegion;

  @Parameter(required = true)
  private String[] runArns;

  @Parameter(defaultValue = "3", required = false) // in seconds
  private int interval;

  @Parameter(defaultValue = "60", required = false) // in minutes
  private int timeout;

  @Override
  public void execute() throws MojoExecutionException, MojoFailureException {
    AWSDeviceFarm aws =
        new AWSDeviceFarmService(awsDeviceFarmAccessKey, awsDeviceFarmSecretKey, awsRegion)
            .getAwsDeviceFarm();
    List runRequests = new ArrayList();
    for (String runArn : runArns) {
      runRequests.add(new GetRunRequest().withArn(runArn));
    }
    long startTime = System.currentTimeMillis();
    Iterator i = runRequests.iterator();
    while (!runRequests.isEmpty()) {
      while (i.hasNext()) {
        Run testRun = aws.getRun(i.next()).getRun();
        LOG.info("gfdydTest run {} has status {}", testRun.getArn(), testRun.getStatus());
        if (testRun.getStatus().equals("COMPLETED") || testRun.getStatus().equals("STOPPING")) {
          i.remove();
        }
        try {
          Thread.sleep(interval * 1000);
        } catch (InterruptedException exception) {
          LOG.error("Error during wait", exception);
        }
      }
      long elapsedTime = System.currentTimeMillis() - startTime;
      if (elapsedTime > timeout * 60 * 1000) {
        LOG.error("Test runs didn't complete within the {} minute timeout", timeout);
      }
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy