001/*
002 *  Copyright 2023 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.runtime.plugins.admin.system;
017
018import java.time.ZonedDateTime;
019
020import org.apache.commons.lang3.StringUtils;
021import org.quartz.JobDataMap;
022import org.quartz.JobExecutionContext;
023
024import org.ametys.core.schedule.progression.ContainerProgressionTracker;
025import org.ametys.core.user.UserIdentity;
026import org.ametys.plugins.core.impl.schedule.AbstractStaticSchedulable;
027import org.ametys.plugins.core.schedule.Scheduler;
028import org.ametys.runtime.servlet.RuntimeServlet;
029import org.ametys.runtime.servlet.RuntimeServlet.ForcedMainteanceInformations;
030import org.ametys.runtime.servlet.RuntimeServlet.MaintenanceStatus;
031import org.ametys.runtime.servlet.RuntimeServlet.RunMode;
032
033/**
034 * The task to enter or leave the maintenance mode 
035 */
036public class MaintenanceSchedulable extends AbstractStaticSchedulable
037{
038    /** The key for mode */
039    public static final String JOBDATAMAP_MODE = "mode";
040    /** The key for comment */
041    public static final String JOBDATAMAP_COMMENT = "comment";
042    
043    @Override
044    public void execute(JobExecutionContext context, ContainerProgressionTracker progressionTracker) throws Exception
045    {
046        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
047        
048        boolean mode = jobDataMap.getBoolean(Scheduler.PARAM_VALUES_PREFIX + JOBDATAMAP_MODE);
049        String comment = StringUtils.defaultString(jobDataMap.getString(Scheduler.PARAM_VALUES_PREFIX + JOBDATAMAP_COMMENT));
050        UserIdentity userIdentity = UserIdentity.stringToUserIdentity(jobDataMap.getString(Scheduler.KEY_RUNNABLE_USERIDENTITY));
051        
052        if (mode)
053        {
054            getLogger().info("Entering maintenance mode");
055            
056            if (RuntimeServlet.getRunMode() != RunMode.NORMAL)
057            {
058                throw new IllegalStateException("Cannot enter the maintenance mode because the current mode is " + RuntimeServlet.getRunMode());
059            }
060            
061            RuntimeServlet.setMaintenanceStatus(MaintenanceStatus.FORCED, new ForcedMainteanceInformations(comment, userIdentity, ZonedDateTime.now()));
062            RuntimeServlet.setRunMode(RunMode.MAINTENANCE);
063        }
064        else
065        {
066            getLogger().info("Leaving maintenance mode");
067            
068            if (RuntimeServlet.getRunMode() != RunMode.MAINTENANCE || RuntimeServlet.getMaintenanceStatus() == MaintenanceStatus.AUTOMATIC)
069            {
070                throw new IllegalStateException("Cannot leave the maintenance mode because the current mode is " + RuntimeServlet.getRunMode() + " and maintenance status " + RuntimeServlet.getMaintenanceStatus());
071            }
072            
073            RuntimeServlet.setMaintenanceStatus(MaintenanceStatus.NONE, null);
074            RuntimeServlet.setRunMode(RunMode.NORMAL);
075        }
076    }
077}