├── .gitignore ├── LICENSE ├── README.md ├── config └── config.exs ├── lib └── exdisque.ex ├── mix.exs ├── mix.lock └── test ├── exdisque_test.exs └── test_helper.exs /.gitignore: -------------------------------------------------------------------------------- 1 | /_build 2 | /deps 3 | erl_crash.dump 4 | *.ez 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Miloš Mošić 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ExDisque 2 | ======== 3 | 4 | [Elixir](http://elixir-lang.org/) client for [Disque](https://github.com/antirez/disque), an in-memory, distributed job queue. 5 | 6 | ### Installation 7 | 8 | Add this to your project's `mix.exs` file: 9 | 10 | ```elixir 11 | {:exdisque, ">= 0.0.1"} 12 | ``` 13 | 14 | ### Usage 15 | 16 | #### Connect to the Disque Server 17 | 18 | ``` elixir 19 | {:ok, client} = ExDisque.start_link 20 | ``` 21 | 22 | #### Add a job to a queue and fetch it back 23 | 24 | ``` elixir 25 | ExDisque.query client, ["ADDJOB", "queue_name", "job_body", "0"] 26 | #=> "DIa88749862374413ece8b8b8156a15466474edecb05a0SQ" 27 | ExDisque.query client, ["GETJOB", "FROM", "queue_name"] 28 | #=> [["queue", "DIa88749862374413ece8b8b8156a15466474edecb05a0SQ", "job_body"]] 29 | ``` 30 | 31 | See more Disque command examples on the [Disque repo](https://github.com/antirez/disque#api). 32 | 33 | ### Dependencies 34 | 35 | Only dependency of ExDisque is [eredis](https://github.com/wooga/eredis), Redis client written in Erlang. 36 | 37 | ### Contributing 38 | 39 | Issues and pull requests welcome. 40 | 41 | ### License 42 | 43 | ExDisque is licensed under the MIT License. 44 | -------------------------------------------------------------------------------- /config/config.exs: -------------------------------------------------------------------------------- 1 | # This file is responsible for configuring your application 2 | # and its dependencies with the aid of the Mix.Config module. 3 | use Mix.Config 4 | 5 | # This configuration is loaded before any dependency and is restricted 6 | # to this project. If another project depends on this project, this 7 | # file won't be loaded nor affect the parent project. For this reason, 8 | # if you want to provide default values for your application for third- 9 | # party users, it should be done in your mix.exs file. 10 | 11 | # Sample configuration: 12 | # 13 | # config :logger, :console, 14 | # level: :info, 15 | # format: "$date $time [$level] $metadata$message\n", 16 | # metadata: [:user_id] 17 | 18 | # It is also possible to import configuration files, relative to this 19 | # directory. For example, you can emulate configuration per environment 20 | # by uncommenting the line below and defining dev.exs, test.exs and such. 21 | # Configuration from the imported file will override the ones defined 22 | # here (which is why it is important to import them last). 23 | # 24 | # import_config "#{Mix.env}.exs" 25 | -------------------------------------------------------------------------------- /lib/exdisque.ex: -------------------------------------------------------------------------------- 1 | defmodule ExDisque do 2 | @moduledoc """ 3 | ExDisque wrapper 4 | """ 5 | 6 | @doc """ 7 | Connects to the Disque server: 8 | 9 | * `start_link` 10 | * `start_link("127.0.0.1", 7711)` 11 | 12 | Returns the `pid` of the connected client. 13 | """ 14 | def start_link( 15 | host \\ "127.0.0.1", 16 | port \\ 7711, 17 | database \\ 0, 18 | password \\ "", 19 | reconnect \\ :no_reconnect 20 | ) do 21 | :eredis.start_link( 22 | String.to_charlist(host), 23 | port, 24 | database, 25 | String.to_charlist(password), 26 | reconnect 27 | ) 28 | end 29 | 30 | @doc """ 31 | Disconnects from the Disque server: 32 | 33 | * `stop client` 34 | 35 | `client` is a `pid` returned from the `start_link`. 36 | """ 37 | def stop(pid) when is_pid(pid) do 38 | :eredis.stop(pid) 39 | end 40 | 41 | @doc """ 42 | Execute a query on the `client` with the given arguments. 43 | 44 | * `query(client, ["ADDJOB", "queue_name", "job_body", "0"])` 45 | * `query(client, ["GETJOB", "FROM", "queue_name"])` 46 | 47 | See more Disque command examples on the [Disque repo](https://github.com/antirez/disque#api). 48 | """ 49 | def query(pid, command) when is_pid(pid) and is_list(command) do 50 | :eredis.q(pid, command) |> elem(1) 51 | end 52 | 53 | @doc """ 54 | Same as above, but provide a timeout. This is particularly useful when 55 | creating a consumer that blocks until a job is received. 56 | 57 | `query(client, ["GETJOB", "FROM", "queue_name"], :infinity)` 58 | ``` 59 | """ 60 | def query(pid, command, timeout) 61 | when is_pid(pid) and is_list(command) and (is_integer(timeout) or timeout == :infinity) do 62 | :eredis.q(pid, command, timeout) |> elem(1) 63 | end 64 | end 65 | -------------------------------------------------------------------------------- /mix.exs: -------------------------------------------------------------------------------- 1 | defmodule ExDisque.Mixfile do 2 | use Mix.Project 3 | 4 | def project do 5 | [ 6 | app: :exdisque, 7 | version: "0.0.1", 8 | elixir: "~> 1.0", 9 | name: "exdisque", 10 | build_embedded: Mix.env() == :prod, 11 | start_permanent: Mix.env() == :prod, 12 | deps: deps(), 13 | package: package(), 14 | description: "Elixir client library for Disque: https://github.com/antirez/disque" 15 | ] 16 | end 17 | 18 | def application do 19 | [] 20 | end 21 | 22 | defp deps do 23 | [{:eredis, "~> 1.0"}] 24 | end 25 | 26 | defp package do 27 | [ 28 | contributors: ["Miloš Mošić"], 29 | licenses: ["MIT"], 30 | links: %{"GitHub" => "https://github.com/mosic/exdisque"} 31 | ] 32 | end 33 | end 34 | -------------------------------------------------------------------------------- /mix.lock: -------------------------------------------------------------------------------- 1 | %{ 2 | "eredis": {:hex, :eredis, "1.0.7", "9b018acf002b9ba5bbd570483bd1076f869e054822f1ecefda2fee25f6c27e38", [:make, :rebar], [], "hexpm"}, 3 | } 4 | -------------------------------------------------------------------------------- /test/exdisque_test.exs: -------------------------------------------------------------------------------- 1 | defmodule ExDisqueTest do 2 | use ExUnit.Case 3 | 4 | test "the truth" do 5 | assert 1 + 1 == 2 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /test/test_helper.exs: -------------------------------------------------------------------------------- 1 | ExUnit.start() 2 | --------------------------------------------------------------------------------