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

Commit

Permalink
Merge pull request #322 from renatomassaro/top-rewrite
Browse files Browse the repository at this point in the history
Rewrite TOP
  • Loading branch information
renatomassaro authored Nov 8, 2017
2 parents 478ae6b + 1382643 commit d164a1c
Show file tree
Hide file tree
Showing 169 changed files with 7,461 additions and 4,393 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ env:
- HELIX_ENDPOINT_SECRET=uhauhauhhauuhahuahuaha
- HELIX_NODE_NAME=travis
- HELIX_CLUSTER_COOKIE=uehauehuaheuaheuaheuhe
- HELIX_TEST_ENV=travis
2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ retry(2) {
unstash 'source'
unstash 'build-test'

withEnv (['MIX_ENV=test', 'HELIX_SKIP_WARNINGS=false']) {
withEnv (['MIX_ENV=test', 'HELIX_SKIP_WARNINGS=false', 'HELIX_TEST_ENV=jenkins']) {
// HACK: mix complains if I don't run deps.get again, not sure why
// TODO: it's compiling everything again, find out why
sh 'mix deps.get'
Expand Down
3 changes: 2 additions & 1 deletion config/account/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ config :bcrypt_elixir, :log_rounds, 2

config :helix, Helix.Account.Repo,
pool: Ecto.Adapters.SQL.Sandbox,
database: prefix <> "_test_account"
database: prefix <> "_test_account",
ownership_timeout: 90_000
3 changes: 2 additions & 1 deletion config/cache/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ prefix = System.get_env("HELIX_DB_PREFIX") || "helix"

config :helix, Helix.Cache.Repo,
pool: Ecto.Adapters.SQL.Sandbox,
database: prefix <> "_test_cache"
database: prefix <> "_test_cache",
ownership_timeout: 90_000
3 changes: 2 additions & 1 deletion config/core/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ prefix = System.get_env("HELIX_DB_PREFIX") || "helix"

config :helix, Helix.Core.Repo,
pool: Ecto.Adapters.SQL.Sandbox,
database: prefix <> "_test_core"
database: prefix <> "_test_core",
ownership_timeout: 90_000
3 changes: 2 additions & 1 deletion config/entity/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ prefix = System.get_env("HELIX_DB_PREFIX") || "helix"

config :helix, Helix.Entity.Repo,
pool: Ecto.Adapters.SQL.Sandbox,
database: prefix <> "_test_entity"
database: prefix <> "_test_entity",
ownership_timeout: 90_000
3 changes: 2 additions & 1 deletion config/hardware/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ prefix = System.get_env("HELIX_DB_PREFIX") || "helix"

config :helix, Helix.Hardware.Repo,
pool: Ecto.Adapters.SQL.Sandbox,
database: prefix <> "_test_hardware"
database: prefix <> "_test_hardware",
ownership_timeout: 90_000
3 changes: 2 additions & 1 deletion config/log/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ prefix = System.get_env("HELIX_DB_PREFIX") || "helix"

config :helix, Helix.Log.Repo,
pool: Ecto.Adapters.SQL.Sandbox,
database: prefix <> "_test_log"
database: prefix <> "_test_log",
ownership_timeout: 90_000
3 changes: 2 additions & 1 deletion config/network/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ prefix = System.get_env("HELIX_DB_PREFIX") || "helix"

config :helix, Helix.Network.Repo,
pool: Ecto.Adapters.SQL.Sandbox,
database: prefix <> "_test_network"
database: prefix <> "_test_network",
ownership_timeout: 90_000
3 changes: 2 additions & 1 deletion config/process/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ prefix = System.get_env("HELIX_DB_PREFIX") || "helix"

config :helix, Helix.Process.Repo,
pool: Ecto.Adapters.SQL.Sandbox,
database: prefix <> "_test_process"
database: prefix <> "_test_process",
ownership_timeout: 90_000
3 changes: 2 additions & 1 deletion config/server/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ prefix = System.get_env("HELIX_DB_PREFIX") || "helix"

config :helix, Helix.Server.Repo,
pool: Ecto.Adapters.SQL.Sandbox,
database: prefix <> "_test_server"
database: prefix <> "_test_server",
ownership_timeout: 90_000
3 changes: 2 additions & 1 deletion config/software/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ prefix = System.get_env("HELIX_DB_PREFIX") || "helix"

config :helix, Helix.Software.Repo,
pool: Ecto.Adapters.SQL.Sandbox,
database: prefix <> "_test_software"
database: prefix <> "_test_software",
ownership_timeout: 90_000
3 changes: 2 additions & 1 deletion config/story/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ prefix = System.get_env("HELIX_DB_PREFIX") || "helix"

config :helix, Helix.Story.Repo,
pool: Ecto.Adapters.SQL.Sandbox,
database: prefix <> "_test_story"
database: prefix <> "_test_story",
ownership_timeout: 90_000
3 changes: 2 additions & 1 deletion config/universe/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ prefix = System.get_env("HELIX_DB_PREFIX") || "helix"

config :helix, Helix.Universe.Repo,
pool: Ecto.Adapters.SQL.Sandbox,
database: prefix <> "_test_universe"
database: prefix <> "_test_universe",
ownership_timeout: 90_000
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
15 changes: 4 additions & 11 deletions lib/cache/state/queue_sync.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,21 @@ defmodule Helix.Cache.State.QueueSync do

alias Helix.Cache.State.PurgeQueue, as: StatePurgeQueue

@interval 15 * 1000 #15s
@interval (if Mix.env == :test,
do: 999_999_999, # Never sync during test
else: 15 * 1000) # 15s default sync time (on :dev and :prod)

@registry_name :cache_queue_sync

def start_link(interval \\ @interval) do
GenServer.start_link(__MODULE__, interval, name: @registry_name)
end

def set_interval(interval) do
GenServer.call(@registry_name, {:set_interval, interval})
end

def init(interval) do
timer_ref = schedule(interval)
{:ok, %{timer_ref: timer_ref, interval: interval}}
end

def handle_call({:set_interval, new_interval}, _from, state) do
Process.cancel_timer(state.timer_ref)
new_timer = schedule(new_interval)
{:reply, :ok, %{timer_ref: new_timer, interval: new_interval}}
end

def handle_info(:sync, state) do
StatePurgeQueue.sync()
timer_ref = schedule(state.interval)
Expand Down
19 changes: 18 additions & 1 deletion lib/event/dispatcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ defmodule Helix.Event.Dispatcher do
:connection_closed
event NetworkEvent.Connection.Closed,
ProcessHandler.TOP,
:connection_closed
:object_handler

##############################################################################
# Log events
Expand All @@ -112,6 +112,23 @@ defmodule Helix.Event.Dispatcher do
# All
event ProcessEvent.Process.Created
event ProcessEvent.Process.Completed
event ProcessEvent.Process.Killed
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
19 changes: 19 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 All @@ -101,6 +114,12 @@ defmodule Helix.Event do
event
end

# Accumulate source event on the stacktrace, and save it on the next event
stack = get_stack(source) || []
new_stack = stack ++ [source.__struct__]

event = set_stack(event, new_stack)

# Everything has been inherited, we are ready to emit/1 the event.
event
end
Expand Down
6 changes: 5 additions & 1 deletion lib/event/meta.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ defmodule Helix.Event.Meta do
# The `process_id` field is used to identify which process (if any) was
# responsible for the emission of the current event. Useful to correlate
# processes side-effects to their process ids on the Client.
:process_id
:process_id,

# The `stack` field is a rudimentary stacktrace. Every time an event is
# emitted from another one, the previous event name is stored on this stack.
:stack
]

@doc """
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
19 changes: 7 additions & 12 deletions lib/hardware/internal/motherboard.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ defmodule Helix.Hardware.Internal.Motherboard do
end
end

@spec fetch_by_nip(Network.id, NetworkConnection.ip) ::
@spec fetch_by_nip(Network.id, Network.ip) ::
Motherboard.t
| nil
def fetch_by_nip(network_id, ip) do
Expand Down Expand Up @@ -102,38 +102,33 @@ 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()
end

@spec resources(Motherboard.t) ::
%{
cpu: non_neg_integer,
ram: non_neg_integer,
hdd: non_neg_integer,
net: %{String.t => %{uplink: non_neg_integer, downlink: non_neg_integer}}
}
Motherboard.resources
def resources(motherboard) do
components_ids = get_components_ids(motherboard)

Expand All @@ -155,7 +150,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
Loading

0 comments on commit d164a1c

Please sign in to comment.