package com.wb.task;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**
 * 用于执行服务器端脚本任务的代理。
 */
public class ScriptProxy implements Job {
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		long start = System.currentTimeMillis();
		String jobDesc = context.getJobDetail().getDescription();

		try {
			if (com.wb.common.Var.taskLog)
				com.wb.util.LogUtil.info("Start job " + jobDesc);
			JobDataMap dataMap = context.getJobDetail().getJobDataMap();
			String serverScript = dataMap.getString("job.serverScript");
			if (!com.wb.util.StringUtil.isEmpty(serverScript))
				com.wb.common.ScriptBuffer.run(dataMap.getString("job.id"), serverScript,
						context);
			if (com.wb.common.Var.taskLog)
				com.wb.util.LogUtil.info(com.wb.util.StringUtil.concat("Finish job ", jobDesc, " in ",
						com.wb.util.DateUtil.format(System.currentTimeMillis() - start)));
		} catch (Throwable e) {
			if (com.wb.common.Var.taskLog)
				com.wb.util.LogUtil.error(com.wb.util.StringUtil.concat("Execute job ", jobDesc,
						" failed with error ", com.wb.util.SysUtil.getRootError(e), " in ",
						com.wb.util.DateUtil.format(System.currentTimeMillis() - start)));
			if (com.wb.common.Var.printError)
				throw new RuntimeException(e);
		}
	}
}