### Rihanna Database Upgrade Migration Source: https://github.com/samsondav/rihanna/blob/master/CHANGELOG.md Example of an Ecto migration file for upgrading Rihanna jobs. ```elixir defmodule MyApp.UpgradeRihannaJobs do use Rihanna.Migration.Upgrade end ``` -------------------------------- ### Define Job Error Callback Source: https://github.com/samsondav/rihanna/blob/master/CHANGELOG.md Example of defining an `after_error` callback for a job to handle failure reasons and arguments. ```elixir def after_error(failure_reason, args) do notify_someone(__MODULE__, failure_reason, args) end ``` -------------------------------- ### Set Default Priority Source: https://github.com/samsondav/rihanna/blob/master/docs/upgrading_v2.md Set the default value for the 'priority' column to 50 after the new code is running. This ensures new jobs or jobs without an explicit priority get a default value. ```sql ALTER TABLE rihanna_jobs ALTER COLUMN priority SET DEFAULT 50; ``` -------------------------------- ### Boot Rihanna Supervisor with Named Config (Without Ecto, Elixir 1.6+) Source: https://github.com/samsondav/rihanna/blob/master/README.md Add Rihanna.Supervisor to your application's children list, specifying a name and providing the Postgrex database configuration. ```elixir children = [ {Rihanna.Supervisor, [name: Rihanna.Supervisor, postgrex: My.Repo.config()]} ] ``` -------------------------------- ### Boot Rihanna Supervisor with Named Config (Without Ecto, Elixir 1.5) Source: https://github.com/samsondav/rihanna/blob/master/README.md Add Rihanna.Supervisor to your application's children list using the supervisor macro, specifying a name and providing the Postgrex database configuration. ```elixir import Supervisor.Spec, warn: false children = [ supervisor(Rihanna.Supervisor, [[name: Rihanna.Supervisor, postgrex: My.Repo.config()]]) ] ``` -------------------------------- ### Boot Rihanna Supervisor with Ecto Repo Config (Elixir 1.6+) Source: https://github.com/samsondav/rihanna/blob/master/README.md Add Rihanna.Supervisor to your application's children list, configuring it to use your Ecto Repo's database configuration. ```elixir children = [ {Rihanna.Supervisor, [postgrex: My.Repo.config()]} ] ``` -------------------------------- ### Create Rihanna Database Source: https://github.com/samsondav/rihanna/blob/master/CONTRIBUTING.md Use this SQL command to create the necessary database for Rihanna to store its jobs. Ensure your PostgreSQL server is running. ```sql -> psql sql (10.5, server 9.6.10) Type "help" for help. (ins)username=# create database rihanna_db; CREATE DATABASE ``` -------------------------------- ### Boot Rihanna Supervisor with Ecto Repo Config (Elixir 1.5) Source: https://github.com/samsondav/rihanna/blob/master/README.md Add Rihanna.Supervisor to your application's children list using the supervisor macro, configuring it with your Ecto Repo's database configuration. ```elixir import Supervisor.Spec, warn: false children = [ supervisor(Rihanna.Supervisor, [[postgrex: My.Repo.config()]]) ] ``` -------------------------------- ### Generate Rihanna Migration SQL Source: https://github.com/samsondav/rihanna/blob/master/CONTRIBUTING.md Run this command within an IEx session to generate the SQL for migrating your database schema. This SQL will create the 'rihanna_jobs' table and its primary key. ```elixir -> iex -S mix Erlang/OTP 21 [erts-10.0.4] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe] Interactive Elixir (1.7.1) - press Ctrl+C to exit (type h() ENTER for help) iex(1)> IO.puts Rihanna.Migration.sql CREATE TABLE rihanna_jobs ( ... more sql ... ALTER TABLE ONLY rihanna_jobs ADD CONSTRAINT rihanna_jobs_pkey PRIMARY KEY (id); :ok ``` -------------------------------- ### Add Rihanna Dependency to mix.exs (Without Ecto) Source: https://github.com/samsondav/rihanna/blob/master/README.md Add the Rihanna dependency to your project's mix.exs file when not using Ecto. ```elixir def deps do [ {:rihanna, ">= 0.0.0"} ] end ``` -------------------------------- ### Enqueue Job with Mod-Fun-Args Source: https://github.com/samsondav/rihanna/blob/master/README.md Use this method for simple, immediate job enqueuing. The job is processed asynchronously. ```elixir # Enqueue job for later execution and return immediately Rihanna.enqueue({MyModule, :my_fun, [arg1, arg2]}) ``` -------------------------------- ### Add Rihanna Dependency to mix.exs (Ecto) Source: https://github.com/samsondav/rihanna/blob/master/README.md Add the Rihanna dependency to your project's mix.exs file when using Ecto. ```elixir def deps do [ {:rihanna, "~> 2.3"} ] end ``` -------------------------------- ### Configure Rihanna Ecto Producer Connection Source: https://github.com/samsondav/rihanna/blob/master/README.md Configure Rihanna to use your existing Ecto Repo for producer database connections. This enables Ecto sandbox usage in tests. ```elixir config :rihanna, producer_postgres_connection: {Ecto, MyApp.Repo} # Use the name of your Repo here ``` -------------------------------- ### Implement Rihanna.Job Behavior Source: https://github.com/samsondav/rihanna/blob/master/README.md Implement this behavior to define custom retry logic and error handling for jobs. The `perform/1` callback is required and receives job arguments as a single list. ```elixir defmodule MyApp.MyJob do @behaviour Rihanna.Job # NOTE: `perform/1` is a required callback. It takes exactly one argument. To # pass multiple arguments, wrap them in a list and destructure in the # function head as in this example def perform([arg1, arg2]) do success? = do_some_work(arg1, arg2) if success? do # job completed successfully :ok else # job execution failed {:error, :failed} end end end ``` -------------------------------- ### Schedule Job at Specific Time Source: https://github.com/samsondav/rihanna/blob/master/CHANGELOG.md Schedule a job to run at a specific `DateTime` using Rihanna. ```elixir Rihanna.schedule( {IO, :puts, ["Hello"]}, at: DateTime.from_naive!(~N[2018-07-01 12:00:00], "Etc/UTC") ) ``` -------------------------------- ### Schedule Job in Relative Time Source: https://github.com/samsondav/rihanna/blob/master/CHANGELOG.md Schedule a job to run in a specified duration using Rihanna. ```elixir Rihanna.schedule({IO, :puts, ["Hello"]}, in: :timer.hours(1)) ``` -------------------------------- ### Fix Rihanna Jobs Index Source: https://github.com/samsondav/rihanna/blob/master/README.md Recreate the `rihanna_jobs_locking_index` to ensure correct sorting with NULL values for `due_at`. ```sql CREATE INDEX CONCURRENTLY rihanna_jobs_locking_index_fixed ON rihanna_jobs (priority ASC, due_at ASC NULLS FIRST, enqueued_at ASC, id ASC); DROP INDEX rihanna_jobs_locking_index; ALTER INDEX rihanna_jobs_locking_index_fixed RENAME TO rihanna_jobs_locking_index; ``` -------------------------------- ### Create Rihanna Jobs Table Migration (Ecto) Source: https://github.com/samsondav/rihanna/blob/master/README.md Define a migration to create the 'rihanna_jobs' table in your database when using Ecto. ```elixir defmodule MyApp.CreateRihannaJobs do use Rihanna.Migration end ``` -------------------------------- ### Add Priority Column and Create Index Source: https://github.com/samsondav/rihanna/blob/master/docs/upgrading_v2.md Directly alter the database table to add a 'priority' column and create a new concurrent index. This is part of the 'Direct SQL upgrade' option. ```sql ALTER TABLE rihanna_jobs ADD COLUMN priority integer; CREATE INDEX CONCURRENTLY rihanna_jobs_locking_index ON rihanna_jobs (priority ASC, due_at ASC NULLS FIRST, enqueued_at ASC, id ASC); ``` -------------------------------- ### Enqueue Custom Job Implementation Source: https://github.com/samsondav/rihanna/blob/master/README.md Enqueue jobs that implement the `Rihanna.Job` behavior. This allows for custom logic defined within the job module. ```elixir # Enqueue job for later execution and return immediately Rihanna.enqueue({MyApp.MyJob, [arg1, arg2]}, opts) ``` -------------------------------- ### Insert Migration Version Source: https://github.com/samsondav/rihanna/blob/master/docs/upgrading_v2.md Manually insert the migration version into the `schema_migrations` table. This is required when performing a direct SQL upgrade to bypass the migration file execution. ```sql INSERT INTO schema_migrations VALUES (YOUR_MIGRATION_VERSION_HERE, now()); ``` -------------------------------- ### Backfill Null Priority Jobs Source: https://github.com/samsondav/rihanna/blob/master/docs/upgrading_v2.md Update existing jobs where the 'priority' column is null to have a default value of 50. This ensures data consistency after the column addition. ```sql UPDATE rihanna_jobs SET priority = 50 WHERE priority IS NULL; ``` -------------------------------- ### Set Priority Column to Not Null Source: https://github.com/samsondav/rihanna/blob/master/docs/upgrading_v2.md Make the 'priority' column a non-nullable column after backfilling any null values. This enforces data integrity for the priority field. ```sql ALTER TABLE rihanna_jobs ALTER COLUMN priority SET NOT NULL; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.