File size: 4,257 Bytes
31e8457
689aa9f
 
 
 
 
 
 
 
31e8457
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
689aa9f
 
 
 
 
 
 
 
 
31e8457
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
 * Runs recurring cron jobs through cluster sharding.
 *
 * This module turns a `Cron.Cron` schedule into a `Layer` that coordinates one
 * recurring job across a cluster. It registers a singleton for the initial
 * scheduling step and a persisted entity message for each run. This is useful
 * for distributed maintenance work where the job should be owned by the cluster
 * rather than by every runner independently.
 *
 * @since 4.0.0
 */
import * as Cron from "../../Cron.js";
import * as DateTime from "../../DateTime.js";
import * as Duration from "../../Duration.js";
import * as Effect from "../../Effect.js";
import * as Exit from "../../Exit.js";
import * as Layer from "../../Layer.js";
import * as Option from "../../Option.js";
import * as PrimaryKey from "../../PrimaryKey.js";
import * as Schedule from "../../Schedule.js";
import * as Schema from "../../Schema.js";
import * as Rpc from "../rpc/Rpc.js";
import * as ClusterSchema from "./ClusterSchema.js";
import { Persisted, Uninterruptible } from "./ClusterSchema.js";
import * as DeliverAt from "./DeliverAt.js";
import * as Entity from "./Entity.js";
import * as Singleton from "./Singleton.js";
/**
 * Creates a layer that runs a cron job through the cluster sharding system.
 *
 * **Details**
 *
 * The job is scheduled as persisted entity messages, with an initial singleton
 * scheduling step and optional controls for shard group, next-run calculation,
 * and skipping stale scheduled runs.
 *
 * @category constructors
 * @since 4.0.0
 */
export const make = options => {
  const CronEntity = Entity.make(`ClusterCron/${options.name}`, [Rpc.make("run", {
    payload: CronPayload
  }).annotate(Persisted, true).annotate(Uninterruptible, true)]).annotate(ClusterSchema.ShardGroup, () => options.shardGroup ?? "default").annotate(ClusterSchema.ClientTracingEnabled, false);
  const InitialRun = Singleton.make(`ClusterCron/${options.name}`, Effect.gen(function* () {
    const now = yield* DateTime.now;
    const next = DateTime.fromDateUnsafe(Cron.next(options.cron, now));
    const entityId = options.calculateNextRunFromPrevious ? "initial" : DateTime.formatIso(next);
    const client = (yield* CronEntity.client)(entityId);
    yield* client.run({
      dateTime: next
    }, {
      discard: true
    });
  }), {
    shardGroup: options.shardGroup
  });
  const skipIfOlderThan = Option.fromUndefinedOr(options.skipIfOlderThan).pipe(Option.map(Duration.fromInputUnsafe), Option.getOrElse(() => Duration.days(1)));
  const effect = Effect.fnUntraced(function* (dateTime) {
    const now = yield* DateTime.now;
    if (DateTime.isLessThan(dateTime, DateTime.subtractDuration(now, skipIfOlderThan))) {
      return;
    }
    return yield* options.execute;
  }, Effect.orDie);
  const EntityLayer = CronEntity.toLayer(Effect.gen(function* () {
    const makeClient = yield* CronEntity.client;
    return {
      run: request => Effect.onExitPrimitive(effect(request.payload.dateTime), Effect.fnUntraced(function* (exit) {
        if (Exit.isFailure(exit)) {
          yield* Effect.logWarning(exit.cause);
        }
        const now = yield* DateTime.now;
        const next = DateTime.fromDateUnsafe(Cron.next(options.cron, options.calculateNextRunFromPrevious ? request.payload.dateTime : now));
        const client = makeClient(DateTime.formatIso(next));
        return yield* client.run({
          dateTime: next
        }, {
          discard: true
        }).pipe(Effect.tapCause(cause => Effect.logWarning("Failed to schedule next run, retrying", cause)), Effect.sandbox, Effect.retry(retryPolicy), Effect.orDie);
      }), true).pipe(Effect.annotateLogs({
        module: "effect/cluster/ClusterCron",
        name: options.name,
        dateTime: request.payload.dateTime
      }))
    };
  }));
  return Layer.merge(InitialRun, EntityLayer);
};
const retryPolicy = /*#__PURE__*/Schedule.exponential(200, 1.5).pipe(/*#__PURE__*/Schedule.either(/*#__PURE__*/Schedule.spaced("1 minute")));
class CronPayload extends /*#__PURE__*/Schema.Class("effect/cluster/ClusterCron/CronPayload")({
  dateTime: Schema.DateTimeUtc
}) {
  [PrimaryKey.symbol]() {
    return "";
  }
  [DeliverAt.symbol]() {
    return this.dateTime;
  }
}
//# sourceMappingURL=ClusterCron.js.map