clean up some sequence/regimen/farm_event stuff
This commit is contained in:
parent
41d46577fc
commit
d569618679
2
.iex.exs
2
.iex.exs
|
@ -13,7 +13,7 @@ if Code.ensure_loaded? Farmbot do
|
|||
RegimenRunner,
|
||||
CeleryScript,
|
||||
DebugLog,
|
||||
FarmEventRunner,
|
||||
FarmEvent.Runner,
|
||||
Lib,
|
||||
Sequence.Runner,
|
||||
Token,
|
||||
|
|
|
@ -13,7 +13,7 @@ if Code.ensure_loaded? Farmbot do
|
|||
RegimenRunner,
|
||||
CeleryScript,
|
||||
DebugLog,
|
||||
FarmEventRunner,
|
||||
FarmEvent.Runner,
|
||||
Lib,
|
||||
Sequence.Runner,
|
||||
Token,
|
||||
|
|
|
@ -17,8 +17,12 @@ defmodule Farmbot.CeleryScript.Command.Execute do
|
|||
def run(%{sequence_id: id}, [], context) do
|
||||
sequence = Farmbot.Database.get_by_id(context, Sequence, id)
|
||||
unless sequence do
|
||||
raise Error, message: "Could not find sequence by id: #{id}"
|
||||
raise Error, context: context,
|
||||
message: "Could not find sequence by id: #{id}"
|
||||
end
|
||||
sequence |> Map.get(:body) |> Ast.parse |> Command.do_command(context)
|
||||
|
||||
sequence.body
|
||||
|> Ast.parse()
|
||||
|> Command.do_command(context)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -65,6 +65,12 @@ defmodule Farmbot.Database.Syncable do
|
|||
import Farmbot.Database.Syncable, only: [parse_resp: 2]
|
||||
defstruct unquote(model) ++ [:id]
|
||||
|
||||
defimpl Inspect, for: __MODULE__ do
|
||||
def inspect(syncable, _) do
|
||||
"#Syncable<#{List.last(Module.split(__MODULE__))} #{syncable.id}>"
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
The Singular api endpoing url.
|
||||
"""
|
||||
|
|
|
@ -3,9 +3,15 @@ defmodule Farmbot.Database.Syncable.Regimen do
|
|||
A Regimen from the Farmbot API.
|
||||
"""
|
||||
|
||||
alias Farmbot.Database
|
||||
alias Farmbot.{Context, Database}
|
||||
alias Database.Syncable
|
||||
use Syncable, model: [
|
||||
:regimen_items
|
||||
], endpoint: {"/regimens", "/regimens"}
|
||||
|
||||
defimpl Farmbot.FarmEvent.Executer, for: __MODULE__ do
|
||||
def execute_event(regimen, %Context{} = ctx, now) do
|
||||
{:ok, _pid} = Farmbot.Regimen.Supervisor.add_child(ctx, regimen, now)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,7 +3,7 @@ defmodule Farmbot.Database.Syncable.Sequence do
|
|||
A Sequence from the Farmbot API.
|
||||
"""
|
||||
|
||||
alias Farmbot.Database
|
||||
alias Farmbot.{Context, Database}
|
||||
alias Database.Syncable
|
||||
use Syncable, model: [
|
||||
:version,
|
||||
|
|
13
lib/farmbot/farm_event/protocol.ex
Normal file
13
lib/farmbot/farm_event/protocol.ex
Normal file
|
@ -0,0 +1,13 @@
|
|||
defprotocol Farmbot.FarmEvent.Executer do
|
||||
# @fallback_to_any true
|
||||
@doc """
|
||||
Execute an item.
|
||||
"""
|
||||
def execute_event(data, context, now)
|
||||
end
|
||||
|
||||
# defimpl Farmbot.FarmEvent.Executer, for: Any do
|
||||
# def execute(context, data) do
|
||||
# raise "This is kind of redundant"
|
||||
# end
|
||||
# end
|
|
@ -1,9 +1,10 @@
|
|||
defmodule Farmbot.FarmEventRunner do
|
||||
defmodule Farmbot.FarmEvent.Runner do
|
||||
@moduledoc """
|
||||
Checks the database every 60 seconds for FarmEvents
|
||||
"""
|
||||
require Logger
|
||||
alias Farmbot.{Context, DebugLog, Database, CeleryScript}
|
||||
import Farmbot.FarmEvent.Executer
|
||||
alias CeleryScript.Ast
|
||||
use DebugLog
|
||||
use GenServer
|
||||
|
@ -34,12 +35,11 @@ defmodule Farmbot.FarmEventRunner do
|
|||
all_events =
|
||||
context
|
||||
|> Database.get_all(FarmEvent)
|
||||
|> Enum.map(fn(thing) -> thing.body end)
|
||||
|> Enum.map(fn(db_object) -> db_object.body end)
|
||||
|
||||
{late_events, new} = do_checkup(context, all_events, now, state)
|
||||
unless Enum.empty?(late_events) do
|
||||
Logger.info "Time for event to run: #{inspect late_events} " <>
|
||||
" at: #{now.hour}:#{now.minute}"
|
||||
Logger.info "Time for event to run at: #{now.hour}:#{now.minute}"
|
||||
start_events(context, late_events, now)
|
||||
end
|
||||
new
|
||||
|
@ -54,15 +54,7 @@ defmodule Farmbot.FarmEventRunner do
|
|||
:: no_return
|
||||
defp start_events(_context, [], _now), do: :ok
|
||||
defp start_events(%Context{} = context, [event | rest], now) do
|
||||
cond do
|
||||
match?(%Sequence{}, event) ->
|
||||
ast = Ast.parse(event)
|
||||
Farmbot.CeleryScript.Command.do_command(ast, context)
|
||||
match?(%Regimen{}, event) ->
|
||||
{:ok, _pid} = Farmbot.Regimen.Supervisor.add_child(context, event, now)
|
||||
true ->
|
||||
Logger.error ">> Doesn't know how to handle event: #{inspect event}"
|
||||
end
|
||||
execute_event(event, context, now)
|
||||
start_events(context, rest, now)
|
||||
end
|
||||
|
||||
|
@ -103,9 +95,9 @@ defmodule Farmbot.FarmEventRunner do
|
|||
:: {late_event | nil, DateTime.t}
|
||||
defp check_event(%Context{} = ctx, %FarmEvent{} = f, now, last_time) do
|
||||
# Get the executable out of the database
|
||||
thing = [Farmbot.Database, Syncable, f.executable_type |> String.to_atom]
|
||||
mod = Module.concat(thing)
|
||||
event = lookup(ctx, mod, f.executable_id)
|
||||
mod_list = [Farmbot.Database, Syncable, f.executable_type |> String.to_atom]
|
||||
mod = Module.safe_concat(mod_list)
|
||||
event = lookup(ctx, mod, f.executable_id)
|
||||
|
||||
# build a local start time
|
||||
start_time = Timex.parse! f.start_time, "{ISO:Extended}"
|
8
lib/farmbot/farm_event/sequence.ex
Normal file
8
lib/farmbot/farm_event/sequence.ex
Normal file
|
@ -0,0 +1,8 @@
|
|||
defimpl Farmbot.FarmEvent.Executer, for: Farmbot.Database.Syncable.Sequence do
|
||||
alias Farmbot.Context
|
||||
def execute_event(sequence, %Context{} = ctx, _now) do
|
||||
sequence
|
||||
|> Farmbot.CeleryScript.Ast.parse()
|
||||
|> Farmbot.CeleryScript.Command.do_command(ctx)
|
||||
end
|
||||
end
|
|
@ -9,8 +9,8 @@ defmodule Farmbot.FarmEvent.Supervisor do
|
|||
children = [
|
||||
worker(Farmbot.Regimen.Supervisor,
|
||||
[context, [name: Farmbot.Regimen.Supervisor ]], [restart: :permanent]),
|
||||
worker(Farmbot.FarmEventRunner,
|
||||
[context, [name: Farmbot.FarmEventRunner ]], [restart: :permanent])
|
||||
worker(Farmbot.FarmEvent.Runner,
|
||||
[context, [name: Farmbot.FarmEvent.Runner ]], [restart: :permanent])
|
||||
]
|
||||
opts = [strategy: :one_for_one]
|
||||
supervise(children, opts)
|
||||
|
|
|
@ -153,7 +153,6 @@ defmodule Farmbot.HTTP do
|
|||
debug_log "Starting client."
|
||||
case url do
|
||||
"/api" <> _ ->
|
||||
debug_log "I think this is a farmbot api request: #{url}"
|
||||
r = {method, url, body, headers, opts}
|
||||
build_api_request(state.token, state.context, r, from)
|
||||
_ ->
|
||||
|
|
|
@ -9,10 +9,6 @@ defmodule Farmbot.Sequence.Runner do
|
|||
require Logger
|
||||
use DebugLog
|
||||
|
||||
@typedoc """
|
||||
This gets injected into the args of a sequence, and all of its children etc.
|
||||
Mostly magic.
|
||||
"""
|
||||
@type context :: Context.t
|
||||
@type sequence_pid :: pid
|
||||
|
||||
|
|
Loading…
Reference in a new issue