Skip to content
This repository has been archived by the owner on Jul 14, 2021. It is now read-only.

Commit

Permalink
Merge pull request #7 from opscode/md/oc-1069
Browse files Browse the repository at this point in the history
md/oc-1069: make GET /pushy/jobs list work
  • Loading branch information
jkeiser committed Sep 10, 2012
2 parents 6850af4 + 0688c05 commit 43717b8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
12 changes: 10 additions & 2 deletions apps/pushy/src/pushy_job_state_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
-export([start_link/0,
start/1,
get_process/1,
get_job_processes/0,
register_process/1]).

%% Supervisor callbacks
Expand All @@ -32,18 +33,25 @@ start(Job) ->
-spec get_process(object_id()) -> pid() | not_found.
get_process(JobId) ->
try
gproc:lookup_pid({n,l,JobId})
gproc:lookup_pid({n,l,{pushy_job, JobId}})
catch
error:badarg -> not_found
end.

-spec get_job_processes() -> {binary(), pid()}.
get_job_processes() ->
MatchHead = pushy_util:gproc_match_head(n, l, {pushy_job, '_'}),
Guard = [],
Result = ['$$'],
[{JobId, Pid} || [{_,_,{_,JobId}},Pid,_] <- gproc:select([{MatchHead, Guard, Result}])].

-spec register_process(object_id()) -> pid().
register_process(JobId) ->
try
%% The most important thing to have happen is this registration; we need to get this
%% assigned before anyone else tries to start things up gproc:reg can only return
%% true or throw
true = gproc:reg({n, l, JobId}),
true = gproc:reg({n, l, {pushy_job, JobId}}),
true
catch
error:badarg ->
Expand Down
28 changes: 26 additions & 2 deletions apps/pushy/src/pushy_jobs_resource.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
content_types_provided/2,
is_authorized/2,
from_json/2,
to_json/2,
post_is_create/2,
create_path/2]).

Expand Down Expand Up @@ -42,13 +43,13 @@ is_authorized(Req, State) ->
{true, Req, State2}.

allowed_methods(Req, State) ->
{['POST'], Req, State}.
{['POST', 'GET'], Req, State}.

content_types_accepted(Req, State) ->
{[{"application/json", from_json}], Req, State}.

content_types_provided(Req, State) ->
{[{"application/json", undef}], Req, State}.
{[{"application/json", to_json}], Req, State}.

% {
% 'command' = 'chef-client',
Expand All @@ -72,8 +73,31 @@ from_json(Req, State) ->
Req2 = ripped_from_chef_rest:set_uri_of_created_resource(Req),
{true, Req2, State}.

%% GET /pushy/jobs
to_json(Req, State) ->
Jobs = jobs_to_json(get_jobs(pushy_job_state_sup:get_job_processes())),

{jiffy:encode(Jobs), Req, State}.

% Private stuff

get_jobs(JobTuples) ->
[pushy_job_state:get_job_state(JobId) || {JobId, _} <- JobTuples].

jobs_to_json(Jobs) ->
[job_to_json(Job) || Job <- Jobs].

job_to_json(#pushy_job{
id = Id,
status = Status
%created_at = CreatedAt
}) ->
%CreatedAtDate = iolist_to_binary(httpd_util:rfc1123_date(CreatedAt)),
{[ {<<"id">>, iolist_to_binary(Id)},
{<<"status">>, Status}
%{<<"created_at">>, CreatedAtDate}
]}.

parse_post_body(Req) ->
Body = wrq:req_body(Req),
JobJson = jiffy:decode(Body),
Expand Down
10 changes: 9 additions & 1 deletion apps/pushy/src/pushy_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
signed_header_from_message/2,
rand_bytes/1,
guid_v4/0,
gen_req_id_using_rand/2
gen_req_id_using_rand/2,
gproc_match_head/3
]).

-include_lib("eunit/include/eunit.hrl").
Expand Down Expand Up @@ -157,3 +158,10 @@ gen_req_id_using_rand(Prefix, NBytes) ->
NBits = NBytes*8,
<<RandValue:NBits, _/binary>> = RandBytes,
iolist_to_binary([Prefix, integer_to_list(RandValue, 16)]).

%% MatchHead for gproc
-spec gproc_match_head(atom(), atom(), any()) -> {{atom(), atom(), any()}, '_', '_'}.
gproc_match_head(Type, Scope, Key) ->
{{Type, Scope, Key}, '_', '_'}.


0 comments on commit 43717b8

Please sign in to comment.