From 69765f05ff7887059a162bc20c02387af1046fab Mon Sep 17 00:00:00 2001 From: eikek Date: Sat, 12 Mar 2022 15:45:21 +0100 Subject: [PATCH] Move user task to scheduler module --- .../scala/docspell/backend/BackendApp.scala | 2 +- .../docspell/backend/ops/OCollective.scala | 2 +- .../docspell/backend/ops/OUserTask.scala | 2 +- .../scala/docspell/joex/JoexAppImpl.scala | 3 +- .../joex/emptytrash/EmptyTrashTask.scala | 4 +- .../docspell/joex/hk/HouseKeepingTask.scala | 6 +- .../joex/updatecheck/UpdateCheckTask.scala | 5 +- .../routes/NotifyDueItemsRoutes.scala | 4 +- .../routes/PeriodicQueryRoutes.scala | 4 +- .../restserver/routes/ScanMailboxRoutes.scala | 4 +- .../scheduler/usertask}/QUserTask.scala | 83 +++++++++++++++---- .../scheduler}/usertask/UserTask.scala | 11 +-- .../scheduler}/usertask/UserTaskScope.scala | 2 +- .../scheduler}/usertask/UserTaskStore.scala | 7 +- .../store/records/RPeriodicTask.scala | 64 -------------- 15 files changed, 87 insertions(+), 116 deletions(-) rename modules/{store/src/main/scala/docspell/store/queries => scheduler/api/src/main/scala/docspell/scheduler/usertask}/QUserTask.scala (58%) rename modules/{store/src/main/scala/docspell/store => scheduler/api/src/main/scala/docspell/scheduler}/usertask/UserTask.scala (92%) rename modules/{store/src/main/scala/docspell/store => scheduler/api/src/main/scala/docspell/scheduler}/usertask/UserTaskScope.scala (97%) rename modules/{store/src/main/scala/docspell/store => scheduler/api/src/main/scala/docspell/scheduler}/usertask/UserTaskStore.scala (98%) diff --git a/modules/backend/src/main/scala/docspell/backend/BackendApp.scala b/modules/backend/src/main/scala/docspell/backend/BackendApp.scala index 64ce4b9b..c4758559 100644 --- a/modules/backend/src/main/scala/docspell/backend/BackendApp.scala +++ b/modules/backend/src/main/scala/docspell/backend/BackendApp.scala @@ -15,8 +15,8 @@ import docspell.ftsclient.FtsClient import docspell.notification.api.{EventExchange, NotificationModule} import docspell.pubsub.api.PubSubT import docspell.scheduler.msg.JobQueuePublish +import docspell.scheduler.usertask.UserTaskStore import docspell.store.Store -import docspell.store.usertask.UserTaskStore import docspell.totp.Totp import emil.Emil diff --git a/modules/backend/src/main/scala/docspell/backend/ops/OCollective.scala b/modules/backend/src/main/scala/docspell/backend/ops/OCollective.scala index f72ba217..ada2bb52 100644 --- a/modules/backend/src/main/scala/docspell/backend/ops/OCollective.scala +++ b/modules/backend/src/main/scala/docspell/backend/ops/OCollective.scala @@ -16,10 +16,10 @@ import docspell.common._ import docspell.store.UpdateResult import docspell.store.queries.{QCollective, QUser} import docspell.store.records._ -import docspell.store.usertask.{UserTask, UserTaskScope, UserTaskStore} import docspell.store.{AddResult, Store} import com.github.eikek.calev._ import docspell.scheduler.JobQueue +import docspell.scheduler.usertask.{UserTask, UserTaskScope, UserTaskStore} trait OCollective[F[_]] { diff --git a/modules/backend/src/main/scala/docspell/backend/ops/OUserTask.scala b/modules/backend/src/main/scala/docspell/backend/ops/OUserTask.scala index 3c5ade36..87b333e4 100644 --- a/modules/backend/src/main/scala/docspell/backend/ops/OUserTask.scala +++ b/modules/backend/src/main/scala/docspell/backend/ops/OUserTask.scala @@ -13,9 +13,9 @@ import fs2.Stream import docspell.common._ import docspell.notification.api.{ChannelRef, PeriodicDueItemsArgs, PeriodicQueryArgs} import docspell.scheduler.JobQueue +import docspell.scheduler.usertask.{UserTask, UserTaskScope, UserTaskStore} import docspell.store.Store import docspell.store.records.RNotificationChannel -import docspell.store.usertask._ import io.circe.Encoder trait OUserTask[F[_]] { diff --git a/modules/joex/src/main/scala/docspell/joex/JoexAppImpl.scala b/modules/joex/src/main/scala/docspell/joex/JoexAppImpl.scala index 3cb650c0..fd357ebf 100644 --- a/modules/joex/src/main/scala/docspell/joex/JoexAppImpl.scala +++ b/modules/joex/src/main/scala/docspell/joex/JoexAppImpl.scala @@ -41,10 +41,9 @@ import docspell.notification.api.NotificationModule import docspell.notification.impl.NotificationModuleImpl import docspell.pubsub.api.{PubSub, PubSubT} import docspell.scheduler.msg.JobQueuePublish +import docspell.scheduler.usertask.{UserTaskScope, UserTaskStore} import docspell.store.Store import docspell.store.records.{REmptyTrashSetting, RJobLog} -import docspell.store.usertask.UserTaskScope -import docspell.store.usertask.UserTaskStore import emil.javamail._ import org.http4s.client.Client diff --git a/modules/joex/src/main/scala/docspell/joex/emptytrash/EmptyTrashTask.scala b/modules/joex/src/main/scala/docspell/joex/emptytrash/EmptyTrashTask.scala index 28a834c7..04cfc337 100644 --- a/modules/joex/src/main/scala/docspell/joex/emptytrash/EmptyTrashTask.scala +++ b/modules/joex/src/main/scala/docspell/joex/emptytrash/EmptyTrashTask.scala @@ -9,14 +9,12 @@ package docspell.joex.emptytrash import cats.effect._ import cats.implicits._ import fs2.Stream - import docspell.backend.ops.{OItem, OItemSearch} import docspell.common._ import docspell.scheduler._ import docspell.store.records.RItem -import docspell.store.usertask.UserTask - import com.github.eikek.calev.CalEvent +import docspell.scheduler.usertask.UserTask object EmptyTrashTask { type Args = EmptyTrashArgs diff --git a/modules/joex/src/main/scala/docspell/joex/hk/HouseKeepingTask.scala b/modules/joex/src/main/scala/docspell/joex/hk/HouseKeepingTask.scala index 206b7c29..8515eae6 100644 --- a/modules/joex/src/main/scala/docspell/joex/hk/HouseKeepingTask.scala +++ b/modules/joex/src/main/scala/docspell/joex/hk/HouseKeepingTask.scala @@ -8,16 +8,14 @@ package docspell.joex.hk import cats.effect._ import cats.implicits._ - import docspell.backend.ops.OFileRepository import docspell.common._ import docspell.joex.Config import docspell.joex.filecopy.FileIntegrityCheckTask import docspell.scheduler.{JobTaskResultEncoder, Task} import docspell.store.records._ -import docspell.store.usertask.UserTaskScope - import com.github.eikek.calev._ +import docspell.scheduler.usertask.{QUserTask, UserTaskScope} import io.circe.Encoder import io.circe.generic.semiauto.deriveEncoder @@ -48,7 +46,7 @@ object HouseKeepingTask { Task.log[F, Unit](_.warn("Cancelling house-keeping task")) def periodicTask[F[_]: Sync](ce: CalEvent): F[RPeriodicTask] = - RPeriodicTask + QUserTask .createJson( true, UserTaskScope(DocspellSystem.taskGroup), diff --git a/modules/joex/src/main/scala/docspell/joex/updatecheck/UpdateCheckTask.scala b/modules/joex/src/main/scala/docspell/joex/updatecheck/UpdateCheckTask.scala index 7a82f65b..94db11b5 100644 --- a/modules/joex/src/main/scala/docspell/joex/updatecheck/UpdateCheckTask.scala +++ b/modules/joex/src/main/scala/docspell/joex/updatecheck/UpdateCheckTask.scala @@ -9,15 +9,12 @@ package docspell.joex.updatecheck import cats.data.OptionT import cats.effect._ import cats.implicits._ - import docspell.common._ import docspell.scheduler.Context import docspell.scheduler.Task +import docspell.scheduler.usertask.{UserTask, UserTaskScope} import docspell.store.records.RPeriodicTask import docspell.store.records.RUserEmail -import docspell.store.usertask.UserTask -import docspell.store.usertask.UserTaskScope - import emil._ object UpdateCheckTask { diff --git a/modules/restserver/src/main/scala/docspell/restserver/routes/NotifyDueItemsRoutes.scala b/modules/restserver/src/main/scala/docspell/restserver/routes/NotifyDueItemsRoutes.scala index eae528e2..04248885 100644 --- a/modules/restserver/src/main/scala/docspell/restserver/routes/NotifyDueItemsRoutes.scala +++ b/modules/restserver/src/main/scala/docspell/restserver/routes/NotifyDueItemsRoutes.scala @@ -9,7 +9,6 @@ package docspell.restserver.routes import cats.data.OptionT import cats.effect._ import cats.implicits._ - import docspell.backend.BackendApp import docspell.backend.MailAddressCodec import docspell.backend.auth.AuthToken @@ -19,8 +18,7 @@ import docspell.restapi.model._ import docspell.restserver.Config import docspell.restserver.conv.{Conversions, NonEmptyListSupport} import docspell.restserver.http4s.ClientRequestInfo -import docspell.store.usertask._ - +import docspell.scheduler.usertask._ import org.http4s._ import org.http4s.circe.CirceEntityDecoder._ import org.http4s.circe.CirceEntityEncoder._ diff --git a/modules/restserver/src/main/scala/docspell/restserver/routes/PeriodicQueryRoutes.scala b/modules/restserver/src/main/scala/docspell/restserver/routes/PeriodicQueryRoutes.scala index 496b069e..f0866e76 100644 --- a/modules/restserver/src/main/scala/docspell/restserver/routes/PeriodicQueryRoutes.scala +++ b/modules/restserver/src/main/scala/docspell/restserver/routes/PeriodicQueryRoutes.scala @@ -9,7 +9,6 @@ package docspell.restserver.routes import cats.data.{NonEmptyList, OptionT} import cats.effect._ import cats.implicits._ - import docspell.backend.BackendApp import docspell.backend.MailAddressCodec import docspell.backend.auth.AuthToken @@ -20,8 +19,7 @@ import docspell.restapi.model._ import docspell.restserver.Config import docspell.restserver.conv.{Conversions, NonEmptyListSupport} import docspell.restserver.http4s.ClientRequestInfo -import docspell.store.usertask._ - +import docspell.scheduler.usertask._ import org.http4s._ import org.http4s.circe.CirceEntityDecoder._ import org.http4s.circe.CirceEntityEncoder._ diff --git a/modules/restserver/src/main/scala/docspell/restserver/routes/ScanMailboxRoutes.scala b/modules/restserver/src/main/scala/docspell/restserver/routes/ScanMailboxRoutes.scala index 3912d872..430baae4 100644 --- a/modules/restserver/src/main/scala/docspell/restserver/routes/ScanMailboxRoutes.scala +++ b/modules/restserver/src/main/scala/docspell/restserver/routes/ScanMailboxRoutes.scala @@ -9,14 +9,12 @@ package docspell.restserver.routes import cats.data.OptionT import cats.effect._ import cats.implicits._ - import docspell.backend.BackendApp import docspell.backend.auth.AuthToken import docspell.common._ import docspell.restapi.model._ import docspell.restserver.conv.Conversions -import docspell.store.usertask._ - +import docspell.scheduler.usertask._ import org.http4s._ import org.http4s.circe.CirceEntityDecoder._ import org.http4s.circe.CirceEntityEncoder._ diff --git a/modules/store/src/main/scala/docspell/store/queries/QUserTask.scala b/modules/scheduler/api/src/main/scala/docspell/scheduler/usertask/QUserTask.scala similarity index 58% rename from modules/store/src/main/scala/docspell/store/queries/QUserTask.scala rename to modules/scheduler/api/src/main/scala/docspell/scheduler/usertask/QUserTask.scala index fdf1f220..f724dfe9 100644 --- a/modules/store/src/main/scala/docspell/store/queries/QUserTask.scala +++ b/modules/scheduler/api/src/main/scala/docspell/scheduler/usertask/QUserTask.scala @@ -1,20 +1,15 @@ -/* - * Copyright 2020 Eike K. & Contributors - * - * SPDX-License-Identifier: AGPL-3.0-or-later - */ - -package docspell.store.queries - -import fs2._ +package docspell.scheduler.usertask +import cats.implicits._ +import cats.effect.Sync +import com.github.eikek.calev.CalEvent import docspell.common._ +import docspell.store.qb.DML import docspell.store.qb.DSL._ -import docspell.store.qb._ -import docspell.store.records._ -import docspell.store.usertask.{UserTask, UserTaskScope} - -import doobie._ +import docspell.store.records.RPeriodicTask +import doobie.ConnectionIO +import fs2.Stream +import io.circe.Encoder object QUserTask { private val RT = RPeriodicTask.T @@ -101,4 +96,64 @@ object QUserTask { def makeUserTask(r: RPeriodicTask): UserTask[String] = UserTask(r.id, r.task, r.enabled, r.timer, r.summary, r.args) + def create[F[_]: Sync]( + enabled: Boolean, + scope: UserTaskScope, + task: Ident, + args: String, + subject: String, + priority: Priority, + timer: CalEvent, + summary: Option[String] + ): F[RPeriodicTask] = + Ident + .randomId[F] + .flatMap(id => + Timestamp + .current[F] + .map { now => + RPeriodicTask( + id, + enabled, + task, + scope.collective, + args, + subject, + scope.fold(_.user, identity), + priority, + None, + None, + timer, + timer + .nextElapse(now.atZone(Timestamp.UTC)) + .map(_.toInstant) + .map(Timestamp.apply) + .getOrElse(Timestamp.Epoch), + now, + summary + ) + } + ) + + def createJson[F[_]: Sync, A]( + enabled: Boolean, + scope: UserTaskScope, + task: Ident, + args: A, + subject: String, + priority: Priority, + timer: CalEvent, + summary: Option[String] + )(implicit E: Encoder[A]): F[RPeriodicTask] = + create[F]( + enabled, + scope, + task, + E(args).noSpaces, + subject, + priority, + timer, + summary + ) + } diff --git a/modules/store/src/main/scala/docspell/store/usertask/UserTask.scala b/modules/scheduler/api/src/main/scala/docspell/scheduler/usertask/UserTask.scala similarity index 92% rename from modules/store/src/main/scala/docspell/store/usertask/UserTask.scala rename to modules/scheduler/api/src/main/scala/docspell/scheduler/usertask/UserTask.scala index 13685aca..1d2dcd65 100644 --- a/modules/store/src/main/scala/docspell/store/usertask/UserTask.scala +++ b/modules/scheduler/api/src/main/scala/docspell/scheduler/usertask/UserTask.scala @@ -4,18 +4,15 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -package docspell.store.usertask +package docspell.scheduler.usertask import cats.effect._ import cats.implicits._ - +import com.github.eikek.calev.CalEvent import docspell.common._ import docspell.common.syntax.all._ import docspell.store.records.RPeriodicTask - -import com.github.eikek.calev.CalEvent -import io.circe.Decoder -import io.circe.Encoder +import io.circe.{Decoder, Encoder} case class UserTask[A]( id: Ident, @@ -51,7 +48,7 @@ object UserTask { scope: UserTaskScope, subject: Option[String] ): F[RPeriodicTask] = - RPeriodicTask + QUserTask .create[F]( ut.enabled, scope, diff --git a/modules/store/src/main/scala/docspell/store/usertask/UserTaskScope.scala b/modules/scheduler/api/src/main/scala/docspell/scheduler/usertask/UserTaskScope.scala similarity index 97% rename from modules/store/src/main/scala/docspell/store/usertask/UserTaskScope.scala rename to modules/scheduler/api/src/main/scala/docspell/scheduler/usertask/UserTaskScope.scala index 6b269d1d..45ba0311 100644 --- a/modules/store/src/main/scala/docspell/store/usertask/UserTaskScope.scala +++ b/modules/scheduler/api/src/main/scala/docspell/scheduler/usertask/UserTaskScope.scala @@ -4,7 +4,7 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -package docspell.store.usertask +package docspell.scheduler.usertask import docspell.common._ diff --git a/modules/store/src/main/scala/docspell/store/usertask/UserTaskStore.scala b/modules/scheduler/api/src/main/scala/docspell/scheduler/usertask/UserTaskStore.scala similarity index 98% rename from modules/store/src/main/scala/docspell/store/usertask/UserTaskStore.scala rename to modules/scheduler/api/src/main/scala/docspell/scheduler/usertask/UserTaskStore.scala index e19fa035..0570531d 100644 --- a/modules/store/src/main/scala/docspell/store/usertask/UserTaskStore.scala +++ b/modules/scheduler/api/src/main/scala/docspell/scheduler/usertask/UserTaskStore.scala @@ -4,17 +4,14 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -package docspell.store.usertask +package docspell.scheduler.usertask import cats.data.OptionT import cats.effect._ import cats.implicits._ -import fs2.Stream - import docspell.common._ -import docspell.store.queries.QUserTask import docspell.store.{AddResult, Store} - +import fs2.Stream import io.circe._ /** User tasks are `RPeriodicTask`s that can be managed by the user. The user can change diff --git a/modules/store/src/main/scala/docspell/store/records/RPeriodicTask.scala b/modules/store/src/main/scala/docspell/store/records/RPeriodicTask.scala index d0e27f07..2f298dff 100644 --- a/modules/store/src/main/scala/docspell/store/records/RPeriodicTask.scala +++ b/modules/store/src/main/scala/docspell/store/records/RPeriodicTask.scala @@ -9,16 +9,12 @@ package docspell.store.records import cats.data.NonEmptyList import cats.effect._ import cats.implicits._ - import docspell.common._ import docspell.store.qb.DSL._ import docspell.store.qb._ -import docspell.store.usertask.UserTaskScope - import com.github.eikek.calev.CalEvent import doobie._ import doobie.implicits._ -import io.circe.Encoder /** A periodic task is a special job description, that shares a few properties of a * `RJob`. It must provide all information to create a `RJob` value eventually. @@ -65,66 +61,6 @@ case class RPeriodicTask( object RPeriodicTask { - def create[F[_]: Sync]( - enabled: Boolean, - scope: UserTaskScope, - task: Ident, - args: String, - subject: String, - priority: Priority, - timer: CalEvent, - summary: Option[String] - ): F[RPeriodicTask] = - Ident - .randomId[F] - .flatMap(id => - Timestamp - .current[F] - .map { now => - RPeriodicTask( - id, - enabled, - task, - scope.collective, - args, - subject, - scope.fold(_.user, identity), - priority, - None, - None, - timer, - timer - .nextElapse(now.atZone(Timestamp.UTC)) - .map(_.toInstant) - .map(Timestamp.apply) - .getOrElse(Timestamp.Epoch), - now, - summary - ) - } - ) - - def createJson[F[_]: Sync, A]( - enabled: Boolean, - scope: UserTaskScope, - task: Ident, - args: A, - subject: String, - priority: Priority, - timer: CalEvent, - summary: Option[String] - )(implicit E: Encoder[A]): F[RPeriodicTask] = - create[F]( - enabled, - scope, - task, - E(args).noSpaces, - subject, - priority, - timer, - summary - ) - final case class Table(alias: Option[String]) extends TableDef { val tableName = "periodic_task"