|
| 1 | +/* |
| 2 | + * Copyright 2020-2025 Typelevel |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package cats.effect.std |
| 18 | + |
| 19 | +import cats.effect.kernel._ |
| 20 | + |
| 21 | +/** |
| 22 | + * A `JobManager` allows you to launch `Jobs` in the background using a unique identifier. Then |
| 23 | + * you can use the identifier to query for the status of the job or cancel it. |
| 24 | + */ |
| 25 | +trait JobManager[F[_], Id, S] { |
| 26 | + |
| 27 | + /** |
| 28 | + * Creates and launches the given `Job` in the background. If another Job with the same id was |
| 29 | + * already running, it will be cancelled before starting this one. |
| 30 | + */ |
| 31 | + def startJob(id: Id, job: Resource[F, JobManager.Job[F, S]]): F[Unit] |
| 32 | + |
| 33 | + /** |
| 34 | + * Gets the status of the `Job` associated with the given `id`. If `id` doesn't exists or the |
| 35 | + * `Job` already finished then the returned value will be a `None`. |
| 36 | + */ |
| 37 | + def getJobStatus(id: Id): F[Option[S]] |
| 38 | + |
| 39 | + /** |
| 40 | + * Signals cancellation of the `Job` associated with the given `id`, and waits for its |
| 41 | + * completion. |
| 42 | + */ |
| 43 | + def cancelJob(id: Id): F[Unit] |
| 44 | +} |
| 45 | + |
| 46 | +object JobManager { |
| 47 | + |
| 48 | + /** |
| 49 | + * Represents a job managed by a `JobManager`. |
| 50 | + */ |
| 51 | + trait Job[F[_], S] { |
| 52 | + |
| 53 | + /** |
| 54 | + * Starts the logic of this `Job`. |
| 55 | + */ |
| 56 | + def run: F[Unit] |
| 57 | + |
| 58 | + /** |
| 59 | + * Gets the status of this `Job`. |
| 60 | + */ |
| 61 | + def getStatus: F[S] |
| 62 | + } |
| 63 | +} |
0 commit comments