Docs

Events and triggers

A trigger connects a Worker to an event source in alien.ts. Alien wires the cloud service, such as an SQS event source mapping or a Pub/Sub push subscription, and the Worker runtime hands each event to the handler you registered.

Queue

One message per invocation by default:

alien.ts
const tasks = new alien.Queue("tasks").build()

const processor = new alien.Worker("processor")
  .code({ type: "source", src: "./processor", toolchain: { type: "typescript" } })
  .trigger({ type: "queue", queue: tasks.ref() })
  .permissions("execution")
  .build()
processor/src/index.ts
import { onQueueMessage } from "@alienplatform/sdk"

onQueueMessage<{ imageId: string }>("tasks", async message => {
  console.log(message.payload.imageId, message.attemptCount)
  // resolves: acknowledged. throws: redelivered.
})

Batches

Add batch to receive several messages per invocation:

alien.ts
.trigger({
  type: "queue",
  queue: tasks.ref(),
  batch: { maxMessages: 10, maxWindowSeconds: 5 },
})
processor/src/index.ts
import { onQueueBatch } from "@alienplatform/sdk"

onQueueBatch<{ imageId: string }>("tasks", async batch => {
  return Promise.all(
    batch.deliveries.map(async ({ deliveryId, message }) => {
      try {
        await resize(message.payload.imageId)
        return { deliveryId, disposition: "success" as const }
      } catch {
        return { deliveryId, disposition: "retry" as const }
      }
    }),
  )
})
FieldRangeNotes
maxMessages1 to 100Values above 10 need maxWindowSeconds above 0.
maxWindowSeconds0 to 300How long to collect messages. On AWS, sparse traffic can wait up to 20 seconds even with a shorter window.

Both are upper bounds. A batch can be smaller, and deliveries are unordered and at least once. Return one outcome per deliveryId; a retry returns only that message to the queue.

Storage events in a queue

A Storage with notifications puts an event into a queue for each new object. Read each message with storageEventFromMessage:

ingest/src/index.ts
import { onQueueBatch, storageEventFromMessage } from "@alienplatform/sdk"

onQueueBatch("work", async batch => {
  return Promise.all(
    batch.deliveries.map(async ({ deliveryId, message }) => {
      try {
        const event = await storageEventFromMessage(message)
        await ingest(event.bucketName, event.objectKey)
        return { deliveryId, disposition: "success" as const }
      } catch {
        return { deliveryId, disposition: "retry" as const }
      }
    }),
  )
})

Python and Rust use storage_event_from_message. The runtime acknowledges messages that no rule's suffix matches before they reach the handler.

Storage

alien.ts
const uploads = new alien.Storage("uploads").build()

const thumbnails = new alien.Worker("thumbnails")
  .code({ type: "source", src: "./thumbnails", toolchain: { type: "typescript" } })
  .trigger({ type: "storage", storage: uploads.ref(), events: ["created"] })
  .permissions("execution")
  .build()
thumbnails/src/index.ts
import { onStorageEvent } from "@alienplatform/sdk"

const filter = { prefix: "photos/" }

onStorageEvent("uploads", async event => {
  console.log(event.eventType, event.objectKey) // "created", "photos/cat.jpg"
}, filter)

Python Workers can't handle storage events.

EventAWSGoogle CloudAzure
createds3:ObjectCreated:*OBJECT_FINALIZEBlobCreated
deleteds3:ObjectRemoved:*OBJECT_DELETEBlobDeleted
archivedOBJECT_ARCHIVE
metadataUpdatedOBJECT_METADATA_UPDATE
tierChangedBlobTierChanged

Use created and deleted for portable stacks. Azure refuses other event names.

For batches, a suffix filter, or Python handlers, have the Storage notify a queue and use a queue trigger instead. A Storage with notifications can't also be a storage trigger source.

If the storage is "frozen" and the stack overrides the management permissions Alien uses after initial setup, add storage/trigger-management for that storage to the management profile. Wiring a trigger changes the bucket's notification settings, and the build refuses the trigger without it.

Schedule

alien.ts
const cleanup = new alien.Worker("cleanup")
  .code({ type: "source", src: "./cleanup", toolchain: { type: "typescript" } })
  .trigger({ type: "schedule", cron: "0 3 * * *" }) // 03:00 UTC daily
  .permissions("execution")
  .build()
cleanup/src/index.ts
import { onCronEvent } from "@alienplatform/sdk"

onCronEvent("*", async event => {
  console.log(event.scheduleName, event.timestamp)
})

Python Workers can't handle schedules.

cron is a standard 5-field expression: minute, hour, day of month, month, day of week. Schedules run in UTC.

Several triggers

A Worker can combine one queue trigger with any number of storage and schedule triggers. Each trigger invokes the Worker on its own:

alien.ts
const worker = new alien.Worker("processor")
  .code({ type: "source", src: "./processor", toolchain: { type: "typescript" } })
  .trigger({ type: "queue", queue: tasks.ref() })
  .trigger({ type: "schedule", cron: "0 0 * * *" })
  .permissions("execution")
  .build()

To consume two queues, declare two Workers.

Platforms

TriggerAWSGoogle CloudAzureKubernetesLocal
QueueSQS event source mappingPub/Sub push subscriptionDapr Service Bus binding (KEDA scale rule for batches)Not supportedLocal trigger service
StorageS3 event notificationsCloud Storage notifications through Pub/SubEvent Grid to Service Bus, then a Dapr bindingNot supportedLocal trigger service
ScheduleEventBridge ruleCloud Scheduler jobDapr cron bindingNot supportedLocal trigger service

Per-platform settings such as visibility timeouts and ack deadlines are in Behavior and limits.

Next

On this page