can I use validateOnly=true in BatchJobService

191 views
Skip to first unread message

Andy Lin

unread,
Jan 4, 2016, 5:09:16 PM1/4/16
to AdWords API Forum
I want to test my mutate requests in BatchJobService using validateOnly=true before submitting.  but I couldn't poll the job result using the batchId, it returns null from the following statement, is it possible to use validateOnly=true and test the mutate operations, and how can I poll the result?
BatchJobPage batchJobPage=batchJobService.get(selector);

this is my polling method, it is similar to the coding example.

private void pollJobResult(BatchJob batchJob,BatchJobServiceInterface batchJobService,BatchJobHelper batchJobHelper) throws Exception{

        // Poll for completion of the batch job using an exponential back off.
        int pollAttempts = 0;
        boolean isPending = true;
        Selector selector =
            new SelectorBuilder()
                .fields(BatchJobField.Id, BatchJobField.Status, BatchJobField.DownloadUrl,
                    BatchJobField.ProcessingErrors, BatchJobField.ProgressStats)
                .equalsId(batchJob.getId())
                .build();
        do {
          long sleepSeconds = (long) Math.scalb(30, pollAttempts);
          logger.debug("Sleeping {} seconds...", sleepSeconds);
         
          Thread.sleep(sleepSeconds * 1000);
    
          BatchJobPage batchJobPage=batchJobService.get(selector);
          if (batchJobPage!=null){
              batchJob = batchJobPage.getEntries(0);
             
              logger.info("Batch job ID {} has status {}.", batchJob.getId(), batchJob.getStatus());
              pollAttempts++;
              isPending = PENDING_STATUSES.contains(batchJob.getStatus());
          }
        } while (isPending && pollAttempts < MAX_POLL_ATTEMPTS);

        if (isPending) {
          throw new TimeoutException(
              "Job is still in pending state after polling " + MAX_POLL_ATTEMPTS + " times.");
        }

        if (batchJob.getProcessingErrors() != null) {
          int i = 0;
          for (BatchJobProcessingError processingError : batchJob.getProcessingErrors()) {
              logger.error( "  Processing error {}: errorType={}, trigger={}, errorString={}, fieldPath={}"
                      + ", reason=%s%n",
                    i++, processingError.getApiErrorType(), processingError.getTrigger(),
                    processingError.getErrorString(), processingError.getFieldPath(),
                    processingError.getReason());
          }
        }

       /* if (batchJob.getDownloadUrl() != null && batchJob.getDownloadUrl().getUrl() != null) {
          BatchJobMutateResponse mutateResponse =
              batchJobHelper.downloadBatchJobMutateResponse(batchJob.getDownloadUrl().getUrl());        
         logger.info("Downloaded results from {}", batchJob.getDownloadUrl().getUrl());
          for (MutateResult mutateResult : mutateResponse.getMutateResults()) {
            String outcome = mutateResult.getErrorList() == null ? "SUCCESS" : "FAILURE";
            logger.info("  Operation {} - {}", mutateResult.getIndex(), outcome);         
          }
        }*/
       
        if (batchJob.getDownloadUrl() != null && batchJob.getDownloadUrl().getUrl() != null) {
            BatchJobMutateResponse mutateResponse =
                batchJobHelper.downloadBatchJobMutateResponse(batchJob.getDownloadUrl().getUrl());
            System.out.printf("Downloaded results from %s:%n", batchJob.getDownloadUrl().getUrl());
            for (MutateResult mutateResult : mutateResponse.getMutateResults()) {
              String outcome = mutateResult.getErrorList() == null ? "SUCCESS" : "FAILURE";
              StringBuilder stringBuilder=new StringBuilder();
              if (mutateResult.getErrorList()!=null){
                  for (ApiError error: mutateResult.getErrorList().getErrors()){
                      stringBuilder.append(error.getFieldPath()).append(" ").append(error.getErrorString()).append("\n");
                  }
              }
              logger.error("  Operation {} - status: {} reason:{} ", mutateResult.getIndex(), outcome, stringBuilder.toString());
            }
          }
    }

Umesh Dengale

unread,
Jan 4, 2016, 5:42:33 PM1/4/16
to AdWords API Forum
Hello,

The validationOnly property (true) will validate the request and will not perform the actual operation and return the failing operations' errors if any. When a batch job completes (validationOnly=false) with the DONE status then you could download the results for each operation from the batch job's downloadUrl. Please check out the Download the batch job results and check for errors section from the Batch processing guide.

Thanks,
Umesh, AdWords API Team.

Andy Lin

unread,
Jan 5, 2016, 11:10:22 AM1/5/16
to AdWords API Forum
I understand this, but if I don't set validationOnly=true, the polling return me a batchJob object, if I set it to validationOnly=true, the polling return me null.  I am wondering if this is a bug ?  My polling method is a standard method from your web site documentation.

Umesh Dengale

unread,
Jan 5, 2016, 5:21:32 PM1/5/16
to AdWords API Forum
Hello,

The BatchJob service will get back a null from API call you make using validateOnly=true if there are no error in the validations and this is expected behavior. 

Regards,
Umesh, AdWords API Team.
Reply all
Reply to author
Forward
0 new messages