com.cip.crane.agent.sheduler.KillTaskThread.java Source code

Java tutorial

Introduction

Here is the source code for com.cip.crane.agent.sheduler.KillTaskThread.java

Source

/**
 * Project: taurus-agent
 * 
 * File Created at 2013-5-21
 * $Id$
 * 
 * Copyright 2013 dianping.com.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Dianping Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with dianping.com.
 */
package com.cip.crane.agent.sheduler;

import com.cip.crane.agent.common.BaseEnvManager;
import com.cip.crane.agent.exec.Executor;
import com.cip.crane.agent.utils.LockHelper;
import com.cip.crane.zookeeper.common.infochannel.bean.ScheduleConf;
import com.cip.crane.zookeeper.common.infochannel.bean.ScheduleStatus;
import com.cip.crane.zookeeper.common.infochannel.interfaces.ScheduleInfoChannel;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.concurrent.locks.Lock;

/**
 * TODO Comment of KillThread
 * 
 * @author renyuan.sun
 * 
 */
public class KillTaskThread extends BaseEnvManager {

    private static final Log LOGGER = LogFactory.getLog(KillTaskThread.class);

    private static final String KILL_COMMAND = "%s %s %s";

    Executor executor;

    String localIp;

    ScheduleInfoChannel cs;

    String jobInstanceId;

    KillTaskThread(Executor executor, String localIp, ScheduleInfoChannel cs, String jobInstanceId) {
        this.executor = executor;
        this.localIp = localIp;
        this.cs = cs;
        this.jobInstanceId = jobInstanceId;
    }

    @Override
    public void run() {
        Lock lock = LockHelper.getLock(jobInstanceId);
        try {
            lock.lock();

            ScheduleConf conf = (ScheduleConf) cs.getConf(localIp, jobInstanceId);

            ScheduleStatus status = (ScheduleStatus) cs.getStatus(localIp, jobInstanceId);

            LOGGER.info("KILL TASK THREAD|jobInstanceId:" + jobInstanceId + "| ip:" + localIp + "  |conf:" + conf
                    + "  |status:" + status);

            if (conf != null && status != null) {
                killTask(localIp, conf, status);
            }

        } catch (Exception e) {
            LOGGER.error(e, e);
        } finally {
            lock.unlock();
        }
    }

    private void killTask(String ip, ScheduleConf conf, ScheduleStatus status) {
        String attemptID = conf.getAttemptID();
        int returnCode = 1;
        try {
            if (ON_WINDOWS) {
                returnCode = executor.kill(attemptID);
            } else {
                String fileName = running + FILE_SEPRATOR + '.' + attemptID;
                BufferedReader br = new BufferedReader(new FileReader((new File(fileName))));
                String pid = br.readLine();
                br.close();
                LOGGER.debug("Ready to kill " + attemptID + ", pid is " + pid);
                String killCMD = "sh " + killJob;
                String kill = String.format("%s %s %s", killCMD, pid, "9");
                returnCode = executor.execute("kill", System.out, System.err, kill);
                try {
                    new File(fileName).delete();
                } catch (Exception e) {
                    LOGGER.error("Fail to delete file for " + attemptID);
                }
            }
        } catch (FileNotFoundException e) {
            LOGGER.debug(attemptID + " is over when been killed.");
            returnCode = 0;
        } catch (Exception e) {
            LOGGER.error(e, e);
            returnCode = 1;
        }

        if (returnCode == 0) {
            //TODO ?/delete,?
            cs.removeRunningJob(localIp, jobInstanceId);
            status.setStatus(ScheduleStatus.DELETE_SUCCESS);
        }
    }

}