Skip to content

Sinks and webhooks

A sink is where Hour Advantage sends events: a webhook of yours, or Time and Attendance in the same program. Every event is stored first, in the outbox. Each sink is then sent the events it wants, in the order they were stored, until it takes them. Connect’s events and Time and Attendance’s both leave this way, so one integration pattern serves both.

Source clock, API, posted stored Outbox in stored order a batch Sink 2xx: the next batch fails: same batch, backoff Kept until every sink took it sink.behind then sink.caught_up still waiting after retention_days
A sink that fails is sent the same batch again until it takes it, and other sinks carry on. Nothing a sink has not taken is removed. A sink still behind after retention_days is reported.
Field
idThe sink’s id, as endpoints and sync groups name it. It is 1 to 64 letters, digits, - or _, and not status. It cannot be changed.
typewebhook, or in_process (below).
urlWebhook, required: where events are POSTed, an http or https URL. A tenant’s sink reaches public addresses only, unless the install lists more in tenant_destinations (where a tenant’s setup reaches).
secretWebhook: signs every request (below). In-process: the key the receiver checks. For attendance, it is a delivery key the tenant issued. It is sealed in the database and never shown. A setup file names the variable holding it, secret_env.
receiverIn-process, required: the receiver its events are given to, attendance.
tenantIn-process, required for attendance: the Time and Attendance tenant the events are for. It is not the Connect tenant the sink belongs to. With system, it is the tenant in that system whose punches the sink is sent. The CLI option is hourctl sink add --receiver-tenant.
eventsThe event types it is sent. Absent or empty: the default set.
sourcesSystems whose posted events it is sent, besides the endpoints and sync groups that name it (below).
matchOnly events whose data has one of the given values in each field named are sent: { cause = ["facts"] }.
latest_onlyEvent types that state the latest of something. Of those waiting about the same subject, only the latest is sent.
max_per_secondAt most this many events a second, at least 1. The rest wait their turn. Absent: as fast as it takes them.
batchAt most this many events in one delivery, at least 1. Absent: as many as fit in batch_bytes.
batch_bytesAt most this many bytes of event data in one delivery, at least 1. Absent: 1 MiB (1048576). An event larger than it is sent alone.
systemThe system the sink speaks for (1 to 64 letters, digits, - or _). It is sent only the punches routed to that system. Each event about a person carries person_id, their id in it (below).
Terminal window
hourctl sink add payroll --url https://payroll.example.com/hooks/clock --secret-env SINK_SECRET --event punch --event command.result
hourctl endpoint set office --sink payroll
hourctl sinks # each sink's filter, signing, events waiting, and state

Or in a setup file:

[[sink]]
id = "payroll"
type = "webhook"
url = "https://payroll.example.com/hooks/clock"
secret_env = "HOURADVANTAGE_WEBHOOK_SECRET"
events = ["punch", "command.result"]
system = "payroll"

hourctl sink add and set take --system payroll. With hourctl sink set, --event, --source and --match replace the sink’s lists. --default-events empties events, which gives it the default set.

A sink is fed from three places: the endpoints that name it, the sync groups that name it (for a group’s own events), and the sources it names. A sink nothing feeds is sent nothing.

  • A new sink starts with the events stored from the moment something first feeds it. Earlier ones are sent only if you move it back.
  • A sink that an endpoint takes off its sinks and names again later carries on from where it stopped. While it is off, it is still sent that endpoint’s events stored before the edit.
  • Once the endpoint, sync group or sink is deleted, nothing more is owed to it. A sink made again with the same id starts as a new sink does.
  • An endpoint may name several sinks, or none. Its events are stored either way and can be pulled with GET /api/tenants/{tenant}/events.
  • A sink cannot be removed while an endpoint or a sync group names it.

A sink belongs to a tenant or to the install. A tenant’s sink (/api/tenants/{tenant}/sinks, hourctl --tenant acme sink) is sent its tenant’s events only. An install-level sink (/api/sinks, hourctl --install sink) is sent every tenant’s events, posted ones included. Examples are a data warehouse and an audit feed. Each event’s envelope says whose it is (tenant). Time and Attendance keeps its own events in a log of its own. They are delivered to sinks of its own the same way.

A sink that names no events is sent what an outside system acts on or keeps up to date with. That is:

  • punches
  • people and their changes at the clocks
  • devices coming and going
  • the result of each action
  • a job pausing and ending

Detail is sent only to a sink that names it in events:

Sent only when namedWhy
command.result, command.unanswered, job.started, job.resumed, job.wave, job.cancelled, job.execution.*The steps beneath an action or a job. action.result and job.completed (with its counts) sum them up.
device.options, work_codeAnswers to a request, which the request’s result already carries.
device.log, device.error, biometric.refused, sync.memberOperations detail.
biometric, biometric.deleted, biometric.photo, user.photo, user.extendedBiometric data. A screen that shows “4 fingers and 1 face” reads the person’s credentials from the people API when it opens.
photo, id_cardPersonal data: a photo taken at a punch, an identity card read with its portrait and fingerprints.

A sync group’s changes (person.changed, person.deleted, access.changed, list.changed, setting.changed) are sent by default only when they came from a device. That means someone enrolled or edited at a clock. Changes made through the API (an origin starting api:) are left out, since whoever made them already knows. A sink that names these types is sent all of them, API changes included.

A sink that names its types is sent exactly those.

Beyond the types, you can tell a sink what to pick out of what it is fed, and how fast to take it:

Terminal window
hourctl -t acme sink add hr-feed --url https://warehouse.example.com/hooks/hr \
--source hr --event employee.hired --event device.new \
--match site=north --max-per-second 200 --batch 50 --batch-bytes 262144
  • sources adds the events other systems post into the tenant (hr, erp). It also adds connect, for Connect’s own events about the tenant (package.updated, sink.behind, sink.caught_up, sink.moved). A source’s events go through the sink’s events and match like any others. Time and Attendance’s events go to its own sinks, never to Connect’s.
  • match keeps only events whose data field has one of the given values (--match cause=facts; repeat for more values or fields). Only top-level fields of data are looked at. Text is compared as it is. A number or true/false is compared as it is written in JSON (--match count=2). An event without the field, or with it null, is not sent.
  • latest_only is for events that state the latest of something. While several of the same type about the same subject wait in one delivery, only the newest is sent. The subject is the event’s device at its endpoint in its tenant. For Time and Attendance’s events, the subject is the person in the Time and Attendance tenant their data names. A sink that fell behind catches up on the current state instead of every step on the way.
  • max_per_second paces deliveries to stay under it. The rest wait in the outbox.
  • batch caps how many events go in one delivery, at least 1.
  • batch_bytes caps the size of the event data in one delivery. An event larger than the cap is sent in a delivery of its own, so a sink is never held up by one.

Events a sink is not sent, by type or by these settings, still move its position on. They are skipped, not held.

Batches{"events": [...]}. A batch holds as many events as the sink’s batch and batch_bytes allow. When it sets neither, that is 1 MiB of event data. A batch holds at least one event. The timeout is 10 s (30 s for an in-process receiver).
RetriesThe same batch again, with backoff from 1 s doubling to 60 s, until the sink answers 2xx. Redirects are not followed, and count as a failure.
OrderEach sink is sent events in the order they were stored.
At least onceAn event can arrive twice. After an instance fails, a batch it had in flight can land late. Deduplicate on the event id. Order punches by data.time.
IsolationA sink that is down or slow holds up only itself. A backlog takes its turn like every other sink.
One senderOne instance at a time delivers to a sink. If it stops, another carries on from the sink’s stored position within seconds.

Each request carries content-type: application/json and a houradvantage/<version> user agent. The whole contract, ids and ordering included, is in events.

Every request carries x-houradvantage-timestamp, the time it was sent in Unix seconds. A sink with a secret also gets x-houradvantage-signature: v1=<hex>. This is the HMAC-SHA256, keyed with the secret, of the timestamp, a ., and the request body. Verify it, and reject old timestamps.

Here is a check in Python, from the examples repository (receivers/verify_signature.py, with its test):

import hashlib, hmac, time
def verify(secret, timestamp, signature_header, body):
if abs(time.time() - int(timestamp)) > 300:
return False
expected = hmac.new(secret.encode(), timestamp.encode() + b"." + body, hashlib.sha256).hexdigest()
return hmac.compare_digest("v1=" + expected, signature_header)

body is the request body as received, before it is parsed. You can test your own check with these values. The secret is whsec_example. The timestamp is 1767225600. The body is {"events":[{"id":"evt_1","kind":"punch"}]}. They give v1=1e4a92c84464b347c6b9b5b67039742d40427345d89386fca1bbbad1bf12bbc8.

To change a sink’s secret without a refused delivery, see rotating credentials.

A webhook sink to an https URL trusts two things:

  • The public certificate authorities (CAs) built into the program.
  • The certificates the host trusts. These are the operating system’s store. Where SSL_CERT_FILE (a PEM file) or SSL_CERT_DIR is set in the instance’s environment, those files are used in its place.

The built-in roots stay either way.

A receiver whose certificate a private CA signed is trusted once the CA is installed on each machine an instance runs on. An instance reads the host’s certificates once, the first time it needs them. Restart it after installing a CA.

In a container, mount the CA bundle and name it. The image carries the system’s public CA bundle (ca-certificates). SSL_CERT_FILE replaces it for the host’s side. So the file holds the private CA’s certificate, and any other you need beyond the built-in public roots:

services:
gateway:
environment:
SSL_CERT_FILE: /etc/ssl/private/acme-ca.pem
volumes:
- ./acme-ca.pem:/etc/ssl/private/acme-ca.pem:ro

Hour Advantage knows a person by their clock user id. Your payroll or HR system knows them by its own. A sink that speaks for a system (system = "payroll") is sent the punches routed to that system (to its tenant there, when it names one), and no others.

Every event about a person that it is sent carries a top-level person_id. That is their id in that system. For a punch, it comes from its route. For any other event, it comes from the person’s routes as they are when the event is sent. A sync group’s default route to a system names no person. A person’s id there is their external id in that system.

Events keep user_id either way. A person with no id in that system gets no person_id. The receiver decides what to do. A sink that names no system is sent every punch, with no person_id. Every event also carries Connect’s own ids (tenant, group, person), so a receiver can always tell whose punch it is.

Each sink has a stored position, the sequence number of the last event it has taken. hourctl sink position <sink> latest skips everything it has not taken. A lower number sends events again (moving a sink).

Events are removed after retention_days, whether or not a sink has taken them. The exception is events a sink has yet to take. An event a sink has not taken is never removed for its age. A sink that is down, refusing or paused keeps every event that waits for it, however long, and only those. The storage is its tenant’s own. Other tenants’ events are not kept for it (houradvantage_events_held{log,tenant} counts them).

A sink that falls behind retention is reported with sink.behind, and with sink.caught_up once it has taken them. Moving a sink by hand is reported with sink.moved. Letting go of what a sink has not taken is the operator’s decision, made by moving it ahead (retention).

houradvantage_sink_behind, houradvantage_sink_oldest_waiting_seconds and houradvantage_sink_failing say how each sink is doing (metrics).

A sink of type in_process gives its events to a receiver in the same program instead of posting them. These are the events a webhook would be sent, in the same batches and order, without the network in between. A receiver accepting a batch counts as a 2xx. It takes no url. Its secret is the key the receiver checks.

Terminal window
export KEY=$(hourctl -t acme attend delivery-keys create --label "time sink")
hourctl sink add time --receiver attendance --receiver-tenant acme --secret-env KEY --event punch
[[sink]]
id = "time"
type = "in_process"
receiver = "attendance"
tenant = "acme"
secret_env = "ACME_DELIVERY_KEY"
events = ["punch"]

The one receiver is attendance, the Time and Attendance engine, on instances that run it (HOURADVANTAGE_ATTEND=on). This is how Connect’s punches reach Time and Attendance (a punch’s path).

  • It records punch events and accepts every other type without using it, so name punch in events.
  • The sink must name the Time and Attendance tenant its punches are for (tenant, hourctl sink add --receiver-tenant). It must also carry one of that tenant’s delivery keys as its secret. A delivery for a tenant that does not exist or is suspended, or with a key that is not the tenant’s, is refused. The sink waits as any refused sink does (a refused sink).
  • A punch it cannot read (no time, no user) is kept among the tenant’s unreadable punches, not retried.
  • It knows the person a punch is about by the event’s person_id. A sink that speaks for a system gives it. Otherwise it uses the person_id of the route the punch came by (data.route). Without one, the person is an unlinked person. They are keyed by Connect’s own id of the person in their sync group (hq~0c6f2a1e-..., with the Connect tenant before it unless that is the default one).
  • On an instance that does not run the receiver, deliveries fail and the events wait in the outbox, as they do for a webhook that is down.

Time and Attendance’s own events go out to its own sinks, the tenant’s and the install’s, in the same way. They are exceptions raised and resolved, timecards approved, periods closed and adjusted, and leave requested, decided and cancelled. See events and sinks and events.