Skip to content

Commit

Permalink
Send events use json format
Browse files Browse the repository at this point in the history
  • Loading branch information
杨赫然 committed Dec 10, 2024
1 parent d04c324 commit 3e579ed
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 31 deletions.
15 changes: 11 additions & 4 deletions common/branch-mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,18 @@ get_commit_id (SeafDBRow *row, void *data)
static void
publish_repo_update_event (const char *repo_id, const char *commit_id)
{
char buf[128];
snprintf (buf, sizeof(buf), "repo-update\t%s\t%s",
repo_id, commit_id);
json_t *msg = json_object ();
char *msg_str = NULL;

seaf_mq_manager_publish_event (seaf->mq_mgr, SEAFILE_SERVER_CHANNEL_EVENT, buf);
json_object_set_new (msg, "msg_type", json_string("repo-update"));
json_object_set_new (msg, "repo_id", json_string(repo_id));
json_object_set_new (msg, "commit_id", json_string(commit_id));

msg_str = json_dumps (msg, JSON_PRESERVE_ORDER);

seaf_mq_manager_publish_event (seaf->mq_mgr, SEAFILE_SERVER_CHANNEL_EVENT, msg_str);
g_free (msg_str);
json_decref (msg);
}

static void
Expand Down
52 changes: 38 additions & 14 deletions fileserver/sync_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ type repoEventData struct {
clientName string
}

type statusEventData struct {
type statsEventData struct {
eType string
user string
repoID string
Expand Down Expand Up @@ -904,16 +904,23 @@ func getRepoStoreID(repoID string) (string, error) {
}

func sendStatisticMsg(repoID, user, operation string, bytes uint64) {
rData := &statusEventData{operation, user, repoID, bytes}
rData := &statsEventData{operation, user, repoID, bytes}

publishStatusEvent(rData)
publishStatsEvent(rData)
}

func publishStatusEvent(rData *statusEventData) {
buf := fmt.Sprintf("%s\t%s\t%s\t%d",
rData.eType, rData.user,
rData.repoID, rData.bytes)
if _, err := rpcclient.Call("publish_event", seafileServerChannelStats, buf); err != nil {
func publishStatsEvent(rData *statsEventData) {
data := make(map[string]interface{})
data["msg_type"] = rData.eType
data["user_name"] = rData.user
data["repo_id"] = rData.repoID
data["bytes"] = rData.bytes
jsonData, err := json.Marshal(data)
if err != nil {
log.Warnf("Failed to publish event: %v", err)
return
}
if _, err := rpcclient.Call("publish_event", seafileServerChannelStats, string(jsonData)); err != nil {
log.Warnf("Failed to publish event: %v", err)
}
}
Expand Down Expand Up @@ -1374,17 +1381,34 @@ func publishRepoEvent(rData *repoEventData) {
if rData.path == "" {
rData.path = "/"
}
buf := fmt.Sprintf("%s\t%s\t%s\t%s\t%s\t%s",
rData.eType, rData.user, rData.ip,
rData.clientName, rData.repoID, rData.path)
if _, err := rpcclient.Call("publish_event", seafileServerChannelEvent, buf); err != nil {
data := make(map[string]interface{})
data["msg_type"] = rData.eType
data["user_name"] = rData.user
data["ip"] = rData.ip
data["user_agent"] = rData.clientName
data["repo_id"] = rData.repoID
data["file_path"] = rData.path
jsonData, err := json.Marshal(data)
if err != nil {
log.Warnf("Failed to publish event: %v", err)
return
}
if _, err := rpcclient.Call("publish_event", seafileServerChannelEvent, string(jsonData)); err != nil {
log.Warnf("Failed to publish event: %v", err)
}
}

func publishUpdateEvent(repoID string, commitID string) {
buf := fmt.Sprintf("repo-update\t%s\t%s", repoID, commitID)
if _, err := rpcclient.Call("publish_event", seafileServerChannelEvent, buf); err != nil {
data := make(map[string]interface{})
data["msg_type"] = "repo-update"
data["repo_id"] = repoID
data["commit_id"] = commitID
jsonData, err := json.Marshal(data)
if err != nil {
log.Warnf("Failed to publish event: %v", err)
return
}
if _, err := rpcclient.Call("publish_event", seafileServerChannelEvent, string(jsonData)); err != nil {
log.Warnf("Failed to publish event: %v", err)
}
}
Expand Down
47 changes: 34 additions & 13 deletions server/http-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -541,28 +541,49 @@ free_stats_event_data (StatsEventData *data)
static void
publish_repo_event (RepoEventData *rdata)
{
GString *buf = g_string_new (NULL);
g_string_printf (buf, "%s\t%s\t%s\t%s\t%s\t%s",
rdata->etype, rdata->user, rdata->ip,
rdata->client_name ? rdata->client_name : "",
rdata->repo_id, rdata->path ? rdata->path : "/");
json_t *msg = json_object ();
char *msg_str = NULL;

json_object_set_new (msg, "msg_type", json_string(rdata->etype));
json_object_set_new (msg, "user_name", json_string(rdata->user));
json_object_set_new (msg, "ip", json_string(rdata->ip));
if (rdata->client_name) {
json_object_set_new (msg, "user_agent", json_string(rdata->client_name));
} else {
json_object_set_new (msg, "user_agent", json_string(""));
}
json_object_set_new (msg, "repo_id", json_string(rdata->repo_id));
if (rdata->path) {
json_object_set_new (msg, "file_path", json_string(rdata->path));
} else {
json_object_set_new (msg, "file_path", json_string("/"));
}

seaf_mq_manager_publish_event (seaf->mq_mgr, SEAFILE_SERVER_CHANNEL_EVENT, buf->str);
msg_str = json_dumps (msg, JSON_PRESERVE_ORDER);

g_string_free (buf, TRUE);
seaf_mq_manager_publish_event (seaf->mq_mgr, SEAFILE_SERVER_CHANNEL_EVENT, msg_str);

g_free (msg_str);
json_decref (msg);
}

static void
publish_stats_event (StatsEventData *rdata)
{
GString *buf = g_string_new (NULL);
g_string_printf (buf, "%s\t%s\t%s\t%"G_GUINT64_FORMAT,
rdata->etype, rdata->user,
rdata->repo_id, rdata->bytes);
json_t *msg = json_object ();
char *msg_str = NULL;

json_object_set_new (msg, "msg_type", json_string(rdata->etype));
json_object_set_new (msg, "user_name", json_string(rdata->user));
json_object_set_new (msg, "repo_id", json_string(rdata->repo_id));
json_object_set_new (msg, "bytes", json_integer(rdata->bytes));

msg_str = json_dumps (msg, JSON_PRESERVE_ORDER);

seaf_mq_manager_publish_event (seaf->mq_mgr, SEAFILE_SERVER_CHANNEL_STATS, buf->str);
seaf_mq_manager_publish_event (seaf->mq_mgr, SEAFILE_SERVER_CHANNEL_STATS, msg_str);

g_string_free (buf, TRUE);
g_free (msg_str);
json_decref (msg);
}

static void
Expand Down

0 comments on commit 3e579ed

Please sign in to comment.