Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
54 changes: 54 additions & 0 deletions .github/workflows/lint_and_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Lint and Test

on:
# Manual trigger
workflow_dispatch:
# Automated (push/pull request)
pull_request:
push:
branches:
- master

jobs:
test:
runs-on: ubuntu-20.04
env:
MIX_ENV: test
strategy:
fail-fast: false
matrix:
include:
- pair:
elixir: 1.15.5
otp: 26.0.2
lint: lint
# Deprecated versions (consider removing and upgrading to Ubuntu 22.04)
- pair:
elixir: 1.11
otp: 24
- pair:
elixir: 1.11
otp: 21
steps:
- uses: actions/checkout@v2

- uses: erlef/setup-beam@v1
with:
otp-version: ${{matrix.pair.otp}}
elixir-version: ${{matrix.pair.elixir}}

- name: Install Dependencies
run: mix deps.get --only test

- run: mix format --check-formatted
if: ${{ matrix.lint }}

- run: mix deps.get && mix deps.unlock --check-unused
if: ${{ matrix.lint }}

- run: mix deps.compile

- run: mix compile --warnings-as-errors
if: ${{ matrix.lint }}

- run: mix test
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# v1.3.0

* Maintenance chores (update dependencies, deprecated methods., etc.)
* Update configuration to use `Config` module (instead of Mix)
* Change `Logger.warn` -> `Logger.warning` (former is deprecated)
* Bump minimum Elixir version to v1.11 (per the above)
* Fix documentation formatting typo
* Add CI testing with GitHub Actions

# v1.2.0

* Add `TaskAfter.change_task_after/2` to allow changing a task.

# v1.1.0

* Add `TaskAfter.cancel_task_after/2` to allow cancelling a task.

# v1.0.0

* First published release

# v0.1.0

* Initial release
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Install this package by adding `task_after` to your list of dependencies in `mix
```elixir
def deps do
[
{:task_after, "~> 1.0.0"},
{:task_after, "~> 1.3.0"},
]
end
```
Expand Down
33 changes: 5 additions & 28 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -1,30 +1,7 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config
import Config

# This configuration is loaded before any dependency and is restricted
# to this project. If another project depends on this project, this
# file won't be loaded nor affect the parent project. For this reason,
# if you want to provide default values for your application for
# 3rd-party users, it should be done in your "mix.exs" file.
# Add any global configuration options here.

# You can configure for your application as:
#
# config :task_after, key: :value
#
# And access this configuration in your application as:
#
# Application.get_env(:task_after, :key)
#
# Or configure a 3rd-party app:
#
# config :logger, level: :info
#

# It is also possible to import configuration files, relative to this
# directory. For example, you can emulate configuration per environment
# by uncommenting the line below and defining dev.exs, test.exs and such.
# Configuration from the imported file will override the ones defined
# here (which is why it is important to import them last).

import_config "#{Mix.env}.exs"
# Finally, import env-specific configuration
# (to allow it to override the global defaults above).
import_config "#{config_env()}.exs"
2 changes: 1 addition & 1 deletion config/dev.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use Mix.Config
import Config

config :task_after, global_name: TaskAfter

Expand Down
2 changes: 1 addition & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use Mix.Config
import Config

config :task_after, global_name: TaskAfter

Expand Down
26 changes: 15 additions & 11 deletions lib/task_after.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,17 @@ defmodule TaskAfter do
42

"""
def task_after(timeout_after_ms, callback, opts \\ []) when is_integer(timeout_after_ms) and is_function(callback, 0) do
name = opts[:name] || opts[:pid] || Application.get_env(:task_after, :global_name, nil) || throw "TaskAfter: `:name` not defined and no global name defined"
def task_after(timeout_after_ms, callback, opts \\ [])
when is_integer(timeout_after_ms) and is_function(callback, 0) do
name =
opts[:name] || opts[:pid] || Application.get_env(:task_after, :global_name, nil) ||
throw("TaskAfter: `:name` not defined and no global name defined")

data = %{
timeout_after: timeout_after_ms,
callback: callback,
id: opts[:id],
send_result: opts[:send_result] || :async,
send_result: opts[:send_result] || :async
}

if opts[:no_return] do
Expand All @@ -54,8 +57,6 @@ defmodule TaskAfter do
end
end



@doc """
cancel_task_after

Expand All @@ -80,11 +81,13 @@ defmodule TaskAfter do

"""
def cancel_task_after(task_id, opts \\ []) do
name = opts[:name] || opts[:pid] || Application.get_env(:task_after, :global_name, nil) || throw "TaskAfter: `:name` not defined and no global name defined"
name =
opts[:name] || opts[:pid] || Application.get_env(:task_after, :global_name, nil) ||
throw("TaskAfter: `:name` not defined and no global name defined")

data = %{
id: task_id,
send_result: opts[:run_result],
send_result: opts[:run_result]
}

if opts[:no_return] do
Expand All @@ -94,15 +97,14 @@ defmodule TaskAfter do
end
end


@doc """
change_task_after

task_id -> A task ID
opts -> Can be:

* `name: name` | `pid: pid` -> Specify a non-global task handler, if unspecified that the application `:global_name` must be specified
* `call_timeout: timeout` -> Override the timeout on calling to the TaskAfter.Worker`
* `call_timeout: timeout` -> Override the timeout on calling to the `TaskAfter.Worker`
* `no_return: true` -> Do not return the id or error, just try to register and forget results otherwise
* `callback: fun` -> Change the callback to this function
* `timeout_after_ms: timeout` -> Change the timeout to this new value
Expand All @@ -128,14 +130,16 @@ defmodule TaskAfter do
:no_message
"""
def change_task_after(task_id, opts \\ []) do
name = opts[:name] || opts[:pid] || Application.get_env(:task_after, :global_name, nil) || throw "TaskAfter: `:name` not defined and no global name defined"
name =
opts[:name] || opts[:pid] || Application.get_env(:task_after, :global_name, nil) ||
throw("TaskAfter: `:name` not defined and no global name defined")

data = %{
id: task_id,
callback: opts[:callback],
timeout_after: opts[:timeout_after_ms],
send_result: opts[:send_result],
recreate: opts[:recreate_if_necessary],
recreate: opts[:recreate_if_necessary]
}

if opts[:no_return] do
Expand Down
20 changes: 10 additions & 10 deletions lib/task_after/application.ex
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
defmodule TaskAfter.Application do
# See http://elixir-lang.org/docs/stable/elixir/Application.html
# for more information on OTP Applications

@moduledoc false

use Application

def start(_type, _args) do
import Supervisor.Spec, warn: false

worker =
children =
case Application.get_env(:task_after, :global_name, nil) do
nil -> []
name when is_atom(name) or is_tuple(name) -> [worker(TaskAfter.Worker, [[name: name]])]
end
nil ->
[]

children =
worker
name when is_atom(name) or is_tuple(name) ->
[
{TaskAfter.Worker, [name: name]}
]
end

opts = [strategy: :one_for_one, name: TaskAfter.Supervisor]
Supervisor.start_link(children, opts)
Supervisor.start_link(children, strategy: :one_for_one, name: TaskAfter.Supervisor)
end
end
Loading