Defines an embedded Ecto schema optimized for Fleet job arguments.
Provides strong typing, automatic runtime casting, and enqueue-time validations using standard Ecto changesets.
Features
- Automatically configures
use Ecto.Schemawith@primary_key false. - Imports
Ecto.Changesetfor building validations. - Generates a default, overridable
changeset/2that casts all schema fields. - Provides
cast_args/1andcast_args!/1helpers for validation and coercion. - Automatically serializes and hydrates typed structs between PostgreSQL and Broadway.
Example (Inline inside a Worker)
defmodule MyApp.Workers.SendInvoice do
use Fleet.Worker, queue: :billing
use Fleet.Schema
embedded_schema do
field :invoice_id, :integer
field :amount, :decimal
field :currency, :string, default: "USD"
end
def changeset(schema, attrs) do
schema
|> cast(attrs, [:invoice_id, :amount, :currency])
|> validate_required([:invoice_id, :amount])
end
@impl Fleet.Worker
def perform(%Fleet.Job{args: %__MODULE__{invoice_id: id, amount: amount, currency: curr}}) do
# `amount` is Decimal, `id` is integer, `curr` defaults to "USD"
MyApp.Billing.charge(id, amount, curr)
end
endExample (Standalone Schema)
defmodule MyApp.Schemas.EmailPayload do
use Fleet.Schema
embedded_schema do
field :to, :string
field :subject, :string
end
end
defmodule MyApp.Workers.SendEmail do
use Fleet.Worker, queue: :mailers, schema: MyApp.Schemas.EmailPayload
@impl Fleet.Worker
def perform(%Fleet.Job{args: %MyApp.Schemas.EmailPayload{to: to, subject: sub}}) do
MyApp.Mailer.send(to, sub)
end
end
Summary
Functions
Returns true if the given module is an Ecto schema or uses Fleet.Schema.
Functions
Returns true if the given module is an Ecto schema or uses Fleet.Schema.