{"id":21100868,"url":"https://github.com/coingaming/coney","last_synced_at":"2025-04-05T17:06:45.345Z","repository":{"id":49063545,"uuid":"87182008","full_name":"coingaming/coney","owner":"coingaming","description":"Consumer server for RabbitMQ with message publishing functionality.","archived":false,"fork":false,"pushed_at":"2025-01-03T13:14:48.000Z","size":156,"stargazers_count":42,"open_issues_count":1,"forks_count":9,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-29T16:07:03.788Z","etag":null,"topics":["amqp","consumer-server","elixir","rabbitmq"],"latest_commit_sha":null,"homepage":"","language":"Elixir","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/coingaming.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-04-04T12:08:11.000Z","updated_at":"2025-01-03T12:59:49.000Z","dependencies_parsed_at":"2025-02-03T17:49:33.262Z","dependency_job_id":"da2a06a3-da5c-4147-a300-7c37ecb8172c","html_url":"https://github.com/coingaming/coney","commit_stats":null,"previous_names":["llxff/coney"],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coingaming%2Fconey","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coingaming%2Fconey/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coingaming%2Fconey/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coingaming%2Fconey/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/coingaming","download_url":"https://codeload.github.com/coingaming/coney/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247369952,"owners_count":20927928,"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":["amqp","consumer-server","elixir","rabbitmq"],"created_at":"2024-11-19T23:34:31.817Z","updated_at":"2025-04-05T17:06:45.326Z","avatar_url":"https://github.com/coingaming.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Coney\n\n[![Hex.pm Version](https://img.shields.io/hexpm/v/coney)](https://hex.pm/packages/coney)\n![Build Status](https://github.com/coingaming/coney/actions/workflows/ci.yaml/badge.svg)\n\nConsumer server for RabbitMQ with message publishing functionality.\n\n## Table of Contents\n\n- [Coney](#Coney)\n  - [Table of Contents](#Table-of-Contents)\n  - [Installation](#Installation)\n  - [Setup a consumer server](#Setup-a-consumer-server)\n  - [Configure consumers](#Configure-consumers)\n    - [Rescuing exceptions](#Rescuing-exceptions)\n      - [error_happened/3](#errorhappened3)\n      - [error_happened/4](#errorhappened4)\n    - [.process/2 and .error_happened return format](#process2-and-errorhappened-return-format)\n    - [Reply description](#Reply-description)\n    - [The default exchange](#The-default-exchange)\n  - [Publish message](#Publish-message)\n  - [Publish message asynchronously](#Publish-message-asynchronously)\n  - [Checking connections](#Checking-connections)\n  - [Contributing](#Contributing)\n  - [License](#License)\n\n## Installation\n\nAdd Coney as a dependency in your `mix.exs` file.\n\n```elixir\ndef deps do\n  [{:coney, \"~\u003e 3.0\"}]\nend\n```\n\nAfter you are done, run `mix deps.get` in your shell to fetch and compile Coney.\n\n## Setup a consumer server\n\nDefault config:\n\n```elixir\n# config/config.exs\nconfig :coney,\n  auto_start: true,\n  settings: %{\n    url: \"amqp://guest:guest@localhost\", # or [\"amqp://guest:guest@localhost\", \"amqp://guest:guest@other_host\"]\n    timeout: 1000\n  }\n```\n\nIf you need to create exchanges or queues before starting the consumer, you can define your RabbitMQ topology as follows:\n```elixir\n  config :coney,\n    topology: %{\n      exchanges: [{:topic, \"my_exchange\", durable: true}],\n      queues: %{\n        \"my_queue\" =\u003e %{\n          options: [\n            durable: true,\n            arguments: [\n              {\"x-dead-letter-exchange\", :longstr, \"dlx_exchange\"},\n              {\"x-message-ttl\", :signedint, 60000}\n            ]\n          ],\n          bindings: [\n            [exchange: \"my_exchange\", options: [routing_key: \"my_queue\"]]\n          ]\n        }\n      }\n    }\n```\n\nAlso, you can create a confuguration module (if you want to retreive settings from Consul or something else):\n\n```elixir\n# config/config.exs\nconfig :coney,\n  auto_start: true,\n  settings: RabbitConfig,\n  topology: RabbitConfig\n```\n\n```elixir\ndefmodule RabbitConfig do\n  def settings do\n    %{\n      url: \"amqp://guest:guest@localhost\",\n      timeout: 1000\n    }\n  end\n\n  def topology do\n    %{\n      exchanges: [{:topic, \"my_exchange\", durable: true}],\n      queues: %{\n        \"my_queue\" =\u003e %{\n          options: [\n            durable: true,\n            arguments: [\n              {\"x-dead-letter-exchange\", :longstr, \"exchange\"},\n              {\"x-message-ttl\", :signedint, 60000}\n            ]\n          ],\n          bindings: [\n            [exchange: \"my_exchange\", options: [routing_key: \"my_queue\"]]\n          ]\n        }\n      }\n    }\n  end\nend\n```\n\nIf you don't want to automatically start Coney and want to control it's start, you can set `auto_start` to `false` and add Coney supervisor into yours:\n\n\n```elixir\n# config/config.exs\nconfig :coney, auto_start: false\n```\n\n```elixir\n\ndefmodule YourApplication do\n  use Application\n\n  def start(_type, _args) do\n    Supervisor.start_link([Coney.ApplicationSupervisor], [strategy: :one_for_one])\n  end\nend\n```\n\nIf you want to disable Coney altogether (useful for testing config) set `enabled: false`\n```elixir\n# config/config.exs\nconfig :coney, enabled: false, settings: %{}, topology: %{}\n```\n\n## Configure consumers\n\n```elixir\n# config/queues.exs\n\nconfig :coney,\n  workers: [\n    MyApplication.MyConsumer\n  ]\n# also you can define mapping like this and skip it in consumer module:\n  workers: [\n    %{\n      connection: %{\n        prefetch_count: 10,\n        queue: \"my_queue\"\n      },\n      worker: MyApplication.MyConsumer\n    }\n  ]\n```\n\n```elixir\n# web/consumers/my_consumer.ex\n\ndefmodule MyApplication.MyConsumer do\n  @behaviour Coney.Consumer\n\n  def connection do\n    %{\n      prefetch_count: 10,\n      queue: \"my_queue\",\n      consumer_tag: \"MyApp - MyConsumer\" # optional\n    }\n  end\n\n  def parse(payload, _meta) do\n    String.to_integer(payload)\n  end\n\n  def process(number, _meta) do\n    if number \u003c= 10 do\n      :ok\n    else\n      :reject\n    end\n  end\n\n  # Be careful here, if call of `error_happened` will raise an exception,\n  # message will be not handled properly and may be left unacked in a queue\n  def error_happened(exception, payload, _meta) do\n    IO.puts \"Exception raised with #{ payload }\"\n    :redeliver\n  end\nend\n```\n\n### Rescuing exceptions\n\nIf exception was happened during calls of `parse` or `process` functions, by default Coney will reject this message. If you want to add additional functionality in order to handle exception in a special manner, you can implement one of `error_happened/3` or `error_happened/4` callbacks. But be careful, if call of `error_happened` will raise an exception, message will be not handled properly and may be left unacked in a queue.\n\n#### error_happened/3\n\nThis callback receives `exception`, original `payload` and `meta` as parameters. Response format is the same as in [process callback](#process2-and-error_happened-return-format).\n\n#### error_happened/4\n\nThis callback receives `exception`, `stacktrace`, original `payload` and `meta` as parameters. Response format is the same as in [process callback](#process2-and-error_happened-return-format).\n\n### .process/2 and .error_happened return format\n\n1. `:ok` - ack message.\n1. `:reject` - reject message.\n1. `:redeliver` - return message to the queue.\n1. `{:reply, binary}` - response will be published to reply exchange.\n\n### Reply description\n\nTo use `{:reply, binary}` you should add response exchange in `connection`:\n\n```elixir\n# web/consumers/my_consumer.ex\n\ndef connection do\n  %{\n    # ...\n    respond_to: \"response_exchange\"\n  }\nend\n```\n\nResponse will be published to `\"response_exchange\"` exchange.\n\n### The default exchange\n\nTo use the default exchange you may declare exchange as `:default`:\n\n```elixir\n%{\n    exchanges: [:default],\n}\n```\nThe following format is also acceptable:\n\n```elixir\n%{\n  exchanges: [{:direct, \"\"}]\n}\n```\n\nOr you can just skip it in the `exchanges` settings and setup the queue in the consumer's settings:\n\n```elixir\n\n%{\n  prefetch_count: 10,\n  queue: \"my_queue\"\n}\n\n```\n\n## Publish message\n\n```elixir\nConey.publish(\"exchange\", \"message\")\n\n# or\n\nConey.publish(\"exchange\", \"routing_key\", \"message\")\n```\n\n## Publish message asynchronously\n\n```elixir\nConey.publish_async(\"exchange\", \"message\")\n\n# or\n\nConey.publish_async(\"exchange\", \"routing_key\", \"message\")\n```\n\n## Checking connections\n\nYou can use`Coney.status/0` if you need to get information about RabbitMQ connections:\n\n```\niex\u003e Coney.status()\n[{#PID\u003c0.972.0\u003e, :connected}]\n```\n\nResult is a list of tuples, where first element in tuple is a pid of running connection server and second element describes connection status.\n\nConnection status can be:\n\n- `:pending` - when coney just started\n- `:connected` - when RabbitMQ connection has been established and all consumers have been started\n- `:disconnected` - when coney lost connection to RabbitMQ\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/coingaming/coney.\n\n### Running test suite locally\n1. Start the RabbitMQ instance via `docker compose up`.\n2. Run `mix test`.\n\n## Architecture\n```mermaid\n  graph TD;\n      A[ApplicationSupervisor - Supervisor] --\u003e B[ConsumerSupervisor - Supervisor];\n      A --\u003e C[ConnectionServer - GenServer];\n      B -- supervises many --\u003e D[ConsumerServer - GenServer];\n      D -- monitors --\u003e E[ConsumerExecutor];\n      E -- sends messages to --\u003e C;\n      D -- opens AMQP conns via --\u003e C;\n```\n\n## License\n\nThe library is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoingaming%2Fconey","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcoingaming%2Fconey","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoingaming%2Fconey/lists"}