Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

job run error #2600

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package com.taobao.arthas.core.shell.command.internal;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import com.taobao.arthas.core.util.LogUtil;

import java.io.*;

/**
* 重定向处理类
*
Expand Down Expand Up @@ -59,7 +55,15 @@ public void close() {
}
}

/**
* 获取 job 重定向文件的地址
*
* @return
*/
public String getFilePath() {
return file.getAbsolutePath();
if (file != null) {
return file.getAbsolutePath();
}
return LogUtil.cacheDir() + File.separator + "result.log";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public Job createJob(InternalCommandManager commandManager, List<CliToken> token
line.append(arg.raw());
}
boolean runInBackground = runInBackground(tokens);
Process process = createProcess(session, tokens, commandManager, jobId, term, resultDistributor);
Process process = createProcess(session, tokens, commandManager, jobId, term, resultDistributor,runInBackground);
process.setJobId(jobId);
JobImpl job = new JobImpl(jobId, this, process, line.toString(), runInBackground, session, jobHandler);
jobs.put(jobId, job);
Expand Down Expand Up @@ -141,9 +141,10 @@ public void handle(Future<Void> v) {
* @param jobId job id
* @param term term
* @param resultDistributor
* @param runInBackground
* @return the created process
*/
private Process createProcess(Session session, List<CliToken> line, InternalCommandManager commandManager, int jobId, Term term, ResultDistributor resultDistributor) {
private Process createProcess(Session session, List<CliToken> line, InternalCommandManager commandManager, int jobId, Term term, ResultDistributor resultDistributor,boolean runInBackground) {
try {
ListIterator<CliToken> tokens = line.listIterator();
while (tokens.hasNext()) {
Expand All @@ -153,7 +154,7 @@ private Process createProcess(Session session, List<CliToken> line, InternalComm
checkPermission(session, token);
Command command = commandManager.getCommand(token.value());
if (command != null) {
return createCommandProcess(command, tokens, jobId, term, resultDistributor);
return createCommandProcess(command, tokens, jobId, term, resultDistributor,runInBackground);
} else {
throw new IllegalArgumentException(token.value() + ": command not found");
}
Expand All @@ -175,13 +176,12 @@ private boolean runInBackground(List<CliToken> tokens) {
return runInBackground;
}

private Process createCommandProcess(Command command, ListIterator<CliToken> tokens, int jobId, Term term, ResultDistributor resultDistributor) throws IOException {
private Process createCommandProcess(Command command, ListIterator<CliToken> tokens, int jobId, Term term, ResultDistributor resultDistributor,boolean runInBackground) throws IOException {
List<CliToken> remaining = new ArrayList<CliToken>();
List<CliToken> pipelineTokens = new ArrayList<CliToken>();
boolean isPipeline = false;
RedirectHandler redirectHandler = null;
List<Function<String, String>> stdoutHandlerChain = new ArrayList<Function<String, String>>();
String cacheLocation = null;
while (tokens.hasNext()) {
CliToken remainingToken = tokens.next();
if (remainingToken.isText()) {
Expand All @@ -196,8 +196,6 @@ private Process createCommandProcess(Command command, ListIterator<CliToken> tok
if (name == null) {
// 如果没有指定重定向文件名,那么重定向到以jobid命名的缓存中
name = LogUtil.cacheDir() + File.separator + Constants.PID + File.separator + jobId;
cacheLocation = name;

if (getRedirectJobCount() == 8) {
throw new IllegalStateException("The amount of async command that saving result to file can't > 8");
}
Expand All @@ -215,13 +213,21 @@ private Process createCommandProcess(Command command, ListIterator<CliToken> tok
injectHandler(stdoutHandlerChain, pipelineTokens);
if (redirectHandler != null) {
stdoutHandlerChain.add(redirectHandler);
term.write("redirect output file will be: " + redirectHandler.getFilePath()+"\n");
} else if (runInBackground) {
redirectHandler = new RedirectHandler();
stdoutHandlerChain.add(redirectHandler);
} else {
stdoutHandlerChain.add(new TermHandler(term));
if (GlobalOptions.isSaveResult) {
stdoutHandlerChain.add(new RedirectHandler());
RedirectHandler saveResultRedirectHandler = new RedirectHandler();
stdoutHandlerChain.add(saveResultRedirectHandler);
term.write("save result job output file location: " + saveResultRedirectHandler.getFilePath() + "\n");
}
}
String cacheLocation = null;
if(redirectHandler !=null){
cacheLocation = redirectHandler.getFilePath();
}
ProcessOutput ProcessOutput = new ProcessOutput(stdoutHandlerChain, cacheLocation, term);
ProcessImpl process = new ProcessImpl(command, remaining, command.processHandler(), ProcessOutput, resultDistributor);
process.setTty(term);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ public synchronized void run(boolean fg) {

if (cacheLocation() != null) {
process.echoTips("job id : " + this.jobId + "\n");
process.echoTips("cache location : " + cacheLocation() + "\n");
process.echoTips("job output file location : " + cacheLocation() + "\n");
}
Runnable task = new CommandProcessTask(process);
ArthasBootstrap.getInstance().execute(task);
Expand Down