001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     * 
010     *      http://www.apache.org/licenses/LICENSE-2.0
011     * 
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.oozie.command.coord;
019    
020    import java.io.IOException;
021    import java.io.StringReader;
022    import java.net.HttpURLConnection;
023    import java.net.URL;
024    
025    import org.apache.hadoop.conf.Configuration;
026    import org.apache.oozie.CoordinatorActionBean;
027    import org.apache.oozie.ErrorCode;
028    import org.apache.oozie.client.OozieClient;
029    import org.apache.oozie.command.CommandException;
030    import org.apache.oozie.command.PreconditionException;
031    import org.apache.oozie.util.LogUtils;
032    import org.apache.oozie.util.ParamChecker;
033    import org.apache.oozie.util.XConfiguration;
034    import org.apache.oozie.util.XLog;
035    
036    /**
037     * This class will send the notification for the coordinator action
038     */
039    public class CoordActionNotificationXCommand extends CoordinatorXCommand<Void> {
040    
041        private final CoordinatorActionBean actionBean;
042        private static final String STATUS_PATTERN = "\\$status";
043        private static final String ACTION_ID_PATTERN = "\\$actionId";
044    
045        private int retries = 0;
046    
047        public CoordActionNotificationXCommand(CoordinatorActionBean actionBean) {
048            super("coord_action_notification", "coord_action_notification", 0);
049            ParamChecker.notNull(actionBean, "Action Bean");
050            this.actionBean = actionBean;
051        }
052    
053        /* (non-Javadoc)
054         * @see org.apache.oozie.command.XCommand#execute()
055         */
056        @Override
057        protected Void execute() throws CommandException {
058            LOG.info("STARTED Coordinator Notification actionId=" + actionBean.getId() + " : " + actionBean.getStatus());
059            Configuration conf;
060            try {
061                conf = new XConfiguration(new StringReader(actionBean.getRunConf()));
062            }
063            catch (IOException e1) {
064                LOG.warn("Configuration parse error. read from DB :" + actionBean.getRunConf());
065                throw new CommandException(ErrorCode.E1005, e1.getMessage(), e1);
066            }
067            String url = conf.get(OozieClient.COORD_ACTION_NOTIFICATION_URL);
068            if (url != null) {
069                url = url.replaceAll(ACTION_ID_PATTERN, actionBean.getId());
070                url = url.replaceAll(STATUS_PATTERN, actionBean.getStatus().toString());
071                LOG.debug("Notification URL :" + url);
072                try {
073                    URL urlObj = new URL(url);
074                    HttpURLConnection urlConn = (HttpURLConnection) urlObj.openConnection();
075                    if (urlConn.getResponseCode() != HttpURLConnection.HTTP_OK) {
076                        handleRetry(url);
077                    }
078                }
079                catch (IOException ex) {
080                    handleRetry(url);
081                }
082            }
083            else {
084                LOG.info("No Notification URL is defined. Therefore nothing to notify for job " + actionBean.getJobId()
085                        + " action ID " + actionBean.getId());
086            }
087            LOG.info("ENDED Coordinator Notification actionId=" + actionBean.getId());
088            return null;
089        }
090    
091        /**
092         * This method handles the retry for the coordinator action.
093         *
094         * @param url This is the URL where the notification has to be sent.
095         */
096        private void handleRetry(String url) {
097            if (retries < 3) {
098                retries++;
099                this.resetUsed();
100                queue(this, 60 * 1000);
101            }
102            else {
103                LOG.warn(XLog.OPS, "could not send notification [{0}]", url);
104            }
105        }
106    
107        /* (non-Javadoc)
108         * @see org.apache.oozie.command.XCommand#getEntityKey()
109         */
110        @Override
111        public String getEntityKey() {
112            return actionBean.getId();
113        }
114    
115        /* (non-Javadoc)
116         * @see org.apache.oozie.command.XCommand#isLockRequired()
117         */
118        @Override
119        protected boolean isLockRequired() {
120            return false;
121        }
122    
123        /* (non-Javadoc)
124         * @see org.apache.oozie.command.XCommand#loadState()
125         */
126        @Override
127        protected void loadState() throws CommandException {
128            LogUtils.setLogInfo(actionBean, logInfo);
129        }
130    
131        /* (non-Javadoc)
132         * @see org.apache.oozie.command.XCommand#verifyPrecondition()
133         */
134        @Override
135        protected void verifyPrecondition() throws CommandException, PreconditionException {
136        }
137    }