summaryrefslogtreecommitdiff
blob: 0d7df473ce5209a023703af0236f98df74c14c38 (plain)
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
-module(bnt_bounty).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-type id() :: ataxia_id:type().
-type job() :: {atom(), atom(), list(any())}.

-record
(
   bounty,
   {
      deadline :: ataxia_time:type(),
      user :: ataxia_security:user(),
      job :: job()
   }
).

-type type() :: #bounty{}.

-export_type([type/0, id/0]).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-export
(
   [
      generate/4,
      execute/1
   ]
).

-export
(
   [
      get_deadline/1,
      get_user/1,
      get_job/1
   ]
).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec generate
   (
      ataxia_security:user(),
      ataxia_time:type(),
      ataxia_time:type(),
      job()
   )
   -> 'ok'.
generate (User, NotBefore, Deadline, Job) ->
   Janitor = ataxia_security:janitor(),
   JanitorOnly = ataxia_security:allow_only(Janitor),

   Bounty =
      {
         deadline = Deadline,
         user = User,
         job = Job
      },

   {ok, _BountyID} =
      ataxia_client:add
      (
         bounty_db,
         JanitorOnly,
         JanitorOnly,
         ataxia_lock:locked(User, NotBefore),
         Bounty
      ),

   ok.

-spec execute (type()) -> any().
execute (Bounty) ->
   {Module, Function, Params} = Bounty#bounty.job,
   erlang:apply(Module, Function, Params).

-spec get_deadline (type()) -> ataxia_time:type().
get_deadline (Bounty) -> Bounty#bounty.deadline.

-spec get_user (type()) -> ataxia_security:user().
get_user (Bounty) -> Bounty#bounty.user.

-spec get_job (type()) -> job().
get_job (Bounty) -> Bounty#bounty.job.