import * as mediaconvert from "@distilled.cloud/aws/mediaconvert";
import { Unowned } from "alchemy/AdoptPolicy";
import { isResolved } from "alchemy/Diff";
import { createPhysicalName } from "alchemy/PhysicalName";
import * as Provider from "alchemy/Provider";
import { Resource } from "alchemy/Resource";
import { createInternalTags, hasAlchemyTags } from "alchemy/Tags";
import * as Effect from "effect/Effect";
import * as Schedule from "effect/Schedule";
import * as Stream from "effect/Stream";
import type { Providers } from "../Providers.ts";
import { readMcTags, syncMcTags } from "./internal.ts";
export interface QueueProps {
/**
* Name of the queue. Must be unique within the account/region and match
* `^[\w-]+
.repos/alchemy-effect/packages/aws/src/MediaConvert/Queue.ts at main · sailfork/t3code-github · Sailfork
. If omitted, a unique name is generated. Changing the name
* replaces the queue.
*/
queueName?: string;
/**
* Optional description of the queue.
*/
description?: string;
/**
* Pricing plan for the queue. `ON_DEMAND` queues bill per transcode minute;
* `RESERVED` queues purchase a reservation (a paid commitment). Changing the
* pricing plan replaces the queue.
* @default "ON_DEMAND"
*/
pricingPlan?: mediaconvert.PricingPlan;
/**
* Reservation plan for a `RESERVED` queue (commitment, renewal type, and the
* number of reserved render units). Ignored for `ON_DEMAND` queues.
*/
reservationPlanSettings?: mediaconvert.ReservationPlanSettings;
/**
* Initial/desired queue status. `ACTIVE` queues process jobs; `PAUSED`
* queues hold submitted jobs until resumed.
* @default "ACTIVE"
*/
status?: mediaconvert.QueueStatus;
/**
* Maximum number of jobs the queue processes concurrently (ON_DEMAND).
*/
concurrentJobs?: number;
/**
* User-defined tags for the queue.
*/
tags?: Record<string, string>;
}
export interface Queue extends Resource<
"AWS.MediaConvert.Queue",
QueueProps,
{
queueName: string;
queueArn: string;
type: string | undefined;
status: string | undefined;
pricingPlan: string | undefined;
},
never,
Providers
> {}
/**
* An AWS Elemental MediaConvert queue — the pool that submitted transcode jobs
* are scheduled against. Every account has a system `Default` on-demand queue;
* create additional queues to isolate workloads or to purchase reserved
* render capacity.
*
* ### Creating a Queue
* **Example:** On-Demand Queue
* ```typescript
* const queue = yield* MediaConvert.Queue("Transcode", {
* description: "Marketing video transcodes",
* });
* ```
*
* **Example:** Paused Queue
* ```typescript
* const queue = yield* MediaConvert.Queue("Transcode", {
* status: "PAUSED",
* tags: { team: "media" },
* });
* ```
*
* ### Reserved Capacity
* **Example:** Reserved Queue with a One-Year Commitment
* ```typescript
* const queue = yield* MediaConvert.Queue("Reserved", {
* pricingPlan: "RESERVED",
* reservationPlanSettings: {
* Commitment: "ONE_YEAR",
* RenewalType: "EXPIRE",
* ReservedSlots: 1,
* },
* });
* ```
*
* @resource
*/
export const Queue = Resource<Queue>("AWS.MediaConvert.Queue");
export const QueueProvider = () =>
Provider.effect(
Queue,
Effect.gen(function* () {
const createName = Effect.fn(function* (id: string, props: QueueProps) {
return props.queueName ?? (yield* createPhysicalName({ id, maxLength: 128 }));
});
const toAttrs = (queue: mediaconvert.Queue & { Name: string }) => ({
queueName: queue.Name,
queueArn: queue.Arn!,
type: queue.Type,
status: queue.Status,
pricingPlan: queue.PricingPlan,
});
/** Get a queue by name; typed not-found → undefined. */
const getQueue = Effect.fn(function* (name: string) {
const response = yield* mediaconvert
.getQueue({ Name: name })
.pipe(Effect.catchTag("NotFoundException", () => Effect.undefined));
return response?.Queue;
});
return {
stables: ["queueName", "queueArn"],
diff: Effect.fn(function* ({ id, olds, news }) {
if (!isResolved(news)) return undefined;
const oldName = yield* createName(id, olds);
const newName = yield* createName(id, news);
// The name is the queue's identity; a change means a new queue.
if (oldName !== newName) return { action: "replace" } as const;
// Pricing plan is immutable once the queue exists.
if ((olds.pricingPlan ?? "ON_DEMAND") !== (news.pricingPlan ?? "ON_DEMAND")) {
return { action: "replace" } as const;
}
}),
read: Effect.fn(function* ({ id, olds, output }) {
const name = output?.queueName ?? (yield* createName(id, olds ?? {}));
const queue = yield* getQueue(name);
if (queue === undefined) return undefined;
const attrs = toAttrs(queue);
const tags = yield* readMcTags(queue.Arn!);
return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs);
}),
reconcile: Effect.fn(function* ({ id, news, output, session }) {
const internalTags = yield* createInternalTags(id);
const desiredTags = { ...internalTags, ...news.tags };
const name = output?.queueName ?? (yield* createName(id, news));
// 1. Observe — cloud state is authoritative; output is an id cache.
let queue = yield* getQueue(name);
// 2. Ensure — create if missing.
if (queue === undefined) {
const created = yield* mediaconvert.createQueue({
Name: name,
Description: news.description,
PricingPlan: news.pricingPlan,
ReservationPlanSettings: news.reservationPlanSettings,
Status: news.status,
ConcurrentJobs: news.concurrentJobs,
Tags: desiredTags,
});
queue = created.Queue!;
} else {
// 3. Sync — MediaConvert's UpdateQueue is a full in-place update of
// the mutable fields. Apply whenever any of them drift.
const statusDrift = (news.status ?? "ACTIVE") !== (queue.Status ?? "ACTIVE");
const descDrift = (news.description ?? undefined) !== (queue.Description ?? undefined);
const concurrencyDrift =
news.concurrentJobs !== undefined && news.concurrentJobs !== queue.ConcurrentJobs;
if (statusDrift || descDrift || concurrencyDrift) {
const updated = yield* mediaconvert.updateQueue({
Name: name,
Description: news.description,
Status: news.status,
ConcurrentJobs: news.concurrentJobs,
ReservationPlanSettings: news.reservationPlanSettings,
});
queue = updated.Queue!;
}
}
// 3b. Sync tags — diff against OBSERVED cloud tags.
yield* syncMcTags(queue.Arn!, desiredTags);
yield* session.note(name);
return toAttrs(queue as mediaconvert.Queue & { Name: string });
}),
delete: Effect.fn(function* ({ output }) {
yield* mediaconvert.deleteQueue({ Name: output.queueName }).pipe(
Effect.catchTag("NotFoundException", () => Effect.void),
// A queue draining in-flight jobs can transiently reject deletion.
Effect.retry({
while: (e) => e._tag === "ConflictException",
schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(10)]),
}),
);
}),
list: () =>
mediaconvert.listQueues.pages({}).pipe(
Stream.runCollect,
Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.Queues ?? [])),
Effect.map((queues) =>
queues
// The built-in `Default` queue is an AWS SYSTEM queue that
// always exists and can never be deleted — keep it out of
// enumeration for account-wide teardown (nuke).
.filter((q) => q.Type !== "SYSTEM")
.map(toAttrs),
),
),
};
}),
);
.repos/alchemy-effect/packages/aws/src/MediaConvert/Queue.ts at main · sailfork/t3code-github · Sailfork
. If omitted, a unique name is generated. Changing the name\n * replaces the queue.\n */\n queueName?: string;\n /**\n * Optional description of the queue.\n */\n description?: string;\n /**\n * Pricing plan for the queue. `ON_DEMAND` queues bill per transcode minute;\n * `RESERVED` queues purchase a reservation (a paid commitment). Changing the\n * pricing plan replaces the queue.\n * @default \"ON_DEMAND\"\n */\n pricingPlan?: mediaconvert.PricingPlan;\n /**\n * Reservation plan for a `RESERVED` queue (commitment, renewal type, and the\n * number of reserved render units). Ignored for `ON_DEMAND` queues.\n */\n reservationPlanSettings?: mediaconvert.ReservationPlanSettings;\n /**\n * Initial/desired queue status. `ACTIVE` queues process jobs; `PAUSED`\n * queues hold submitted jobs until resumed.\n * @default \"ACTIVE\"\n */\n status?: mediaconvert.QueueStatus;\n /**\n * Maximum number of jobs the queue processes concurrently (ON_DEMAND).\n */\n concurrentJobs?: number;\n /**\n * User-defined tags for the queue.\n */\n tags?: Record\u003cstring, string>;\n}\n\nexport interface Queue extends Resource\u003c\n \"AWS.MediaConvert.Queue\",\n QueueProps,\n {\n queueName: string;\n queueArn: string;\n type: string | undefined;\n status: string | undefined;\n pricingPlan: string | undefined;\n },\n never,\n Providers\n> {}\n\n/**\n * An AWS Elemental MediaConvert queue — the pool that submitted transcode jobs\n * are scheduled against. Every account has a system `Default` on-demand queue;\n * create additional queues to isolate workloads or to purchase reserved\n * render capacity.\n *\n * ### Creating a Queue\n * **Example:** On-Demand Queue\n * ```typescript\n * const queue = yield* MediaConvert.Queue(\"Transcode\", {\n * description: \"Marketing video transcodes\",\n * });\n * ```\n *\n * **Example:** Paused Queue\n * ```typescript\n * const queue = yield* MediaConvert.Queue(\"Transcode\", {\n * status: \"PAUSED\",\n * tags: { team: \"media\" },\n * });\n * ```\n *\n * ### Reserved Capacity\n * **Example:** Reserved Queue with a One-Year Commitment\n * ```typescript\n * const queue = yield* MediaConvert.Queue(\"Reserved\", {\n * pricingPlan: \"RESERVED\",\n * reservationPlanSettings: {\n * Commitment: \"ONE_YEAR\",\n * RenewalType: \"EXPIRE\",\n * ReservedSlots: 1,\n * },\n * });\n * ```\n *\n * @resource\n */\nexport const Queue = Resource\u003cQueue>(\"AWS.MediaConvert.Queue\");\n\nexport const QueueProvider = () =>\n Provider.effect(\n Queue,\n Effect.gen(function* () {\n const createName = Effect.fn(function* (id: string, props: QueueProps) {\n return props.queueName ?? (yield* createPhysicalName({ id, maxLength: 128 }));\n });\n\n const toAttrs = (queue: mediaconvert.Queue & { Name: string }) => ({\n queueName: queue.Name,\n queueArn: queue.Arn!,\n type: queue.Type,\n status: queue.Status,\n pricingPlan: queue.PricingPlan,\n });\n\n /** Get a queue by name; typed not-found → undefined. */\n const getQueue = Effect.fn(function* (name: string) {\n const response = yield* mediaconvert\n .getQueue({ Name: name })\n .pipe(Effect.catchTag(\"NotFoundException\", () => Effect.undefined));\n return response?.Queue;\n });\n\n return {\n stables: [\"queueName\", \"queueArn\"],\n\n diff: Effect.fn(function* ({ id, olds, news }) {\n if (!isResolved(news)) return undefined;\n const oldName = yield* createName(id, olds);\n const newName = yield* createName(id, news);\n // The name is the queue's identity; a change means a new queue.\n if (oldName !== newName) return { action: \"replace\" } as const;\n // Pricing plan is immutable once the queue exists.\n if ((olds.pricingPlan ?? \"ON_DEMAND\") !== (news.pricingPlan ?? \"ON_DEMAND\")) {\n return { action: \"replace\" } as const;\n }\n }),\n\n read: Effect.fn(function* ({ id, olds, output }) {\n const name = output?.queueName ?? (yield* createName(id, olds ?? {}));\n const queue = yield* getQueue(name);\n if (queue === undefined) return undefined;\n const attrs = toAttrs(queue);\n const tags = yield* readMcTags(queue.Arn!);\n return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs);\n }),\n\n reconcile: Effect.fn(function* ({ id, news, output, session }) {\n const internalTags = yield* createInternalTags(id);\n const desiredTags = { ...internalTags, ...news.tags };\n const name = output?.queueName ?? (yield* createName(id, news));\n\n // 1. Observe — cloud state is authoritative; output is an id cache.\n let queue = yield* getQueue(name);\n\n // 2. Ensure — create if missing.\n if (queue === undefined) {\n const created = yield* mediaconvert.createQueue({\n Name: name,\n Description: news.description,\n PricingPlan: news.pricingPlan,\n ReservationPlanSettings: news.reservationPlanSettings,\n Status: news.status,\n ConcurrentJobs: news.concurrentJobs,\n Tags: desiredTags,\n });\n queue = created.Queue!;\n } else {\n // 3. Sync — MediaConvert's UpdateQueue is a full in-place update of\n // the mutable fields. Apply whenever any of them drift.\n const statusDrift = (news.status ?? \"ACTIVE\") !== (queue.Status ?? \"ACTIVE\");\n const descDrift = (news.description ?? undefined) !== (queue.Description ?? undefined);\n const concurrencyDrift =\n news.concurrentJobs !== undefined && news.concurrentJobs !== queue.ConcurrentJobs;\n if (statusDrift || descDrift || concurrencyDrift) {\n const updated = yield* mediaconvert.updateQueue({\n Name: name,\n Description: news.description,\n Status: news.status,\n ConcurrentJobs: news.concurrentJobs,\n ReservationPlanSettings: news.reservationPlanSettings,\n });\n queue = updated.Queue!;\n }\n }\n\n // 3b. Sync tags — diff against OBSERVED cloud tags.\n yield* syncMcTags(queue.Arn!, desiredTags);\n\n yield* session.note(name);\n return toAttrs(queue as mediaconvert.Queue & { Name: string });\n }),\n\n delete: Effect.fn(function* ({ output }) {\n yield* mediaconvert.deleteQueue({ Name: output.queueName }).pipe(\n Effect.catchTag(\"NotFoundException\", () => Effect.void),\n // A queue draining in-flight jobs can transiently reject deletion.\n Effect.retry({\n while: (e) => e._tag === \"ConflictException\",\n schedule: Schedule.max([Schedule.fixed(\"3 seconds\"), Schedule.recurs(10)]),\n }),\n );\n }),\n\n list: () =>\n mediaconvert.listQueues.pages({}).pipe(\n Stream.runCollect,\n Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.Queues ?? [])),\n Effect.map((queues) =>\n queues\n // The built-in `Default` queue is an AWS SYSTEM queue that\n // always exists and can never be deleted — keep it out of\n // enumeration for account-wide teardown (nuke).\n .filter((q) => q.Type !== \"SYSTEM\")\n .map(toAttrs),\n ),\n ),\n };\n }),\n );\n","preview":null,"history":null,"version":"589d5553fee77ae01853697113e48b6cddfd1eef"},"/_session?shell=1":null}