amelinium.common.oplog
added in 1.0.0
amelinium service, operation logger.
drain!
(drain! chan)
Drains the channel by getting all elements from it until nothing is left to get.
init!
(init! k {:keys [db table writer reporter buffered-max timeout], :as config})
Initializes operations logging by starting a separate thread listening for log messages on a channel which is shared with a generated reporting function using lexical closure.
The writer (:writer
) should be a function taking the following arguments: id
(will be used to pass a configuration key k
), db
(will be used to pass a database connection (a data source), if set under :db
configuration key), table
(will be set to a table name, if set under the :table
configuration key), messages
(will be set to a queue of messages to be written). If there is no :writer
option given in the configuration map, the initializer will try to find one by using k
with a string -writer
attached (and the converted to a symbol).
The reporter (:reporter
) should be a function taking the following arguments: id
(will be used to pass a configuration key k
), channel
(will be used to pass a channel for sending messages to the listening thread), message
(will be used to pass a message. The responsibility for handling the message is delegated to the writer function, with the exception that a message cannot be nil (such messages should be ignored).
A call of the reporter function is wrapped into anonymous function which closes over initialized k
and channel
values, providing the call with two first arguments. This new function is exposed under :fn/reporter
configuration key associated by the initializer within a returned map.
So, if no :reporter
nor :writer
is set in configuration, and the configuration key is :amelinium.common.oplog.auth/log
then the defaults would be: amelinium.common.oplog.auth/log-reporter
(reporting) and amelinium.common.oplog.auth/log-writer
(writing). Both must be defined.
queue
(queue)
(queue coll)
Creates new FIFO queue. If coll
is given, new queue will be initialized with elements from it.
run!
(run! {:keys [id db table timeout fn/writer-buffered fn/writer channel/kill channel/messages], :as config})
Starts logger thread which waits for new data to be put on queue whenever there are any on communication channel (:channel/messages
key of the config
). If the timeout is exceeded (:timeout
key) or if the queue holds more messages than a number under :buffered-max
configuration key, will run the function passed under the :writer
key of the configuration map to actually insert all of the accumulated messages into a database. The thread will finish its work (after draining the channel and flushing queue) when there is any value received on its kill channel (:channel/kill
).
stop!
(stop! k {:keys [fn/reporter-name channel/kill channel/runner]})
Stops the operations logger by sending a message via the given kill channel (:channel/kill
configuration key) and waiting for the listening thread to finish its work.