-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjob.ex
More file actions
95 lines (76 loc) · 2.18 KB
/
job.ex
File metadata and controls
95 lines (76 loc) · 2.18 KB
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
defmodule ElixirBench.Benchmarks.Job do
use Ecto.Schema
import Ecto.Changeset
import Ecto.Query, only: [from: 2, where: 2]
alias ElixirBench.Repos
alias ElixirBench.Repo
alias ElixirBench.Benchmarks.{Runner, Job, Config}
schema "jobs" do
field :uuid, :binary_id
belongs_to(:repo, Repos.Repo)
belongs_to(:claimant, Runner, foreign_key: :claimed_by)
field :claimed_at, :utc_datetime
field :completed_at, :utc_datetime
field :log, :string
field :exit_status, :integer
field :branch_name, :string
field :commit_message, :string
field :commit_sha, :string
field :commit_url, :string
field :cpu, :string
field :cpu_count, :integer
field :dependency_versions, {:map, :string}
field :elixir_version, :string
field :erlang_version, :string
field :memory_mb, :integer
field :claim_count, :integer, default: 0
embeds_one(:config, Config)
timestamps()
end
@max_retries Confex.fetch_env!(:elixir_bench, :job_max_retries)
@submit_fields [
:elixir_version,
:erlang_version,
:dependency_versions,
:cpu,
:cpu_count,
# TODO: change to a string memory
# :memory_mb,
:log,
:exit_status
]
@create_fields [
:branch_name,
:commit_sha
]
def claim_changeset(%Job{} = job, claimed_by) do
job
|> change(claimed_by: claimed_by, claimed_at: DateTime.utc_now())
|> change(claim_count: job.claim_count + 1)
# |> prepare_changes(&increment_claim_count/1)
end
def create_changeset(%Job{} = job, attrs) do
job
|> cast(attrs, @create_fields)
|> validate_required(@create_fields)
|> put_change(:uuid, Ecto.UUID.generate())
end
def submit_changeset(%Job{} = job, attrs) do
job
|> cast(attrs, @submit_fields)
|> validate_required(@submit_fields)
|> put_change(:completed_at, DateTime.utc_now())
end
def filter_by_repo(query, repo_id) do
from(j in query, where: j.repo_id == ^repo_id)
end
def claimable(query) do
from(j in query, where: j.claim_count < @max_retries)
end
defp increment_claim_count(changeset) do
Job
|> where(id: ^changeset.data.id)
|> Repo.update_all(inc: [claim_count: 1])
changeset
end
end