<commit_before>/* postal-redis.c
 *
 * Copyright (C) 2012 Catch.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <redis-glib.h>

#include "postal-debug.h"
#include "postal-device.h"
#include "postal-redis.h"

G_DEFINE_TYPE(PostalRedis, postal_redis, NEO_TYPE_SERVICE_BASE)

struct _PostalRedisPrivate
{
   gchar       *channel;
   RedisClient *redis;
   gchar       *host;
   guint        port;
};

PostalRedis *
postal_redis_new (void)
{
   return g_object_new(POSTAL_TYPE_REDIS,
                       "name", "redis",
                       NULL);
}

static void
postal_redis_set_channel (PostalRedis *redis,
                          const gchar *channel)
{
   g_free(redis->priv->channel);
   redis->priv->channel = g_strdup(channel);
}

static void
postal_redis_set_host (PostalRedis *redis,
                       const gchar *host)
{
   g_free(redis->priv->host);
   redis->priv->host = g_strdup(host);
}

static void
postal_redis_set_port (PostalRedis *redis,
                        guint        port)
{
   redis->priv->port = port;
}

static void
postal_redis_connect_cb (GObject      *object,
                         GAsyncResult *result,
                         gpointer      user_data)
{
   PostalRedisPrivate *priv;
   RedisClient *client = (RedisClient *)object;
   PostalRedis *redis = user_data;
   GError *error = NULL;

   ENTRY;

   g_assert(POSTAL_IS_REDIS(redis));

   priv = redis->priv;

   if (!redis_client_connect_finish(client, result, &error)) {
      g_warning("Failed to connect to Redis: %s", error->message);
      g_error_free(error);
      /*
       * TODO: Try again with exponential timeout or patch redis-glib
       *       to handle connecting on its own.
       */
      EXIT;
   }

   g_message("Connected to redis at %s:%u", priv->host, priv->port);

   EXIT;
}

static void
postal_redis_start (NeoServiceBase *service,
                    GKeyFile       *config)
{
   PostalRedisPrivate *priv;
   PostalRedis *redis = (PostalRedis *)service;
   gchar *str;
   guint port;

   ENTRY;

   g_assert(POSTAL_IS_REDIS(redis));

   priv = redis->priv;

   if (config) {
      if (g_key_file_get_boolean(config, "redis", "enabled", NULL)) {
         str = g_key_file_get_string(config, "redis", "host", NULL);
         postal_redis_set_host(redis, str);
         g_free(str);

         str = g_key_file_get_string(config, "redis", "channel", NULL);
         str = str ?: g_strdup("localhost");
         postal_redis_set_channel(redis, str);
         g_free(str);

         port = g_key_file_get_integer(config, "redis", "port", NULL);
         postal_redis_set_port(redis, port ?: 6379);

         g_clear_object(&priv->redis);

         if (priv->host && priv->port) {
            priv->redis = redis_client_new();
            redis_client_connect_async(priv->redis,
                                       priv->host,<commit_msg>redis: add connection start message.<commit_after>/* postal-redis.c
 *
 * Copyright (C) 2012 Catch.<EMAIL>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <redis-glib.h>

#include "postal-debug.h"
#include "postal-device.h"
#include "postal-redis.h"

G_DEFINE_TYPE(PostalRedis, postal_redis, NEO_TYPE_SERVICE_BASE)

struct _PostalRedisPrivate
{
   gchar       *channel;
   RedisClient *redis;
   gchar       *host;
   guint        port;
};

PostalRedis *
postal_redis_new (void)
{
   return g_object_new(POSTAL_TYPE_REDIS,
                       "name", "redis",
                       NULL);
}

static void
postal_redis_set_channel (PostalRedis *redis,
                          const gchar *channel)
{
   g_free(redis->priv->channel);
   redis->priv->channel = g_strdup(channel);
}

static void
postal_redis_set_host (PostalRedis *redis,
                       const gchar *host)
{
   g_free(redis->priv->host);
   redis->priv->host = g_strdup(host);
}

static void
postal_redis_set_port (PostalRedis *redis,
                        guint        port)
{
   redis->priv->port = port;
}

static void
postal_redis_connect_cb (GObject      *object,
                         GAsyncResult *result,
                         gpointer      user_data)
{
   PostalRedisPrivate *priv;
   RedisClient *client = (RedisClient *)object;
   PostalRedis *redis = user_data;
   GError *error = NULL;

   ENTRY;

   g_assert(POSTAL_IS_REDIS(redis));

   priv = redis->priv;

   if (!redis_client_connect_finish(client, result, &error)) {
      g_warning("Failed to connect to Redis: %s", error->message);
      g_error_free(error);
      /*
       * TODO: Try again with exponential timeout or patch redis-glib
       *       to handle connecting on its own.
       */
      EXIT;
   }

   g_message("Connected to redis at %s:%u", priv->host, priv->port);

   EXIT;
}

static void
postal_redis_start (NeoServiceBase *service,
                    GKeyFile       *config)
{
   PostalRedisPrivate *priv;
   PostalRedis *redis = (PostalRedis *)service;
   gchar *str;
   guint port;

   ENTRY;

   g_assert(POSTAL_IS_REDIS(redis));

   priv = redis->priv;

   if (config) {
      if (g_key_file_get_boolean(config, "redis", "enabled", NULL)) {
         str = g_key_file_get_string(config, "redis", "host", NULL);
         postal_redis_set_host(redis, str);
         g_free(str);

         str = g_key_file_get_string(config, "redis", "channel", NULL);
         str = str ?: g_strdup("localhost");
         postal_redis_set_channel(redis, str);
         g_free(str);

         port = g_key_file_get_integer(config, "redis", "port", NULL);
         postal_redis_set_port(redis, port ?: 6379);

         g_clear_object(&priv->redis);

         if (priv->host && priv->port) {
            priv->redis = redis_client_new();
            g_message("Connecting to redis server at %s:%u",
                      priv->host, priv->port);
            redis_client_connect_async(priv->redis,
                                       priv->host,
{'fraction_non_alphanumeric': 0.10488043630261502, 'fraction_numerical': 0.0025171304712627604, 'mean_word_length': 2.5724275724275723, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4784817', 'n_tokens_mistral': 2380, 'n_tokens_neox': 2306, 'n_words': 595}