Function runtimeRestarter

  • A helper function that checks the metadata version / runtime version in intervals. If a change is detected, an appropriate callback is executed.

    Parameters

    • opts: {
          checkIntervalSeconds?: number;
          log?: ((msg) => void);
          metadata?: {
              getMetadataVersion: (() => Promise<string>);
              onMetadataChange: (() => void | Promise<void>);
          };
          runtime?: {
              getRuntimeVersionHash: (() => Promise<string>);
              onRuntimeChange: (() => void | Promise<void>);
          };
      }
      • Optional checkIntervalSeconds?: number

        How often to check for changes. 10 minutes by default.

      • Optional log?: ((msg) => void)

        Optional function to log the output.

          • (msg): void
          • Parameters

            • msg: string

            Returns void

      • Optional metadata?: {
            getMetadataVersion: (() => Promise<string>);
            onMetadataChange: (() => void | Promise<void>);
        }

        Options to restart upon metadata version change.

        • getMetadataVersion: (() => Promise<string>)
            • (): Promise<string>
            • Returns Promise<string>

        • onMetadataChange: (() => void | Promise<void>)
            • (): void | Promise<void>
            • Returns void | Promise<void>

      • Optional runtime?: {
            getRuntimeVersionHash: (() => Promise<string>);
            onRuntimeChange: (() => void | Promise<void>);
        }

        Options to restart upon runtime hash change.

        • getRuntimeVersionHash: (() => Promise<string>)
            • (): Promise<string>
            • Returns Promise<string>

        • onRuntimeChange: (() => void | Promise<void>)
            • (): void | Promise<void>
            • Returns void | Promise<void>

    Returns (() => void)

    A function to stop and cleanup the restarter.

      • (): void
      • Returns void

    Example

    runtimeRestarter({
    metadata: {
    getMetadataVersion: async () => (await api.rpc.state.getMetadata()).version.toString(),
    onMetadataChange: () => process.exit(1),
    },
    runtime: {
    getRuntimeVersionHash: async () => (await api.rpc.state.getRuntimeVersion()).hash.toString(),
    onRuntimeChange: () => process.exit(1),
    }
    log: (msg) => console.log(msg)
    })