tomcat service to start only after the Redis replication is completed

Hi,

We use a single redis master server and many slave nodes that are running tomcat along with the redis slave service.

One of the problems I have is that I need my tomcat application to start when the replication is completed, on my playbook I first install and start redis slave service before tomcat but it seems tomcat service starts before the replication is completed which is not acceptable. How can I have Ansible to check when the redis replication was completed so it can THEN start tomcat ?

I could add a pause command but the replication time is something that will change in the future along with our database, I’m able to check for the replication status using the redis cli command or inspecting the redis log file :

CLI

/usr/bin/redis-cli -a <password> -p <slave_port> info  replication

Log

“”"

[11347] 01 Sep 09:05:54.259 # Server started, Redis version 2.8.13
[11347] 01 Sep 09:06:17.406 * DB loaded from append only file: 23.148 seconds
[11347] 01 Sep 09:06:17.406 * The server is now ready to accept connections on port 6380
[11347] 01 Sep 09:06:18.259 * Connecting to MASTER master-redis[.example.com:6379](http://asset-feed.cogmatch.net:6379/)
[11347] 01 Sep 09:06:18.261 * MASTER <-> SLAVE sync started
[11347] 01 Sep 09:06:18.263 * Non blocking connect for SYNC fired the event.
[11347] 01 Sep 09:06:18.264 * Master replied to PING, replication can continue...
[11347] 01 Sep 09:06:18.264 * Partial resynchronization not possible (no cached master)
[11347] 01 Sep 09:06:18.265 * Full resync from master: 16d5e3d27f324kjdf2d10e5c8dadae90186ab85bd51:72507275921
[11347] 01 Sep 09:06:40.402 * MASTER <-> SLAVE sync: receiving 1672419047 bytes from master
[11347] 01 Sep 09:07:32.165 * MASTER <-> SLAVE sync: Flushing old data
[11347] 01 Sep 09:07:40.012 * MASTER <-> SLAVE sync: Loading DB in memory
[11347] 01 Sep 09:07:59.408 * MASTER <-> SLAVE sync: Finished with success

“”"

Can you please advise ?

Regards,
Nicolas.

Hi Nicolas,

have you tried “wait_for”?

Something like:

`
wait_for: path=/var/log/redis.log search_regex=“Finished with success” state=present

`

Regards,

Frank

Hi Frank, thanks for the reply.

I was not aware of the search_regex thanks you, should be useful in other cases but I don’t thin in my case. The reason is if I need to restart the services (redis/tomcat), tomcat will start immediately as it will find the “Finished with success” from previous boot in the logs.

You may delete or rotate the log file before.

this is not ideal, I found a better solution for my case :

“”"

  • name: check if Redis replication has finished before proceeding
    shell: /usr/bin/redis-cli -a {{ REDIS_PASSWORD }} -p {{ REDIS_SLAVE_PORT }} info replication | grep master_link_status | cut -d":" -f2
    register: result
    until: result.stdout.find(“up”) != -1
    retries: 5
    delay: 10
    “”"

Thanks for the help !