52
loading...
This website collects cookies to deliver better user experience
Daily Analytics - your analytics application displays usage metrics broken down by day.
Since you want these analytics available to your users by 8AM local time, you queue up one analytics job for each account every night starting at 12:01AM.
Scheduled Reminders - your online game relies on in-app ads and upgrade notifications to drive revenue, but the logic for deciding which content to trigger for the user is dynamic. So, you queue up all the notification content for each account and in real-time consume and filter the desired content.
Real-Time Events - your financial application receives millions of hits to its API throughout the day. In order to keep your endpoints fast, you queue events immediately after receiving them and process them later.
details
field.Task
:table tasks:
id serial
details text
status text -- One of: queued, executing, completed
operator_id text
expiry timestamp
created timestamp
last_updated timestamp
queued
state when it is written to the table,executing
state when a worker is assigned the task and it begins execution, andcompleted
state when it finishes execution.select
*
from tasks t
where t.status = 'queued' or (t.status = 'executing' and t.expiry <= NOW())
update tasks t
set t.status = 'executing',
t.operator_id = $operator_id,
t.expiry = $expiry,
t.last_updated = NOW()
where t.id = $id and t.last_updated = $last_updated
expiry
value as the current time plus the maximum amount of time it expects a worker to take to execute the task, plus some buffer. If the query returns with 0 rows updated, then it means the Manager did not obtain the lock. Otherwise, if the query returns with a value of 1, it means the Manager successfully locked the task. The Manager can now assign the task to a worker for execution.id
and use the details
to execute the task. You may wish to have your worker update the task row to specify its own operator_id
to aid in debugging once it begins executing the task. Regardless, when the worker completes execution of the task, it updates the row to indicate that it's complete.update tasks t
set t.status = 'complete',
t.operator_id = NULL,
t.expiry = NULL,
t.last_updated = NOW()
where t.id = $id and t.last_updated = $last_updated
tasks
table depending how quickly it grows in size, but broadly speaking, the audit log it provides is very useful for debugging problems.retries
of a task.