| 25 | * @category Constructors |
| 26 | */ |
| 27 | export const make = <E, R>(options: { |
| 28 | readonly name: string |
| 29 | readonly cron: Cron.Cron |
| 30 | readonly execute: Effect.Effect<void, E, R> |
| 31 | |
| 32 | /** |
| 33 | * Choose a shard group to run this cron job on. |
| 34 | */ |
| 35 | readonly shardGroup?: string | undefined |
| 36 | |
| 37 | /** |
| 38 | * Whether to run the next cron job based from the time of the previous run. |
| 39 | * |
| 40 | * Defaults to `false`, meaning the next run will be calculated from the |
| 41 | * current time. |
| 42 | */ |
| 43 | readonly calculateNextRunFromPrevious?: boolean | undefined |
| 44 | |
| 45 | /** |
| 46 | * If set, the cron job will skip execution if the scheduled time is older |
| 47 | * than this duration. |
| 48 | * |
| 49 | * This is useful to prevent running jobs that were scheduled too far in the |
| 50 | * past. |
| 51 | * |
| 52 | * Defaults to "1 day". |
| 53 | */ |
| 54 | readonly skipIfOlderThan?: Duration.DurationInput | undefined |
| 55 | }): Layer.Layer<never, never, Sharding | Exclude<R, Scope>> => { |
| 56 | const CronEntity = Entity.make(`ClusterCron/${options.name}`, [ |
| 57 | Rpc.make("run", { |
| 58 | payload: CronPayload |
| 59 | }) |
| 60 | .annotate(Persisted, true) |
| 61 | .annotate(Uninterruptible, true) |
| 62 | ]) |
| 63 | .annotate(ClusterSchema.ShardGroup, () => options.shardGroup ?? "default") |
| 64 | .annotate(ClusterSchema.ClientTracingEnabled, false) |
| 65 | |
| 66 | const InitialRun = Singleton.make( |
| 67 | `ClusterCron/${options.name}`, |
| 68 | Effect.gen(function*() { |
| 69 | const now = yield* DateTime.now |
| 70 | const next = DateTime.unsafeFromDate(Cron.next(options.cron, now)) |
| 71 | const entityId = options.calculateNextRunFromPrevious ? "initial" : DateTime.formatIso(next) |
| 72 | const client = (yield* CronEntity.client)(entityId) |
| 73 | yield* client.run({ dateTime: next }, { discard: true }) |
| 74 | }), |
| 75 | { shardGroup: options.shardGroup } |
| 76 | ) |
| 77 | |
| 78 | const skipIfOlderThan = Option.fromNullable(options.skipIfOlderThan).pipe( |
| 79 | Option.map(Duration.decode), |
| 80 | Option.getOrElse(() => Duration.days(1)) |
| 81 | ) |
| 82 | |
| 83 | const effect = Effect.fnUntraced(function*(dateTime: DateTime.Utc) { |
| 84 | const now = yield* DateTime.now |