https://github.com/straightdave/exticker
Simple Elixir ticker
https://github.com/straightdave/exticker
elixir periodically ticker
Last synced: 4 months ago
JSON representation
Simple Elixir ticker
- Host: GitHub
- URL: https://github.com/straightdave/exticker
- Owner: straightdave
- License: mit
- Created: 2020-12-27T14:58:47.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2020-12-29T07:12:06.000Z (over 5 years ago)
- Last Synced: 2025-10-31T22:06:43.227Z (8 months ago)
- Topics: elixir, periodically, ticker
- Language: Elixir
- Homepage:
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ExTicker
Simple Elixir ticker.
## Basic usage
Defining your periodical worker:
```elixir
defmodule MyWorker do
# Options:
# - interval -> interval (ms), default 1000
# - do -> function to trigger, default :work
use ExTicker, interval: 5000, do: :work
def work() do
# .. do some stuff ...
end
end
```
You create it with:
```elixir
MyWorker.new() # it calls `MyWork.start_link([])` behind.
```
> With this `#start_link(any)` function, *MyWorker* could be started by your supervisor:
>```
>children = [
> {MyWorker, []}
>]
>
>opts = [strategy: :one_for_one, name: MySupervisor]
>Supervisor.start_link(children, opts)
>```
After creation, you can start / stop it with:
```elixir
MyWorker.start()
# begin to do some thing periodically
MyWorker.stop()
# stopped
MyWorker.start()
# resumed ...
```
### Test it in IEx
```
Interactive Elixir (1.11.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> defmodule My do
...(1)> use ExTicker
...(1)> def work() do
...(1)> IO.puts("hello")
...(1)> end
...(1)> end
{:module, My,
<<70, 79, 82, 49, 0, 0, 19, 184, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 1, 204,
0, 0, 0, 49, 9, 69, 108, 105, 120, 105, 114, 46, 77, 121, 8, 95, 95, 105,
110, 102, 111, 95, 95, 10, 97, 116, 116, ...>>, {:work, 0}}
iex(2)> My.new()
{:ok, #PID<0.231.0>}
iex(3)> My.start()
{:update, :running}
hello
hello
hello
hello
hello
hello
iex(4)> My.stop()
{:update, :stopped}
iex(5)>
```
## Installation
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `exticker` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:exticker, "~> 0.1.0"}
]
end
```
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at [https://hexdocs.pm/exticker](https://hexdocs.pm/exticker).