teardown

Build composable components for your application with clear teardown semantics

https://github.com/roman/Haskell-teardown#readme

Version on this page:0.3.0.0
LTS Haskell 22.13:0.5.0.1
Stackage Nightly 2024-03-14:0.5.0.1
Latest on Hackage:0.5.0.1

See all snapshots teardown appears in

MIT licensed by Roman Gonzalez
Maintained by [email protected]
This version can be pinned in stack with:teardown-0.3.0.0@sha256:426db411eaa53c641ea62d2f76f863d9925b0c1db48d657dac94b9fc8eec8de3,3262

Module documentation for 0.3.0.0

Hackage Stackage LTS Stackage Nightly

🗑️ teardown

Composable, idempotent & transparent application resource cleanup sub-routines

Table Of Contents

Raison d’etre

The correct teardown of a system becomes a crucial matter when running applications through GHCi while doing REPL driven development; this library provides a stable API to manage the cleanup process of resources your application allocates when it starts up.

One could naively implement a teardown sub-routine of an application by doing something like the following:

-- All functions in this example initialize hypothetical resources, the
-- idea stands that there is a way to allocate a system resource
-- using some sort of configuration record, and there is a
-- sub-routine to release those resources once the application
-- shuts down

initDb :: Logger -> DbConnInfo -> IO (DbConn, IO ())
initDb logger connInfo = do
  conn <- newConn connInfo
  return (conn, info logger "Teardown Database" >> closeConn conn)

initTcpServer :: Logger -> ServerInfo -> IO (Socket, IO ())
initTcpServer logger serverInfo = do
  socket <- startServer serverInfo
  return (socket, info logger "Teardown Tcp Server" >> closeSocket socket)

initApp :: Logger -> DbConnInfo -> ServerInfo -> IO (IO ())
initApp logger connInfo serverInfo = do
  (connInfo, teardownDb) <- initDb logger connInfo
  (serverInfo, teardownSocket) <- initTcpServer logger serverInfo
  -- do something with connInfo and serverInfo ...
  return (info logger "Teardown Application"
          >> teardownDb
          >> teardownSocket)

The previous implementation does not address a few concerns:

  • If for some reason we execute the @IO ()@ sub-routine returned by the @initApp@ function more than once, there is likely going to be a runtime exception of the “already closed resource” nature. This library ensures that teardown sub-routines are executed /exactly/ once, even on the scenario where we execute the teardown procedure multiple times.

  • The teardown of sub-systems can be built and composed via the @(>>)@ operator, what happens if the @teardownDb@ sub-routine in the previous example throws an exception? Likely other resource teardown sub-routines are going to be affected. This library ensures that errors are isolated from every other resource teardown sub-routines.

  • All teardown sub-routines use a description argument to keep track of what is being cleaned up; By requiring this, we avoid confusion around what is going on when shutting down an application. This library makes this documentation a /required/ argument when building teardown sub-routines, thus helping trace-ability.

  • You may notice the structure of teardown sub-routines form a tree shape. This library provides a data structure representation of this tree that allows the developer to report all teardown sub-routines in hierarchy order, with other details around if sub-routines failed (or not).

  • Also, this library keeps track how much time every teardown sub-routine takes, allowing the developer to learn which parts of the teardown procedure are slow and adequately address those on development time (e.g., Faster reload => Faster development feedback loops).

By using this library, you may implement without much effort a good, reliable and transparent strategy for application resource teardown sub-routines.

Documentation

To learn more about the library, please refer to the documentation in Hackage for

Development

Build Status Hackage Dependencies Github

This library is intended to be minimal, providing a few functions that work reliably among many different kind of projects. If you want to contribute, Pull Request are very welcome! Please try to follow these simple rules:

  • Please create a topic branch for every separate change you make.
  • Update the README.md file if necessary.
  • Please do not change the version number on your Pull Request.

Open Commit Bit

This project has an open commit bit policy: Anyone with an accepted pull request gets added as a repository collaborator. Please try to follow these simple rules:

  • Commit directly onto the master branch only for typos, improvements to the README and documentation.
  • Create a feature branch and open a pull-request early for any new features to get feedback.
  • Make sure you adhere to the general pull request rules above.

License

Copyright (c) 2027, Roman Gonzalez

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Changes

Change log

teardown uses Semantic Versioning. The change log is available on GitHub.

v0.3.0.0

  • Bump from lts-9.1 to lts-9.5
  • Add Control.Monad.Component module
  • Add Control.Teardown.Tutorial module
  • Add IResource instance for [Teardown]

v0.2.0.0

  • Bump from lts-8.21 to lts-9.1
  • Re-organize test files to support nightly (GHC-8.2)
  • Drop support for lts-6 (GHC-7.10)
  • Bump dependencies for time, QuickCheck, protolude and doctest
  • Add NFData instance for TeardownResult record
  • Add travisCI builder for nightly

v0.1.0.1

  • Add benchmark to compare with vanilla IO unit
  • Bump version of criterion to 1.2

v0.1.0.0

BREAKING CHANGES

  • Relax Glob dependency bounds
  • Add IResource typeclass and make newTeardown part of it
  • Remove concatTeardown and newDynTeardown functions in favor of overloades of IResource
  • Update TestSuite
  • Update Example

v0.0.0.2

  • Add haddock documentation to modules

v0.0.0.1

  • First release of teardown library