package org.gcube.dataanalysis.dataminer.poolmanager.service;

import java.io.File;
import java.io.FileOutputStream;
***REMOVED***
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.LinkedList;
***REMOVED***
import java.util.UUID;

import org.gcube.dataanalysis.dataminer.poolmanager.ansible.AnsibleWorker;
import org.gcube.dataanalysis.dataminer.poolmanager.ansiblebridge.AnsibleBridge;
***REMOVED***
import org.gcube.dataanalysis.dataminer.poolmanager.datamodel.Cluster;
import org.gcube.dataanalysis.dataminer.poolmanager.datamodel.Dependency;
import org.gcube.dataanalysis.dataminer.poolmanager.util.SVNUpdater;
***REMOVED***


public abstract class DMPMJob ***REMOVED***
	
	
	protected SVNUpdater svnUpdater;
	protected File jobLogs;
	
	protected String id;
	
	public DMPMJob(SVNUpdater svnUpdater)***REMOVED***
		this.svnUpdater = svnUpdater;
		this.id = UUID.randomUUID().toString();
		
		***REMOVED***TODO: dmpm work directory should be loaded from configuration file
		this.jobLogs = new File(System.getProperty("user.home")+File.separator+"dataminer-pool-manager"+File.separator+"jobs");
	
		this.jobLogs.mkdirs();
***REMOVED***

	
	public String start()***REMOVED***
		new Thread(new Runnable() ***REMOVED***
	***REMOVED***
			public void run() ***REMOVED***
				try ***REMOVED***
					execute();
			***REMOVED*** catch (Exception e) ***REMOVED***
					e.printStackTrace();
			***REMOVED***
		***REMOVED***
	***REMOVED***).start();
		
		return this.id;
***REMOVED***
	
	protected AnsibleWorker createWorker(Algorithm algo,
			Cluster dataminerCluster,
			boolean includeAlgorithmDependencies,
			String user)***REMOVED***
		AnsibleBridge ansibleBridge = new AnsibleBridge();
		try ***REMOVED***
			return ansibleBridge.createWorker(algo, dataminerCluster, includeAlgorithmDependencies, user);
	***REMOVED*** catch (IOException e) ***REMOVED***
			e.printStackTrace();
	***REMOVED***
***REMOVED***
		
***REMOVED***
	
	protected abstract void execute();
	
	protected int executeAnsibleWorker(AnsibleWorker worker) throws IOException, InterruptedException, SVNException***REMOVED***
		File path = new File(worker.getWorkdir() + File.separator + "jobs");
		path.mkdirs();
		
		File n = new File(this.jobLogs + File.separator + this.id);
		FileOutputStream fos = new FileOutputStream(n, true);
		PrintStream ps = new PrintStream(fos);	
		
***REMOVED***		File m = new File(this.jobLogs + File.separator + this.id + "_exitStatus");
***REMOVED***		PrintWriter fos2 = new PrintWriter(m, "UTF-8");
				
		return worker.execute(ps);
***REMOVED***

***REMOVED***
