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.user.UserIdentity; 025import org.ametys.plugins.core.impl.schedule.AbstractStaticSchedulable; 026import org.ametys.plugins.core.schedule.Scheduler; 027import org.ametys.runtime.servlet.RuntimeServlet; 028import org.ametys.runtime.servlet.RuntimeServlet.ForcedMainteanceInformations; 029import org.ametys.runtime.servlet.RuntimeServlet.MaintenanceStatus; 030import org.ametys.runtime.servlet.RuntimeServlet.RunMode; 031 032/** 033 * The task to enter or leave the maintenance mode 034 */ 035public class MaintenanceSchedulable extends AbstractStaticSchedulable 036{ 037 /** The key for mode */ 038 public static final String JOBDATAMAP_MODE = "mode"; 039 /** The key for comment */ 040 public static final String JOBDATAMAP_COMMENT = "comment"; 041 042 @Override 043 public void execute(JobExecutionContext context) throws Exception 044 { 045 JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); 046 047 boolean mode = jobDataMap.getBoolean(Scheduler.PARAM_VALUES_PREFIX + JOBDATAMAP_MODE); 048 String comment = StringUtils.defaultString(jobDataMap.getString(Scheduler.PARAM_VALUES_PREFIX + JOBDATAMAP_COMMENT)); 049 UserIdentity userIdentity = UserIdentity.stringToUserIdentity(jobDataMap.getString(Scheduler.KEY_RUNNABLE_USERIDENTITY)); 050 051 if (mode) 052 { 053 getLogger().info("Entering maintenance mode"); 054 055 if (RuntimeServlet.getRunMode() != RunMode.NORMAL) 056 { 057 throw new IllegalStateException("Cannot enter the maintenance mode because the current mode is " + RuntimeServlet.getRunMode()); 058 } 059 060 RuntimeServlet.setMaintenanceStatus(MaintenanceStatus.FORCED, new ForcedMainteanceInformations(comment, userIdentity, ZonedDateTime.now())); 061 RuntimeServlet.setRunMode(RunMode.MAINTENANCE); 062 } 063 else 064 { 065 getLogger().info("Leaving maintenance mode"); 066 067 if (RuntimeServlet.getRunMode() != RunMode.MAINTENANCE || RuntimeServlet.getMaintenanceStatus() == MaintenanceStatus.AUTOMATIC) 068 { 069 throw new IllegalStateException("Cannot leave the maintenance mode because the current mode is " + RuntimeServlet.getRunMode() + " and maintenance status " + RuntimeServlet.getMaintenanceStatus()); 070 } 071 072 RuntimeServlet.setMaintenanceStatus(MaintenanceStatus.NONE, null); 073 RuntimeServlet.setRunMode(RunMode.NORMAL); 074 } 075 } 076}