private Void terminateApplication()

in server/src/main/java/com/epam/aidial/core/server/service/ApplicationService.java [570:620]


    private Void terminateApplication(ResourceDescriptor resource, String error) {
        try (LockService.Lock lock = lockService.tryLock(deploymentLockKey(resource))) {
            if (lock == null) {
                return null;
            }

            Application application;

            try {
                application = getApplication(resource).getValue();
            } catch (ResourceNotFoundException e) {
                application = null;
            }

            if (isPending(application)) {
                Application.Function function = application.getFunction();

                // for public/review application source folder is equal to target folder
                // source files are copied to read-only deployment bucket for such applications
                if (!isPublicOrReview(resource)) {
                    deleteFolder(function.getTargetFolder());
                }

                controller.deleteApplicationImage(function);
                controller.deleteApplicationDeployment(function);

                resourceService.computeResource(resource, json -> {
                    Application existing = ProxyUtil.convertToObject(json, Application.class);
                    if (existing == null || !Objects.equals(existing.getFunction(), function)) {
                        throw new IllegalStateException("Application function has been updated");
                    }

                    Application.Function.Status status = (function.getStatus() == Application.Function.Status.UNDEPLOYING)
                            ? Application.Function.Status.UNDEPLOYED
                            : Application.Function.Status.FAILED;

                    function.setStatus(status);
                    function.setError(status == Application.Function.Status.FAILED ? error : null);

                    existing.setFunction(function);
                    return ProxyUtil.convertToString(existing);
                });
            }

            pendingApplications.remove(resource.getUrl());
            return null;
        } catch (Throwable e) {
            log.warn("Failed to terminate application: {}", resource.getUrl(), e);
            throw e;
        }
    }