001/*
002 * Stallion Core: A Modern Web Framework
003 *
004 * Copyright (C) 2015 - 2016 Stallion Software LLC.
005 *
006 * This program is free software: you can redistribute it and/or modify it under the terms of the
007 * GNU General Public License as published by the Free Software Foundation, either version 2 of
008 * the License, or (at your option) any later version. This program is distributed in the hope that
009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
011 * License for more details. You should have received a copy of the GNU General Public License
012 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
013 *
014 *
015 *
016 */
017
018package io.stallion.boot;
019
020import io.stallion.Context;
021import io.stallion.asyncTasks.AsyncCoordinator;
022import io.stallion.asyncTasks.AsyncTaskController;
023import io.stallion.jobs.JobCoordinator;
024import io.stallion.plugins.PluginRegistry;
025import io.stallion.requests.RequestHandler;
026import io.stallion.restfulEndpoints.EndpointsRegistry;
027import io.stallion.restfulEndpoints.SlugRegistry;
028import io.stallion.services.Log;
029import io.stallion.settings.Settings;
030import org.eclipse.jetty.server.Server;
031import sun.misc.Signal;
032
033import java.text.MessageFormat;
034
035import static io.stallion.utils.Literals.*;
036import static io.stallion.Context.*;
037
038
039public class ForceTaskAction implements StallionRunAction<ForceActionOptions> {
040
041    @Override
042    public String getActionName() {
043        return "force-action";
044    }
045
046    @Override
047    public String getHelp() {
048        return "Force run a particular task or a particular job, regardless of whether it is scheduled to run or not.";
049    }
050
051    @Override
052    public void loadApp(ForceActionOptions options) {
053        AppContextLoader.loadCompletely(options);
054        AppContextLoader.instance().startAllServices();
055    }
056
057    @Override
058    public ForceActionOptions newCommandOptions() {
059        return new ForceActionOptions();
060    }
061
062    @Override
063    public void execute(ForceActionOptions options) throws Exception {
064
065        if (options.getTaskId() > 0L) {
066            Log.info("Run AsyncTask with id {0}", options.getTaskId());
067            AsyncCoordinator.instance().runTaskForId(options.getTaskId(), options.getForce());
068            return;
069        }
070
071        if (!empty(options.getJobName())) {
072            Log.info("Force run job with name {0}", options.getJobName());
073            JobCoordinator.instance().forceRunJob(options.getJobName(), options.getForce());
074        }
075
076        AsyncCoordinator.gracefulShutdown();
077        JobCoordinator.shutdown();
078    }
079}