001// Generated by delombok at Mon Oct 12 22:59:35 BST 2020
002/*
003 *  Licensed to the Apache Software Foundation (ASF) under one
004 *  or more contributor license agreements.  See the NOTICE file
005 *  distributed with this work for additional information
006 *  regarding copyright ownership.  The ASF licenses this file
007 *  to you under the Apache License, Version 2.0 (the
008 *  "License"); you may not use this file except in compliance
009 *  with the License.  You may obtain a copy of the License at
010 *
011 *        http://www.apache.org/licenses/LICENSE-2.0
012 *
013 *  Unless required by applicable law or agreed to in writing,
014 *  software distributed under the License is distributed on an
015 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 *  KIND, either express or implied.  See the License for the
017 *  specific language governing permissions and limitations
018 *  under the License.
019 */
020package org.apache.isis.extensions.commandreplay.secondary.job;
021
022import javax.inject.Inject;
023import org.quartz.DisallowConcurrentExecution;
024import org.quartz.Job;
025import org.quartz.JobExecutionContext;
026import org.quartz.PersistJobDataAfterExecution;
027import org.apache.isis.applib.services.xactn.TransactionService;
028import org.apache.isis.core.runtime.iactn.IsisInteractionFactory;
029import org.apache.isis.core.security.authentication.AuthenticationSession;
030import org.apache.isis.core.security.authentication.standard.SimpleSession;
031import org.apache.isis.extensions.commandreplay.secondary.config.SecondaryConfig;
032import org.apache.isis.extensions.commandreplay.secondary.jobcallables.IsTickingClockInitialized;
033import org.apache.isis.extensions.commandreplay.secondary.jobcallables.ReplicateAndRunCommands;
034import org.apache.isis.extensions.commandreplay.secondary.SecondaryStatus;
035
036@DisallowConcurrentExecution
037@PersistJobDataAfterExecution
038public class ReplicateAndReplayJob implements Job {
039    @java.lang.SuppressWarnings("all")
040    private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(ReplicateAndReplayJob.class);
041    @Inject
042    SecondaryConfig secondaryConfig;
043    AuthenticationSession authSession;
044
045    public void execute(final JobExecutionContext quartzContext) {
046        // figure out if this instance is configured to run as primary or secondary
047        new SecondaryStatusData(quartzContext);
048        if (secondaryConfig.isConfigured()) {
049            authSession = new SimpleSession(secondaryConfig.getPrimaryUser(), secondaryConfig.getQuartzRoles());
050            exec(quartzContext);
051        }
052    }
053
054    @Inject
055    protected IsisInteractionFactory isisInteractionFactory;
056
057    private void exec(final JobExecutionContext quartzContext) {
058        final org.apache.isis.extensions.commandreplay.secondary.job.SecondaryStatusData ssh = new SecondaryStatusData(quartzContext);
059        final org.apache.isis.extensions.commandreplay.secondary.SecondaryStatus secondaryStatus = ssh.getSecondaryStatus(SecondaryStatus.TICKING_CLOCK_STATUS_UNKNOWN);
060        switch (secondaryStatus) {
061        case TICKING_CLOCK_STATUS_UNKNOWN: 
062        case TICKING_CLOCK_NOT_YET_INITIALIZED: 
063            ssh.setSecondaryStatus(isTickingClockInitialized(authSession) ? SecondaryStatus.OK : SecondaryStatus.TICKING_CLOCK_NOT_YET_INITIALIZED);
064            if (ssh.getSecondaryStatus() == SecondaryStatus.OK) {
065                log.info("Ticking clock now initialised");
066            } else {
067                log.info("Still waiting for ticking clock to be initialised: {}", secondaryStatus);
068            }
069            return;
070        case OK: 
071            final org.apache.isis.extensions.commandreplay.secondary.SecondaryStatus newStatus = isisInteractionFactory.callAuthenticated(authSession, new ReplicateAndRunCommands());
072            if (newStatus != null) {
073                ssh.setSecondaryStatus(newStatus);
074            }
075            return;
076        case REST_CALL_FAILING: 
077        case FAILED_TO_UNMARSHALL_RESPONSE: 
078        case UNKNOWN_STATE: 
079            log.warn("skipped - configured as secondary, however: {}", secondaryStatus);
080            return;
081        default: 
082            throw new IllegalStateException("Unrecognised status: " + secondaryStatus);
083        }
084    }
085
086    private boolean isTickingClockInitialized(final AuthenticationSession authSession) {
087        return isisInteractionFactory.callAuthenticated(authSession, new IsTickingClockInitialized());
088    }
089}