How to redirect the output of redis subscription

假如想象 提交于 2020-01-14 02:18:35

问题


I am exploring redis to do pub/sub. I wanted to write a script that uses redis-cli to subscribe to a channel and dump whatever is published to a file. What I notice however is that redis-cli subscripe channel > output does not quite work.

I'd appreciate any help very much.

Regards, Kashyap


回答1:


This is because there is no automatic flush of stdout when redis-cli displays the messages associated to the subscription. So the last messages before stopping redis-cli do not appear in the output file.

There is no option you can use to enforce a systematic flush, redis-cli.c needs to be patched. In Redis source code, edit src/redis-cli.c, and find the following piece of code. Add the missing fflush line.

    if (config.pubsub_mode) {
        if (config.output != OUTPUT_RAW)
            printf("Reading messages... (press Ctrl-C to quit)\n");
        while (1) {
            if (cliReadReply(output_raw) != REDIS_OK) exit(1);
            // The following line must be added
            fflush(stdout);
        }
    }

Once redis-cli has been compiled again, it should work as expected.



来源:https://stackoverflow.com/questions/17922809/how-to-redirect-the-output-of-redis-subscription

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!