-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from mbuhot/lukyanov-params-as-erlang-term
Lukyanov params as erlang term
- Loading branch information
Showing
9 changed files
with
177 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,37 @@ | ||
# 3.1.0 | ||
|
||
Version 3.1.0 adds support for job params as an Elixir/Erlang term. | ||
You can insert any arbitrary Elixir/Erlang term into the queue: | ||
|
||
```elixir | ||
{"SendEmail", "[email protected]", "Welcome!"} | ||
|> MyApp.JobQueue.new() | ||
|> MyApp.Repo.insert() | ||
``` | ||
|
||
You should use the option `:params_type` when defining your queue module: | ||
|
||
```elixir | ||
defmodule MyJobQueue do | ||
use EctoJob.JobQueue, table_name: "jobs", params_type: :binary | ||
# ... | ||
end | ||
``` | ||
|
||
Possible values of the option are: `:map` (default) and `:binary` (for storing Elixir/Erlang terms). | ||
|
||
You should use the same option when setting up the migration: | ||
|
||
```elixir | ||
@ecto_job_version 3 | ||
|
||
def up do | ||
EctoJob.Migrations.Install.up() | ||
EctoJob.Migrations.up("jobs", version: @ecto_job_version, params_type: :binary) | ||
end | ||
``` | ||
|
||
|
||
# 3.0.0 | ||
|
||
Version 3.0 adds support for prioritizing jobs within each job queue. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,7 @@ It is compatible with Postgresql and Mysql with a major difference: | |
Add `:ecto_job` to your `dependencies` | ||
|
||
```elixir | ||
{:ecto_job, "~> 3.0"} | ||
{:ecto_job, "~> 3.1"} | ||
``` | ||
|
||
## Installation | ||
|
@@ -58,6 +58,17 @@ defmodule MyApp.Repo.Migrations.CreateJobQueue do | |
end | ||
``` | ||
|
||
By default, a job holds a map of arbitrary data (which corresponds to a `jsonb` field in the table). | ||
If you want to store an arbitrary Elixir/Erlang term in the job (`bytea` in the table), | ||
you can set up the `params_type` option: | ||
|
||
``` | ||
def up do | ||
EctoJob.Migrations.Install.up() | ||
EctoJob.Migrations.CreateJobTable.up("jobs", version: @ecto_job_version, params_type: :binary) | ||
end | ||
``` | ||
|
||
### Compatibility | ||
|
||
`EctoJob` leverages specific PostgreSQL features, like notification mechanism | ||
|
@@ -91,6 +102,8 @@ defmodule MyApp.Repo.Migrations.UpdateJobQueue do | |
end | ||
``` | ||
|
||
|
||
|
||
Add a module for the queue, mix in `EctoJob.JobQueue`. | ||
This will declare an `Ecto.Schema` to use with the table created in the migration, and a `start_link` function allowing the worker supervision tree to be started conveniently. | ||
|
||
|
@@ -100,6 +113,14 @@ defmodule MyApp.JobQueue do | |
end | ||
``` | ||
|
||
For jobs being Elixir/Erlang terms, you should add the `:params_type` option: | ||
|
||
```elixir | ||
defmodule MyApp.JobQueue do | ||
use EctoJob.JobQueue, table_name: "jobs", params_type: :binary | ||
end | ||
``` | ||
|
||
Add `perform/2` function to the job queue module, this is where jobs from the queue will be dispatched. | ||
|
||
```elixir | ||
|
@@ -143,6 +164,22 @@ A job can be inserted into the Repo directly by constructing a job with the `new | |
|> MyApp.Repo.insert() | ||
``` | ||
|
||
For inserting any arbitrary Elixir/Erlang term: | ||
|
||
```elixir | ||
{"SendEmail", "[email protected]", "Welcome!"} | ||
|> MyApp.JobQueue.new() | ||
|> MyApp.Repo.insert() | ||
``` | ||
|
||
or | ||
|
||
```elixir | ||
|> %MyStruct{} | ||
|> MyApp.JobQueue.new() | ||
|> MyApp.Repo.insert() | ||
``` | ||
|
||
A job can be inserted with optional params: | ||
|
||
- `:schedule` : runs the job at the given `%DateTime{}`. The default value is `DateTime.utc_now()`. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
test/support/migrations/20200603110510_add_job_table_binary.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule EctoJob.Test.Repo.Migrations.AddJobTableBinary do | ||
use Ecto.Migration | ||
alias EctoJob.Migrations.{CreateJobTable, Install} | ||
|
||
@ecto_job_version 3 | ||
|
||
def up do | ||
CreateJobTable.up("jobs_binary", | ||
version: @ecto_job_version, | ||
params_type: :binary | ||
) | ||
end | ||
|
||
def down do | ||
CreateJobTable.down("jobs_binary") | ||
end | ||
end |
Oops, something went wrong.