Search in sources :

Example 6 with Profiler

use of org.slf4j.profiler.Profiler in project testcontainers-java by testcontainers.

the class RemoteDockerImage method resolve.

@Override
protected final String resolve() {
    Profiler profiler = new Profiler("Rule creation - prefetch image");
    Logger logger = DockerLoggerFactory.getLogger(imageName.toString());
    profiler.setLogger(logger);
    Profiler nested = profiler.startNested("Obtaining client");
    DockerClient dockerClient = DockerClientFactory.instance().client();
    try {
        nested.stop();
        profiler.start("Check local images");
        int attempts = 0;
        DockerClientException lastException = null;
        while (true) {
            // Does our cache already know the image?
            if (AVAILABLE_IMAGE_NAME_CACHE.contains(imageName)) {
                logger.trace("{} is already in image name cache", imageName);
                break;
            }
            // Update the cache
            ListImagesCmd listImagesCmd = dockerClient.listImagesCmd();
            if (Boolean.parseBoolean(System.getProperty("useFilter"))) {
                listImagesCmd = listImagesCmd.withImageNameFilter(imageName.toString());
            }
            List<Image> updatedImages = listImagesCmd.exec();
            updatedImages.stream().map(Image::getRepoTags).filter(Objects::nonNull).flatMap(Stream::of).map(DockerImageName::new).collect(Collectors.toCollection(() -> AVAILABLE_IMAGE_NAME_CACHE));
            // And now?
            if (AVAILABLE_IMAGE_NAME_CACHE.contains(imageName)) {
                logger.trace("{} is in image name cache following listing of images", imageName);
                break;
            }
            // Log only on first attempt
            if (attempts == 0) {
                logger.info("Pulling docker image: {}. Please be patient; this may take some time but only needs to be done once.", imageName);
                profiler.start("Pull image");
            }
            if (attempts++ >= 3) {
                logger.error("Retry limit reached while trying to pull image: {}. Please check output of `docker pull {}`", imageName, imageName);
                throw new ContainerFetchException("Retry limit reached while trying to pull image: " + imageName, lastException);
            }
            // The image is not available locally - pull it
            try {
                final PullImageResultCallback callback = new PullImageResultCallback();
                dockerClient.pullImageCmd(imageName.getUnversionedPart()).withTag(imageName.getVersionPart()).exec(callback);
                callback.awaitSuccess();
                AVAILABLE_IMAGE_NAME_CACHE.add(imageName);
                break;
            } catch (DockerClientException e) {
                lastException = e;
            }
        }
        return imageName.toString();
    } catch (DockerClientException e) {
        throw new ContainerFetchException("Failed to get Docker client for " + imageName, e);
    } finally {
        profiler.stop().log();
    }
}
Also used : Profiler(org.slf4j.profiler.Profiler) DockerClient(com.github.dockerjava.api.DockerClient) DockerClientException(com.github.dockerjava.api.exception.DockerClientException) PullImageResultCallback(com.github.dockerjava.core.command.PullImageResultCallback) ListImagesCmd(com.github.dockerjava.api.command.ListImagesCmd) Stream(java.util.stream.Stream) Logger(org.slf4j.Logger) Image(com.github.dockerjava.api.model.Image) ContainerFetchException(org.testcontainers.containers.ContainerFetchException)

Example 7 with Profiler

use of org.slf4j.profiler.Profiler in project openlmis-stockmanagement by OpenLMIS.

the class StockEventNotificationProcessor method callNotifications.

private void callNotifications(StockEventDto event, StockEventLineItemDto eventLine) {
    XLOGGER.entry(event, eventLine);
    Profiler profiler = new Profiler("CALL_NOTIFICATION_FOR_LINE_ITEM");
    profiler.setLogger(XLOGGER);
    profiler.start("COPY_STOCK_CARD");
    OrderableLotIdentity identity = OrderableLotIdentity.identityOf(eventLine);
    StockCard card = event.getContext().findCard(identity);
    StockCard copy = card.shallowCopy();
    for (StockCardLineItem line : copy.getLineItems()) {
        StockCardLineItemReason reason = line.getReason();
        if (null != reason) {
            line.setReason(event.getContext().findCardReason(reason.getId()));
        }
    }
    profiler.start("CALCULATE_STOCK_ON_HAND");
    copy.calculateStockOnHand();
    profiler.start("NOTIFY_STOCK_CARD_EDITORS");
    if (copy.getStockOnHand() == 0) {
        stockoutNotifier.notifyStockEditors(copy);
    }
    profiler.stop().log();
    XLOGGER.exit();
}
Also used : StockCardLineItemReason(org.openlmis.stockmanagement.domain.reason.StockCardLineItemReason) Profiler(org.slf4j.profiler.Profiler) StockCardLineItem(org.openlmis.stockmanagement.domain.card.StockCardLineItem) OrderableLotIdentity(org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity) StockCard(org.openlmis.stockmanagement.domain.card.StockCard)

Example 8 with Profiler

use of org.slf4j.profiler.Profiler in project openlmis-stockmanagement by OpenLMIS.

the class StockEventProcessor method process.

/**
 * Validate and persist event and create stock card and line items from it.
 *
 * @param eventDto stock event dto.
 * @return the persisted event ids.
 */
public UUID process(StockEventDto eventDto) {
    XLOGGER.entry(eventDto);
    Profiler profiler = new Profiler("PROCESS");
    profiler.setLogger(XLOGGER);
    profiler.start("BUILD_CONTEXT");
    StockEventProcessContext context = contextBuilder.buildContext(eventDto);
    eventDto.setContext(context);
    profiler.start("VALIDATE");
    stockEventValidationsService.validate(eventDto);
    UUID eventId = saveEventAndGenerateLineItems(eventDto, profiler.startNested("SAVE_AND_GENERATE_LINE_ITEMS"));
    profiler.stop().log();
    XLOGGER.exit(eventId);
    return eventId;
}
Also used : Profiler(org.slf4j.profiler.Profiler) UUID(java.util.UUID) StockEventProcessContext(org.openlmis.stockmanagement.util.StockEventProcessContext)

Example 9 with Profiler

use of org.slf4j.profiler.Profiler in project openlmis-stockmanagement by OpenLMIS.

the class StockEventsController method createStockEvent.

/**
 * Create stock event.
 *
 * @param eventDto a stock event bound to request body.
 * @return created stock event's ID.
 */
@RequestMapping(value = "stockEvents", method = POST)
public ResponseEntity<UUID> createStockEvent(@RequestBody StockEventDto eventDto) {
    LOGGER.debug("Try to create a stock event");
    XLOGGER.entry(eventDto);
    Profiler profiler = new Profiler("CREATE_STOCK_EVENT");
    profiler.setLogger(XLOGGER);
    checkPermission(eventDto, profiler.startNested("CHECK_PERMISSION"));
    profiler.start("PROCESS");
    UUID createdEventId = stockEventProcessor.process(eventDto);
    profiler.start("CREATE_RESPONSE");
    ResponseEntity<UUID> response = new ResponseEntity<>(createdEventId, CREATED);
    profiler.stop().log();
    XLOGGER.exit(response);
    return response;
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) Profiler(org.slf4j.profiler.Profiler) UUID(java.util.UUID) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Profiler (org.slf4j.profiler.Profiler)9 UUID (java.util.UUID)3 DockerClient (com.github.dockerjava.api.DockerClient)2 IOException (java.io.IOException)2 StockCard (org.openlmis.stockmanagement.domain.card.StockCard)2 OrderableLotIdentity (org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity)2 StockCardLineItemReason (org.openlmis.stockmanagement.domain.reason.StockCardLineItemReason)2 StockEventProcessContext (org.openlmis.stockmanagement.util.StockEventProcessContext)2 Logger (org.slf4j.Logger)2 BuildImageCmd (com.github.dockerjava.api.command.BuildImageCmd)1 ListImagesCmd (com.github.dockerjava.api.command.ListImagesCmd)1 DockerClientException (com.github.dockerjava.api.exception.DockerClientException)1 BuildResponseItem (com.github.dockerjava.api.model.BuildResponseItem)1 Image (com.github.dockerjava.api.model.Image)1 BuildImageResultCallback (com.github.dockerjava.core.command.BuildImageResultCallback)1 PullImageResultCallback (com.github.dockerjava.core.command.PullImageResultCallback)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 HashMap (java.util.HashMap)1