{"id":17988549,"url":"https://github.com/heyvito/difo","last_synced_at":"2025-03-25T22:33:53.304Z","repository":{"id":57713629,"uuid":"273541810","full_name":"heyvito/difo","owner":"heyvito","description":"⚙️ Distributed FIFOs. Simple background processing for Clojure","archived":false,"fork":false,"pushed_at":"2020-06-19T16:43:17.000Z","size":33,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-20T22:03:54.139Z","etag":null,"topics":["background-jobs","clojure","distributed-job","jobs"],"latest_commit_sha":null,"homepage":"","language":"Clojure","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/heyvito.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-06-19T16:39:30.000Z","updated_at":"2020-07-03T15:57:32.000Z","dependencies_parsed_at":"2022-09-17T18:51:48.823Z","dependency_job_id":null,"html_url":"https://github.com/heyvito/difo","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heyvito%2Fdifo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heyvito%2Fdifo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heyvito%2Fdifo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heyvito%2Fdifo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/heyvito","download_url":"https://codeload.github.com/heyvito/difo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245556314,"owners_count":20634876,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["background-jobs","clojure","distributed-job","jobs"],"created_at":"2024-10-29T19:11:59.411Z","updated_at":"2025-03-25T22:33:53.021Z","avatar_url":"https://github.com/heyvito.png","language":"Clojure","funding_links":[],"categories":[],"sub_categories":[],"readme":"![Difo](https://user-images.githubusercontent.com/77198/85149541-cbbf4500-b227-11ea-9c93-ecf773516939.png)\n\n![](https://github.com/heyvito/difo/workflows/Test/badge.svg)\n[![cljdoc badge](https://cljdoc.org/badge/difo/difo)](https://cljdoc.org/d/difo/difo/CURRENT)\n[![Clojars Project](https://img.shields.io/clojars/v/difo.svg)](https://clojars.org/difo)\n\n**Difo** implements distributed work queues for background processing.\u003cbr/\u003e\nThink of it as a background task runner heavily inspired by [Sidekiq](https://github.com/mperham/sidekiq/) and [Resque](https://github.com/github/resque).\n\n## Installation\n\nAdd the following dependency to your `project.clj` file:\n\n```clojure\n[difo \"0.1.0\"]\n```\n\n## Documentation\n\nDocumentation is available on [cljdoc](https://cljdoc.org/d/difo/difo/CURRENT)\n\n## Usage\n\nDifo uses [Redis](https://redis.io) as its storage backend, so a Redis instance is required in order to use it.\nThe library is divided into two parts: Client, and Server. Both implementations are present, so only one dependency is required.\n\n### Server\nA Difo process is responsible for spinning up an arbitrary, user-defined number of *Workers*. Each Worker has its own thread, and their function is to wait for *Unit*s to be processed. Each Unit represents a single task to be performed. Each process, by its turn, can watch a list of *Queues*.\n\n#### Configuration\nServer configuration is done through environment variables. The server process looks for three diferent variables, those being:\n\n| Key         | Description |\n|-------------|-------------|\n| `WORKERS`   | An integer representing how many Workers will be started for the server process. Defaults to `10`. |\n| `QUEUES`    | A list of queues to watch, separated by spaces. Defaults to `default`. |\n| `REDIS_URL` | The URL to the Redis server, following the following format: `redis://[[username:]password@]hostname[:port][/database]`. Defaults to `redis://localhost:6379/0`. |\n\n#### Running\n\nIn order to start a Difo server, your application must implement a separate entrypoint responsible for running it. In this entrypoint, one may also want to perform any other configuration required to initialize other libraries or mechanisms in order to process units:\n\n```clojure\n(ns myapplication.core\n    (:require [difo.server :as difo-server]))\n\n(defn -main-difo []\n  (configure-my-application)\n  (difo-server/start))\n```\n\n`difo.server/start` will block the calling thread until the server terminates.\n\n### Client\nIn order to enqueue units, Difo provides a `difo.client` namespace. To use it, the following environment variables must be configured:\n\n| Key         | Description |\n|-------------|-------------|\n| `REDIS_URL` | The URL to the Redis server, following the following format: `redis://[[username:]password@]hostname[:port][/database]`. Defaults to `redis://localhost:6379/0`. |\n\n`REDIS_URL` **must the the same** as used by the server process.\n\nThen, to enqueue an unit:\n\n```clojure\n(ns myapplication.sample\n    (:require [difo.client :as background]))\n    \n(defn very-expensive-calculation [a b]\n  (Thread/sleep 5000)\n  (+ a b))\n  \n(defn do-something []\n  (background/perform very-expensive-calculation 1 2))\n```\n\nThis way, invoking `(do-something)` will cause a new unit to be enqueued for later processing. The client is responsible for serialising all parameters into an [EDN](https://github.com/edn-format/edn) structure, and enqueue it into a queue. By default, `perform` enqueues units to the `default` queue. To specify which queue to enqueue into, use `perform-on`:\n\n```clojure\n(ns myapplication.sample\n    (:require [difo.client :as background]))\n    \n(defn very-expensive-calculation [a b]\n  (Thread/sleep 5000)\n  (+ a b))\n  \n(defn do-something []\n  (background/perform-on :calculations very-expensive-calculation 1 2))\n```\n\n\u003e :warning: **WARNING:** As mentioned, the client side of Difo will serialise arguments to your function. **This means the arguments to your workers must be simple datatypes**, such as numbers, strings, booleans, lists, vectors, and maps (hashes). Complex objects (like Atoms, Refs, DateTime...) will not serialise properly, and **will cause problems.**\n\n## Details to keep in mind when using Difo\n\n### 1. Keep parameters small and simple\nDifo uses Redis to persist information about units to be processed. In order to do so, EDN is used as the serialisation format.\nComplex objects cannot be serialised using EDN. For instance, attempting to serialise an `atom` will result in the following output:\n\n```\n#object[clojure.lang.Atom 0x6548bb7d {:status :ready, :val {:foo :bar}}]\n```\n\nPutting in simpler terms, **do not store state into Units**. In case one needs to provide a database row to a function that will be run in background, provide to it an identifier instead, and look it up within that function.\n\nArguments passed to either `perform` or `perform-on` must be simple types, such as `String`, `Long`, `Double`, `Boolean`, `nil`, `Keyword`, `Vector`, `List`, and `Map`. Difo uses `prn-str` and `clojure.edn/read-string` to serialise/deserialise data, and complex types (such as Atoms, for instance) will not survive the serialize/deserialize round trip.\n\n### 2. Ensure your functions are idempotent and transactional\n[Idempotency](https://en.wikipedia.org/wiki/Idempotence) means that a function enqueued for background processing can safely be executed multiple times. This will be specially important when automatic retries are implemented into Difo. When such feature lands, a unit can be half-executed, and then executed again, until it succeeds. That said, it is important to wrap operations in transactions, and make sure sensitive operations can be rolled-back in case an error happens during runtime.\nIn the future, Difo will ensure units will be executed **at least once**, but not **exactly** once.\n\n## Contributions\n\n...are more than welcome!\nFeel free to fork and open a pull request. Please include tests, and ensure all tests are green!\n\n## TODO\nThe following features will be implemented in the near future:\n\n- [ ] Statistics\n- [ ] Web Dashboard\n- [ ] Automatic Retries\n- [ ] Scheduled Units\n\n## License\n\n```\nMIT License\n\nCopyright (c) 2020 - Victor Gama de Oliveira\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fheyvito%2Fdifo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fheyvito%2Fdifo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fheyvito%2Fdifo/lists"}