-
Notifications
You must be signed in to change notification settings - Fork 414
/
task.ex
184 lines (137 loc) · 4.48 KB
/
task.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
defmodule Credo.Execution.Task do
@moduledoc """
A Task is a step in a pipeline, which is given an `Credo.Execution` struct and must return one as well.
Tasks in a pipeline are only called if they are not "halted" (see `Credo.Execution.halt/2`).
It implements a `call/1` or `call/2` callback, which is called with the `Credo.Execution` struct
as first parameter (and the Task's options as the second in case of `call/2`).
"""
@typedoc false
@type t :: module
@doc """
Is called by the pipeline and contains the Task's actual code.
defmodule FooTask do
use Credo.Execution.Task
def call(exec) do
IO.inspect(exec)
end
end
The `call/1` functions receives an `exec` struct and must return a (modified) `Credo.Execution`.
"""
@callback call(exec :: Credo.Execution.t()) :: Credo.Execution.t()
@doc """
Works like `call/1`, but receives the options, which are optional when registering the Task, as second argument.
defmodule FooTask do
use Credo.Execution.Task
def call(exec, opts) do
IO.inspect(opts)
exec
end
end
"""
@callback call(exec :: Credo.Execution.t(), opts :: Keyword.t()) :: Credo.Execution.t()
@doc """
Gets called if `call` holds the execution via `Credo.Execution.halt/1` or `Credo.Execution.halt/2`.
"""
@callback error(exec :: Credo.Execution.t()) :: Credo.Execution.t()
@doc """
Works like `error/1`, but receives the options, which were given during pipeline registration, as second argument.
"""
@callback error(exec :: Credo.Execution.t(), opts :: Keyword.t()) :: Credo.Execution.t()
require Logger
alias Credo.Execution
alias Credo.Execution.ExecutionTiming
defmacro __using__(_opts \\ []) do
quote do
@behaviour Credo.Execution.Task
import Credo.Execution
alias Credo.Execution
alias Credo.CLI.Output.UI
@impl true
def call(%Execution{halted: false} = exec) do
exec
end
@impl true
def call(%Execution{halted: false} = exec, opts) do
call(exec)
end
@impl true
def error(exec) do
case Execution.get_halt_message(exec) do
"" <> halt_message ->
command_name = Execution.get_command_name(exec) || "credo"
UI.warn([:red, "** (#{command_name}) ", halt_message])
_ ->
IO.warn("Execution halted during #{__MODULE__}!")
end
exec
end
@impl true
def error(exec, _opts) do
error(exec)
end
defoverridable call: 1
defoverridable call: 2
defoverridable error: 1
defoverridable error: 2
end
end
@doc false
def run(task, exec, opts \\ [])
def run(task, %Credo.Execution{debug: true} = exec, opts) do
run_with_timing(task, exec, opts)
end
def run(task, %Execution{} = exec, opts) do
do_run(task, exec, opts)
end
def run(_task, exec, _opts) do
IO.warn(
"Expected second parameter of Task.run/3 to match %Credo.Execution{}, " <>
"got: #{inspect(exec)}"
)
exec
end
defp do_run(task, %Credo.Execution{halted: false} = exec, opts) do
old_parent_task = exec.parent_task
old_current_task = exec.current_task
exec =
exec
|> Execution.set_parent_and_current_task(exec.current_task, task)
|> task.call(opts)
|> Execution.ensure_execution_struct("#{task}.call/2")
if exec.halted do
exec
|> task.error(opts)
|> Execution.set_parent_and_current_task(old_parent_task, old_current_task)
else
Execution.set_parent_and_current_task(exec, old_parent_task, old_current_task)
end
end
defp do_run(_task, exec, _opts) do
exec
end
#
defp run_with_timing(task, exec, opts) do
context_tuple = {:task, exec, task, opts}
log(:call_start, context_tuple)
{started_at, time, exec} = ExecutionTiming.run(&do_run/3, [task, exec, opts])
log(:call_end, context_tuple, time)
ExecutionTiming.append(exec, [task: task, parent_task: exec.parent_task], started_at, time)
exec
end
defp log(:call_start, {:task, _exec, task, _opts}) do
Logger.info("Calling #{task} ...")
end
defp log(:call_end, {:task, _exec, task, _opts}, time) do
Logger.info("Finished #{task} in #{format_time(time)} ...")
end
defp format_time(time) do
cond do
time > 1_000_000 ->
"#{div(time, 1_000_000)}s"
time > 1_000 ->
"#{div(time, 1_000)}ms"
true ->
"#{time}μs"
end
end
end