Giter Club home page Giter Club logo

conditional_child's Introduction

Tests Contributor Covenant

Conditional Child

A wrapper for starting and stopping a child process in runtime, based on periodic checks.

A common use case is to start and stop processes when feature flags are toggled, but any condition can be used.

Installation

Add conditional_child to your list of dependencies:

def deps do
  [{:conditional_child, "~> 0.1"}]
end

How to use

Suppose you have a static Demo.Worker child in your application supervision tree:

defmodule Demo.Application do
  @moduledoc false

  use Application

  @impl true
  def start(_type, _args) do
    children = [
      Demo.Worker
    ]

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

To make it conditional, just wrap your child definition into a ConditionalChild process, passing start_if and child options, like in the diff below:

-      Demo.Worker
+      {ConditionalChild, child: Demo.Worker, start_if: fn -> your_condition() end}

Becoming:

defmodule Demo.Application do
  @moduledoc false

  use Application

  @impl true
  def start(_type, _args) do
    children = [
      {ConditionalChild, child: Demo.Worker, start_if: fn -> your_condition() end}
    ]

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

During initialization, ConditionalChild will execute start_if and only start the child process if it evaluates to true.

After that, it will execute start_if every second, and start/stop the process based on the result. The check interval can be changed if desired.

For more details, see ConditionalChild.

Code of Conduct

This project Contributor Covenant version 2.1. Check CODE_OF_CONDUCT.md file for more information.

License

conditional_child source code is released under Apache License 2.0.

Check NOTICE and LICENSE files for more information.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.