Skip to content

Commit 81b6479

Browse files
Merge pull request #7 from criteo/extend_structures
Expose more fields from commandInfo to hooks
2 parents 7595efd + 753916b commit 81b6479

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

include/RubyEngine.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace criteo {
1414
namespace helpers{
1515
void hash_set(VALUE hash, const std::string& key, const std::string& value);
1616
void hash_set(VALUE hash, const std::string& key, VALUE value);
17+
void ary_push(VALUE hash, const std::string& el);
1718
bool is_non_empty_hash(VALUE hash);
1819
bool is_non_empty_array(VALUE array);
1920
}

src/RubyEngine.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ namespace criteo {
2929
rb_hash_aset(hash, rb_str_new_cstr(key.c_str()), value);
3030
}
3131

32+
void ary_push(VALUE ary, const std::string& el)
33+
{
34+
rb_ary_push(ary, rb_str_new_cstr(el.c_str()));
35+
}
36+
3237
bool is_non_empty_hash(VALUE hash)
3338
{
3439
return (!NIL_P(hash) && RB_TYPE_P(hash, T_HASH) && RHASH_SIZE(hash) > 0);

src/RubyHook.cpp

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "RubyHook.hpp"
55
#include <stdexcept>
6+
#include <string>
67

78
#include <ruby.h>
89

@@ -59,28 +60,6 @@ static int unwrapEnv(VALUE name, VALUE value, VALUE arg)
5960
return ST_CONTINUE;
6061
}
6162

62-
// map parts of mesos::TaskInfo into a Ruby hash structure
63-
VALUE wrapTaskInfo(const mesos::TaskInfo& taskInfo)
64-
{
65-
// create top level hash with required PB fields
66-
VALUE hash = rb_hash_new();
67-
hash_set(hash, "name", taskInfo.name());
68-
hash_set(hash, "task_id", taskInfo.task_id().value());
69-
hash_set(hash, "slave_id", taskInfo.slave_id().value());
70-
71-
// add Labels as a strings hash and insert it into main hash as "labels"
72-
// e.g. in Ruby use: taskInfo["labels"]["foo"] = "bar"
73-
VALUE labels = rb_hash_new();
74-
if (taskInfo.has_labels()) {
75-
foreach (const mesos::Label& l, taskInfo.labels().labels()) {
76-
hash_set(labels, l.key(), l.has_value() ? l.value() : "");
77-
}
78-
}
79-
hash_set(hash, "labels", labels);
80-
81-
return hash;
82-
}
83-
8463
// map parts of mesos::ExecutorInfo into a Ruby hash structure
8564
VALUE wrapExecutorInfo(const mesos::ExecutorInfo& executorInfo)
8665
{
@@ -104,11 +83,47 @@ VALUE wrapExecutorInfo(const mesos::ExecutorInfo& executorInfo)
10483
VALUE user = commandInfo.has_user() ? rb_str_new_cstr(commandInfo.user().c_str())
10584
: rb_str_new_cstr("no_user_found");
10685
hash_set(command, "user", user);
86+
VALUE value = commandInfo.has_value() ? rb_str_new_cstr(commandInfo.value().c_str())
87+
: rb_str_new_cstr("no command value");
88+
hash_set(command, "value", value);
89+
90+
VALUE args = rb_ary_new();
91+
foreach (const std::string& arg, commandInfo.arguments()) {
92+
ary_push(args, arg);
93+
}
94+
hash_set(command, "args", args);
10795
}
10896
hash_set(hash, "command", command);
10997

11098
return hash;
11199
}
100+
101+
// map parts of mesos::TaskInfo into a Ruby hash structure
102+
VALUE wrapTaskInfo(const mesos::TaskInfo& taskInfo)
103+
{
104+
// create top level hash with required PB fields
105+
VALUE hash = rb_hash_new();
106+
hash_set(hash, "name", taskInfo.name());
107+
hash_set(hash, "task_id", taskInfo.task_id().value());
108+
hash_set(hash, "slave_id", taskInfo.slave_id().value());
109+
if (taskInfo.has_executor()) {
110+
const mesos::ExecutorInfo& executorInfo = taskInfo.executor();
111+
hash_set(hash, "executor", wrapExecutorInfo(executorInfo));
112+
}
113+
114+
// add Labels as a strings hash and insert it into main hash as "labels"
115+
// e.g. in Ruby use: taskInfo["labels"]["foo"] = "bar"
116+
VALUE labels = rb_hash_new();
117+
if (taskInfo.has_labels()) {
118+
foreach (const mesos::Label& l, taskInfo.labels().labels()) {
119+
hash_set(labels, l.key(), l.has_value() ? l.value() : "");
120+
}
121+
}
122+
hash_set(hash, "labels", labels);
123+
124+
return hash;
125+
}
126+
112127
}
113128

114129
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)