-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patherlesque.erl
97 lines (76 loc) · 1.89 KB
/
erlesque.erl
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
-module(erlesque_worker).
-export([start/2, magic/2, test_job/1]).
-export([parse_resque_job/1]).
start(Address, Queues) ->
% Check that Queues is not empty
case erldis:connect(Address) of
{ok, Client} ->
io:format("Connected to redis server ~s~n", [Address]),
Job = get_job(Client, Queues, Queues),
io:format("~s~n", [Job]);
ignore ->
io:format("Unable to connect to redis server ~s~n", [Address])
end.
get_job(Client, [], All_queues) ->
io:format("No jobs found, sleeping for 10 seconds~n", []),
timer:sleep(10000),
get_job(Client, All_queues, All_queues);
get_job(Client, [First_queue|Other_queues], All_queues) ->
Queue = string:concat("resque:queue:", First_queue),
io:format("Attempting lpop of ~s~n", [Queue]),
case erldis:lpop(Client, Queue) of
ok ->
case erldis:get_all_results(Client) of
[nil] ->
get_job(Client, Other_queues, All_queues);
[Item] -> Item
end;
_ -> {error}
end.
% stubs
working() ->
{ok}.
workers() ->
{ok}.
worker_exists(Worker_id) ->
{ok}.
working_on(Job_id) ->
{ok}.
queues() ->
{ok}.
shutdown(Worker_id) ->
{ok}.
pause(Worker_id) ->
{ok}.
unpause(Worker_id) ->
{ok}.
register_worker() ->
{ok}.
unregister_worker() ->
{ok}.
done_working() ->
{ok}.
processed() ->
{ok}.
started() ->
{ok}.
state(Worker_id) ->
{ok}.
log_message() ->
{ok}.
% /stubs
magic(Job, Args) ->
Fun_str = string:concat("fun(A) -> ", string:concat(Job, "(A) end.")),
{ok, Tokens, _} = erl_scan:string(Fun_str),
{ok, [Form]} = erl_parse:parse_exprs(Tokens),
{value, Fun, _} = erl_eval:expr(Form, []),
Fun(Args).
% Need to find an Erlang JSON parser
parse_resque_job(String) ->
Data = string:tokens(String, "\"{}[],"),
LessData = [X || X <- Data, X /= ":"],
Job = lists:nth(2, LessData),
Args = lists:nthtail(3, LessData),
{Job, Args}.
test_job(Args) ->
io:format("TEST JOB ~s~n", Args).