Skip to content
This repository has been archived by the owner on Jun 11, 2023. It is now read-only.

Commit

Permalink
Merge f275ee1 into 2de2128
Browse files Browse the repository at this point in the history
  • Loading branch information
renatomassaro authored Nov 7, 2017
2 parents 2de2128 + f275ee1 commit c8d9b54
Show file tree
Hide file tree
Showing 127 changed files with 5,823 additions and 3,712 deletions.
2 changes: 1 addition & 1 deletion lib/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ defmodule Helix.Application do

def start(_type, _args) do
children = [
# worker(HELF.Broker, []),
supervisor(Helix.Endpoint, []),
supervisor(Helix.Application.DomainsSupervisor, [])
]
Expand Down Expand Up @@ -54,6 +53,7 @@ defmodule Helix.Application.DomainsSupervisor do
supervisor(Helix.Account.Supervisor, []),
supervisor(Helix.Cache.Supervisor, []),
supervisor(Helix.Core.Supervisor, []),
supervisor(Helix.Event.Supervisor, []),
supervisor(Helix.Entity.Supervisor, []),
supervisor(Helix.Hardware.Supervisor, []),
supervisor(Helix.Log.Supervisor, []),
Expand Down
16 changes: 16 additions & 0 deletions lib/event/dispatcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,22 @@ defmodule Helix.Event.Dispatcher do
# All
event ProcessEvent.Process.Created
event ProcessEvent.Process.Completed
event ProcessEvent.Process.Signaled
event ProcessEvent.TOP.BringMeToLife
event ProcessEvent.TOP.Recalcado

# Custom handlers
event ProcessEvent.Process.Created,
ProcessHandler.TOP,
:recalque_handler

event ProcessEvent.TOP.BringMeToLife,
ProcessHandler.TOP,
:wake_me_up

event ProcessEvent.Process.Signaled,
ProcessHandler.Process,
:signal_handler

##############################################################################
# Server events
Expand Down
13 changes: 13 additions & 0 deletions lib/event/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Helix.Event do

alias Helix.Event.Dispatcher, as: HelixDispatcher
alias Helix.Event.Meta, as: EventMeta
alias Helix.Event.State.Timer, as: EventTimer
alias Helix.Process.Model.Process

@type t :: HELF.Event.t
Expand Down Expand Up @@ -85,6 +86,18 @@ defmodule Helix.Event do
def emit(event),
do: HelixDispatcher.emit(event)

@spec emit_after([t] | t, interval :: float | non_neg_integer) ::
term
@doc """
Emits the given event(s) after `interval` milliseconds have passed.
"""
def emit_after([], _),
do: :noop
def emit_after(events = [_|_], interval),
do: Enum.each(events, &(emit_after(&1, interval)))
def emit_after(event, interval),
do: EventTimer.emit_after(event, interval)

@spec inherit(t, t) ::
t
docp """
Expand Down
20 changes: 20 additions & 0 deletions lib/event/state/supervisor.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule Helix.Event.State.Supervisor do

use Supervisor

alias Helix.Event.State.Timer, as: EventTimer

@doc false
def start_link do
Supervisor.start_link(__MODULE__, [])
end

@doc false
def init(_) do
children = [
worker(EventTimer, [])
]

supervise(children, strategy: :one_for_one)
end
end
44 changes: 44 additions & 0 deletions lib/event/state/timer.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
defmodule Helix.Event.State.Timer do
@moduledoc """
`EventTimer` is responsible for handling events that were asked to be emitted
sometime in the future.
"""

use GenServer

alias Helix.Event

@registry_name :event_timer

# Client API

def start_link,
do: GenServer.start_link(__MODULE__, [], name: @registry_name)

@spec emit_after(Event.t, interval :: float | non_neg_integer) ::
term
@doc """
Emits `event` after `interval` milliseconds have passed.
Unit is in milliseconds!
"""
def emit_after(event, interval),
do: GenServer.call(@registry_name, {:emit_after, event, interval})

# Callbacks

def init(_),
do: {:ok, []}

def handle_call({:emit_after, event, interval}, _from, state) do
Process.send_after(@registry_name, {:emit, event}, interval)

{:reply, :ok, state}
end

def handle_info({:emit, event}, state) do
Event.emit(event)

{:noreply, state}
end
end
20 changes: 20 additions & 0 deletions lib/event/supervisor.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule Helix.Event.Supervisor do

use Supervisor

alias Helix.Event.State.Supervisor, as: StateSupervisor

@doc false
def start_link do
Supervisor.start_link(__MODULE__, [])
end

@doc false
def init(_) do
children = [
supervisor(StateSupervisor, [])
]

supervise(children, strategy: :one_for_one)
end
end
18 changes: 12 additions & 6 deletions lib/hardware/internal/motherboard.ex
Original file line number Diff line number Diff line change
Expand Up @@ -102,26 +102,26 @@ defmodule Helix.Hardware.Internal.Motherboard do
|> get_hdds_from_ids()
end

defp get_cpus_from_ids(components) do
def get_cpus_from_ids(components) do
components
|> Component.CPU.Query.from_components_ids()
|> Repo.all()
end

defp get_rams_from_ids(components) do
def get_rams_from_ids(components) do
components
|> Component.RAM.Query.from_components_ids()
|> Repo.all()
end

defp get_nics_from_ids(components) do
def get_nics_from_ids(components) do
components
|> Component.NIC.Query.from_components_ids()
|> Component.NIC.Query.inner_join_network_connection()
|> Repo.all()
end

defp get_hdds_from_ids(components) do
def get_hdds_from_ids(components) do
components
|> Component.HDD.Query.from_components_ids()
|> Repo.all()
Expand All @@ -132,7 +132,13 @@ defmodule Helix.Hardware.Internal.Motherboard do
cpu: non_neg_integer,
ram: non_neg_integer,
hdd: non_neg_integer,
net: %{String.t => %{uplink: non_neg_integer, downlink: non_neg_integer}}
net: %{
Network.id =>
%{
uplink: non_neg_integer,
downlink: non_neg_integer
}
}
}
def resources(motherboard) do
components_ids = get_components_ids(motherboard)
Expand All @@ -155,7 +161,7 @@ defmodule Helix.Hardware.Internal.Motherboard do
components_ids
|> get_nics_from_ids()
|> Enum.reduce(%{}, fn el, acc ->
network = to_string(el.network_connection.network_id)
network = el.network_connection.network_id
value = Map.take(el.network_connection, [:uplink, :downlink])

sum_map_values = &Map.merge(&1, value, fn _, v1, v2 -> v1 + v2 end)
Expand Down
2 changes: 1 addition & 1 deletion lib/hardware/internal/network_connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ defmodule Helix.Hardware.Internal.NetworkConnection do
end
end

@spec update_ip(NetworkConnection.t | NetworkConnection.id, NetworkConnection.ip) ::
@spec update_ip(NetworkConnection.idt, NetworkConnection.ip) ::
{:ok, NetworkConnection}
| {:error, Ecto.Changeset.t}
def update_ip(nc = %NetworkConnection{}, new_ip) do
Expand Down
6 changes: 2 additions & 4 deletions lib/hell/hack.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,9 @@ defmodule HELL.Hack.Experience do
{:join, 3}
],
"Elixir.Helix.Process.Model.Processable" => [
{:dynamic_resources, 1},
{:state_change, 4},
{:kill, 3},
{:minimum, 1},
{:conclusion, 2},
{:complete, 2},
{:connection_closed, 3},
{:after_read_hook, 1}
],
"Elixir.Helix.Process.Public.View.ProcessViewable" => [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
defmodule Helix.Process.Model.Process.NaiveStruct do
@moduledoc false

import HELL.Macros

docp """
defmodule HELL.NaiveStruct do
@moduledoc """
This module is a converter that transforms any struct into a non-struct map
(while keeping the metadata of which struct that map was) and transforms that
map back into it's original struct on runtime.
Expand All @@ -13,6 +9,7 @@ defmodule Helix.Process.Model.Process.NaiveStruct do
"""

@behaviour Ecto.Type
import HELL.Macros

def type, do: :map

Expand Down
2 changes: 1 addition & 1 deletion lib/log/event/handler/log.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ defmodule Helix.Log.Event.Handler.Log do

def log_forge_conclusion(event = %LogForgeCreateComplete{}) do
{:ok, _, events} = LogAction.create(
event.target_server_id,
event.target_id,
event.entity_id,
event.message,
event.version)
Expand Down
33 changes: 18 additions & 15 deletions lib/network/henforcer/network.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,25 @@ defmodule Helix.Network.Henforcer.Network do

@spec node_connected?(Server.id, Network.id) ::
boolean
def node_connected?(server, network) do
# FIXME: This looks awful
# FIXME: Test (needs network factory and some patience)
network_id = to_string(network)
with \
%{motherboard_id: motherboard} <- ServerQuery.fetch(server),
component = %{} <- ComponentQuery.fetch(motherboard),
motherboard = %{} <- MotherboardQuery.fetch(component),
%{net: %{^network_id => _}} <- MotherboardQuery.resources(motherboard)
do
true
else
_ ->
false
end
def node_connected?(_, _) do
true
end
# def node_connected?(server, network) do
# # FIXME: This looks awful
# # FIXME: Test (needs network factory and some patience)
# network_id = to_string(network)
# with \
# %{motherboard_id: motherboard} <- ServerQuery.fetch(server),
# component = %{} <- ComponentQuery.fetch(motherboard),
# motherboard = %{} <- MotherboardQuery.fetch(component),
# %{net: %{^network_id => _}} <- MotherboardQuery.resources(motherboard)
# do
# true
# else
# _ ->
# false
# end
# end

@spec has_ssh_connection?(Server.id, Server.id) ::
boolean
Expand Down
16 changes: 16 additions & 0 deletions lib/process/action/flow/process.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule Helix.Process.Action.Flow.Process do

import HELF.Flow

alias Helix.Event
alias Helix.Process.Model.Process
alias Helix.Process.Action.Process, as: ProcessAction

def signal(process = %Process{}, signal, params) do
flowing do
with {:ok, events} <- ProcessAction.signal(process, signal, params) do
Event.emit(events)
end
end
end
end
Loading

0 comments on commit c8d9b54

Please sign in to comment.