001package ball.spring.mysqld;
002/*-
003 * ##########################################################################
004 * Reusable Spring Components
005 * %%
006 * Copyright (C) 2018 - 2022 Allen D. Ball
007 * %%
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *      http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 * ##########################################################################
020 */
021import lombok.NoArgsConstructor;
022import lombok.ToString;
023import lombok.extern.log4j.Log4j2;
024import org.springframework.beans.factory.annotation.Autowired;
025import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
026import org.springframework.scheduling.annotation.Scheduled;
027import org.springframework.stereotype.Component;
028
029import static java.util.concurrent.TimeUnit.SECONDS;
030
031/**
032 * {@code mysqld} {@link Component}.
033 *
034 * {@injected.fields}
035 *
036 * @see MysqldConfiguration
037 *
038 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
039 */
040@Component
041@ConditionalOnBean(name = { "mysqld" })
042@NoArgsConstructor @ToString @Log4j2
043public class MysqldComponent {
044    @Autowired private Process mysqld;
045
046    @Scheduled(fixedRate = 15 * 1000)
047    public void run() {
048        if (mysqld != null) {
049            if (mysqld.isAlive()) {
050                try {
051                    mysqld.waitFor(15, SECONDS);
052                } catch (InterruptedException exception) {
053                }
054            } else {
055                throw new IllegalStateException("mysqld is not running");
056            }
057        }
058    }
059}