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

io.github.honhimw.ms.api.reactive.ReactiveTasks Maven / Gradle / Ivy

There is a newer version: 1.12.0.0
Show newest version
/*
 * 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.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.github.honhimw.ms.api.reactive;

import io.github.honhimw.ms.model.*;
import io.swagger.v3.oas.annotations.Operation;
import reactor.core.publisher.Mono;
import reactor.util.retry.RetrySpec;

import java.time.Duration;
import java.util.function.Consumer;

/**
 * The tasks route gives information about the progress of the asynchronous operations.
 *
 * @author hon_him
 * @since 2024-01-02
 */

public interface ReactiveTasks {

    /**
     * Get all tasks
     *
     * @return paginated result
     */
    @Operation(method = "GET", tags = "/tasks")
    Mono> list(GetTasksRequest request);

    @Operation(method = "GET", tags = "/tasks")
    default Mono> list(Consumer builder) {
        GetTasksRequest.Builder _builder = GetTasksRequest.builder();
        builder.accept(_builder);
        return list(_builder.build());
    }

    /**
     * Delete finished tasks
     *
     * @return delete task
     */
    @Operation(method = "DELETE", tags = "/tasks")
    Mono delete(GetTasksRequest request);

    @Operation(method = "DELETE", tags = "/tasks")
    default Mono delete(Consumer builder) {
        GetTasksRequest.Builder _builder = GetTasksRequest.builder();
        builder.accept(_builder);
        return delete(_builder.build());
    }

    /**
     * Get a single task.
     *
     * @param uid uid of the requested task
     */
    @Operation(method = "GET", tags = "/tasks/{taskUid}")
    Mono get(Integer uid);

    /**
     * Cancel any number of enqueued or processing tasks based on their uid, status, type, indexUid,
     * or the date at which they were enqueued, processed, or completed.
     * Task cancelation is an atomic transaction: either all tasks are successfully canceled or none are.
     *
     * @param request A valid uids, statuses, types, indexUids, or date(beforeXAt or afterXAt) parameter is required.
     */
    @Operation(method = "POST", tags = "/tasks/cancel")
    Mono cancel(CancelTasksRequest request);

    @Operation(method = "POST", tags = "/tasks/cancel")
    default Mono cancel(Consumer builder) {
        CancelTasksRequest.Builder _builder = CancelTasksRequest.builder();
        builder.accept(_builder);
        return cancel(_builder.build());
    }

    default Mono waitForTask(int uid) {
        return waitForTask(uid, 100, Duration.ofMillis(50));
    }

    default Mono waitForTask(int uid, int maxAttempts, Duration fixedDelay) {
        return get(uid)
            .doOnNext(taskInfo -> {
                if (taskInfo.getStatus() != TaskStatus.SUCCEEDED && taskInfo.getStatus() != TaskStatus.FAILED) {
                    throw new IllegalStateException("task not completed");
                }
            })
            .retryWhen(RetrySpec.fixedDelay(maxAttempts, fixedDelay))
            .then();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy