ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - rubencaro/harakiri: Help applications kill themselves
Help applications kill themselves . Contribute to rubencaro/harakiri development by creating an account on GitHub.
Visit Site

GitHub - rubencaro/harakiri: Help applications kill themselves

GitHub - rubencaro/harakiri: Help applications kill themselves

Harakiri 腹切

Build Status Hex Version Hex Version

Harakiri was concieved to help applications kill themselves in response to a touch to a file on disk. It grew into something scarier.

Given a list of files, an application, and an action. When any of the files change on disk (i.e. a gentle touch is enough), then the given action is fired over the app.

Everything is in an OTP Application so you can have it running in your system to help all your other applications kill themselves.

Actions can be:

  • Any anonymous function.
  • :restart: Restarts the whole VM, runs :init.restart.
  • :stop: Stops, unloads and deletes app's entry from path.
  • :reload: like :stop, then adds given lib_path to path and runs Application.ensure_all_started/1.

Use

First of all, add it to your applications list to ensure it's up before your app starts.

Then add to your deps like this:

{:harakiri, ">= 1.2.0"}

Add an monitor like this:

Harakiri.monitor "/path/to/tmp/file", &MyModule.myfun

Or an action group like this:

Harakiri.add %{paths: ["file1","file2"], action: &MyModule.myfun}

You are done. That would run MyModule.myfun when file1 (or file2) is touched. All given files (file1, file2, etc.) must exist, unless you give the option :create_paths. Then all given paths will be created if they do not already exist.

Whole VM restart

If your app is the main one in the Erlang node, then you may consider a whole :restart:

Harakiri.monitor "/path/to/tmp/restart", :restart

That would restart the VM. I.e. stop every application and start them again. All without stopping the running node, so it's fast enough for most cases. It tipically takes around one second. See init.restart/0.

Anonymous functions

If you need some specific function for your app to be cleanly accessible from outside your VM, then you can pass it as a function. To that function is passed a list with the whole ActionGroup and some info on the actual path that fired the event. Like this:

myfun = fn(data)->
  # check the exact path that fired
  case data[:file][:path] do
    "/path/to/fire/myfun1" -> do_something1
    "/path/to/fire/myfun2" -> do_something2
  end
  # see all the info you have
  data |> inspect |> Logger.info
end

Harakiri.add %{paths: ["/path/to/fire/myfun1","/path/to/fire/myfun2"],
               action: myfun}

This way you can code in pure elixir any complex process you need to perform on a production system. You could perform hot code swaps back and forth between releases of some module, go up&down logging levels, some weird maintenance task, etc. All with a simple touch of the right file.

If you perform an echo instead of a touch, then you could even do something with the contents of the file that fired.

This is quite powerful. Enjoy it.

Shipped actions

The :restart action is suited for a project deployed as the main application in the entire VM. :init.restart will kill all applications and then restart them all again.

The :stop and reload actions are suited for quick operations over a single application, not its dependencies. For instance, :stop unloads and deletes the app's entry from path. No other application is stopped and removed from path.

Harakiri.monitor "file1", :stop

:reload will ensure all dependencies are started before the app as it uses ensure_all_started, but it will not bother adding them to the path. So any dependency that changed will most probably not start because it will be missing from path.

Harakiri.add %{paths: ["file1"],
               action: :reload,
               lib_path: "path"}

lib_path is the path to the folder containing the ebin folder for the current version of the app, usually a link to it. lib_path is only needed by :reload.

Demo

asciicast

TODOs

  • Support for multiple apps on each action set.
  • Support for several actions on each action set.
  • Deeper test, complete deploy/upgrade/reload simulation

Changelog

1.2.0

  • Add monitor for simpler use
  • Remove Elixir 1.5 warnings

1.1.1

  • Remove Elixir 1.4 warnings

1.1.0

  • Add support for async firings
  • Make more noise when given function fails

1.0.2

  • Avoid Elixir 1.3 warnings

1.0.1

  • Do not touch already existing files on start

1.0.0

  • Use it on several projects in production without problems
  • Avoid race conditions with ETS on testing

0.6.0

  • Support for anonymous functions as actions

0.5.1

  • Set initial mtime for created files

0.5.0

  • Support create paths when asked
  • Fix some testing inconsistency

0.4.0

  • Use ETS to preserve state
  • Rearrange using a supervised Task for the main loop and regular helpers to access the ETS table. No need for a GenServer anymore.

0.3.0

  • Allow only one instance of the same action group.

0.2.0

  • First release

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory