MCPcopy Index your code
hub / github.com/Effect-TS/effect / make

Function make

packages/cluster/src/ClusterCron.ts:27–124  ·  view source on GitHub ↗
(options: {
  readonly name: string
  readonly cron: Cron.Cron
  readonly execute: Effect.Effect<void, E, R>

  /**
   * Choose a shard group to run this cron job on.
   */
  readonly shardGroup?: string | undefined

  /**
   * Whether to run the next cron job based from the time of the previous run.
   *
   * Defaults to `false`, meaning the next run will be calculated from the
   * current time.
   */
  readonly calculateNextRunFromPrevious?: boolean | undefined

  /**
   * If set, the cron job will skip execution if the scheduled time is older
   * than this duration.
   *
   * This is useful to prevent running jobs that were scheduled too far in the
   * past.
   *
   * Defaults to "1 day".
   */
  readonly skipIfOlderThan?: Duration.DurationInput | undefined
})

Source from the content-addressed store, hash-verified

25 * @category Constructors
26 */
27export 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

Callers

nothing calls this directly

Calls 10

mergeMethod · 0.80
makeClientFunction · 0.70
annotateMethod · 0.65
makeMethod · 0.65
nextMethod · 0.65
pipeMethod · 0.65
mapMethod · 0.65
toLayerMethod · 0.65
effectFunction · 0.50
runMethod · 0.45

Tested by

no test coverage detected