moonlog-mail

    SMTP mail sink for moonbitstack/moonlog.

    moonlog
    log
    mail
    smtp
    moonbit
    Download zip
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    33 minutes ago
    Downloads
    4

    #moonlog-mail

    SMTP mail sink for moonbitstack/moonlog.

    Held

    type Held

    One record waiting to be mailed.

    Mail

    pub struct Mail {
    host : String
    port : Int
    security : Security
    tls :
    Client
    ?
    auth : (String, String)?
    from : String
    to : Array[String]
    subject : String
    level :
    Level

    window : Int64
    at_most : Int
    capacity : Int
    clock : () -> Int64
    held : Array[Held]
    sent : Int64
    dropped : Int64
    failures : Int64
    }

    A sink that mails the records a deployment cannot afford to lose.

    It is the mail handler a service already knows: a level of its own, a window in which a burst becomes one mail, and a ceiling on how many records one mail carries. write only queues — a log call never waits for SMTP — and send does the talking, from the application's own async loop. When the server is down the mail stays queued, and the slot's retries, health machine and alarm take over from there.
    impl Sink for Mail

    Mail::at_most

    fn Mail::at_most(self : Mail) -> Int

    How many records one mail carries.

    Mail::draft

    The mail the pending records would become, without sending it.

    What a check before a deployment prints, and what a test reads instead of standing up a server.

    Mail::dropped

    fn Mail::dropped(self : Mail) -> Int64

    How many records were dropped because the queue was full.

    Mail::due

    fn Mail::due(self : Mail, now : Int64) -> Bool

    Whether a batch is ready at now: the oldest record has waited out the window, or there are enough of them to fill a mail.

    Mail::failures

    fn Mail::failures(self : Mail) -> Int64

    How many sends failed and left their records queued.

    Mail::html_for

    fn Mail::html_for(_self : Mail, batch : Array[Held]) -> String

    The HTML body: the same lines in a table, with the fields that came with them, so an inbox shows the same thing a console does.

    Mail::letter

    The mail a batch becomes, ready to encode.

    Mail::level

    The floor this sink mails at.

    Mail::new

    fn Mail::new(host~ : String, from~ : String, to~ : Array[String], port? : Int, security? : Security, tls? :
    Client
    , auth? : (String, String), subject? : String, level? :
    Level
    , window? : Int64, at_most? : Int, capacity? : Int, clock? : () -> Int64) -> Mail

    A sink mailing through host.

    from is the envelope sender and to the recipients, both as addresses. subject is a template: {count} (a number), {records} (the number and the noun that goes with it), {level} and {target} are filled in from the batch. level is the floor this sink is interested in — it mails what a slot would already have let through, but a sink built beside a console slot gets the whole stream, and this is what keeps a queued warning out of the on-call inbox.

    security decides how the connection is protected; StartTls and Tls need tls, a client with its own trust store, because which roots to trust is the application's decision and not this sink's.

    Mail::pending

    fn Mail::pending(self : Mail) -> Int

    How many records are waiting.

    Mail::send

    async fn Mail::send(self : Mail) -> Result[Int, String]

    Sends one batch, and answers how many records it carried.

    Records that could not be sent stay queued, in order, for the next call; the failure is the answer, so the caller's loop decides when to try again.

    Mail::sent

    fn Mail::sent(self : Mail) -> Int64

    How many records have been mailed.

    Mail::slot

    fn Mail::slot(self : Mail, name? : String) ->
    Slot

    A slot around this sink.

    Mail::subject_for

    fn Mail::subject_for(self : Mail, batch : Array[Held]) -> String

    The subject a batch is mailed under.

    Mail::text_for

    fn Mail::text_for(_self : Mail, batch : Array[Held]) -> String

    The plain text body: one line per record, as the slots wrote them.

    Mail::window

    fn Mail::window(self : Mail) -> Int64

    How long a batch is gathered.

    Mail::write

    fn Mail::write(self : Mail, entry :
    Entry
    ) -> Result[Unit, String]

    Holds the record until the window closes or the queue is full.

    Security

    pub(all) enum Security {
    Clear
    StartTls
    Tls
    } derive(Eq,
    Debug
    )

    How the connection to the mail server is protected.

    Security::equal

    fn Security::equal(Security, Security) -> Bool
    automatically derived

    Security::not_equal

    fn Security::not_equal(x : Security, y : Security) -> Bool
    automatically derived

    Security::port

    fn Security::port(self : Security) -> Int

    The port a security setting usually means: 25 for the clear, 587 for submission with STARTTLS, 465 for implicit TLS. The submission ports are what a relay in front of a service listens on when it is not the local one.

    Security::to_repr

    automatically derived

    at_most_default

    let at_most_default : Int

    How many records one mail carries; a burst larger than this is sent in several mails rather than one too big to read.

    window_default

    let window_default : Int64

    How long a batch of records is gathered before it is mailed.

    Source Files