using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using HeuristicLab.Services.Hive.Common.ServiceContracts; using HeuristicLab.Services.Hive.Common.DataTransfer; using System.IO; using System.Security.Permissions; using System.Data.Linq; using HeuristicLab.Services.Hive.Common; using System.Transactions; namespace HeuristicLab.Services.Hive { /// /// Implementation of the Hive service (interface ). /// [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)] public class HiveService : IHiveService { private DataAccess.IHiveDao dao { get { return ServiceLocator.Instance.HiveDao; } } private HeuristicLab.Services.Hive.DataAccess.TransactionManager trans { get { return ServiceLocator.Instance.TransactionManager; } } private IAuthorizationManager auth { get { return ServiceLocator.Instance.AuthorizationManager; } } private ILifecycleManager lifecycleManager { get { return ServiceLocator.Instance.LifecycleManager; } } #region Job Methods //[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)] //[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)] public Guid AddJob(Job job, JobData jobData) { using (trans.OpenTransaction()) { jobData.JobId = dao.AddJob(job); dao.AddJobData(jobData); return jobData.JobId; } } public Guid AddChildJob(Guid parentJobId, Job job, JobData jobData) { using (trans.OpenTransaction()) { job.ParentJobId = parentJobId; return AddJob(job, jobData); } } public Job GetJob(Guid jobId) { return dao.GetJob(jobId); } public IEnumerable GetJobs() { return dao.GetJobs(x => true); } public IEnumerable GetLightweightJobs(IEnumerable jobIds) { return dao.GetJobs(x => jobIds.Contains(x.JobId)).Select(x => new LightweightJob(x)).ToArray(); } public IEnumerable GetLightweightChildJobs(Guid? parentJobId, bool recursive, bool includeParent) { return GetChildJobs(parentJobId, recursive, includeParent).Select(x => new LightweightJob(x)).ToArray(); } // formerly GetLastSerializedResult public JobData GetJobData(Guid jobId) { return dao.GetJobData(jobId); } public Stream GetJobDataStreamed(Guid jobId) { throw new NotImplementedException(); } public void UpdateJob(Job jobDto, JobData jobDataDto) { using (trans.OpenTransaction()) { dao.UpdateJob(jobDto); dao.UpdateJobData(jobDataDto); } } public void UpdateJobDataStreamed(Stream stream) { using (trans.OpenTransaction()) { throw new NotImplementedException(); } } public void DeleteChildJobs(Guid jobId) { using (trans.OpenTransaction()) { var jobs = GetChildJobs(jobId, true, false); foreach (var job in jobs) { dao.DeleteJob(job.Id); dao.DeleteJobData(job.Id); }; } } public Job AquireJob(Guid slaveId) { using (trans.OpenTransaction()) { var slave = dao.GetSlave(slaveId); var availableJobs = dao.GetWaitingJobs(slave); var job = availableJobs.FirstOrDefault(); if (job != null) { job.SlaveId = slaveId; job.JobState = JobState.Calculating; } return job; } } #endregion #region Job Control Methods public void StopJob(Guid jobId) { using (trans.OpenTransaction()) { throw new NotImplementedException(); } } public void PauseJob(Guid jobId) { using (trans.OpenTransaction()) { throw new NotImplementedException(); } } #endregion #region HiveExperiment Methods public HiveExperiment GetHiveExperiment(Guid id) { return dao.GetHiveExperiments(x => x.UserId == auth.UserId && x.HiveExperimentId == id).FirstOrDefault(); } public IEnumerable GetHiveExperiments() { return dao.GetHiveExperiments(x => x.UserId == auth.UserId); } public Guid AddHiveExperiment(HiveExperiment hiveExperimentDto) { using (trans.OpenTransaction()) { hiveExperimentDto.UserId = auth.UserId; return dao.AddHiveExperiment(hiveExperimentDto); } } public void UpdateHiveExperiment(HiveExperiment hiveExperimentDto) { using (trans.OpenTransaction()) { dao.UpdateHiveExperiment(hiveExperimentDto); } } public void DeleteHiveExperiment(Guid hiveExperimentId) { using (trans.OpenTransaction()) { HiveExperiment he = dao.GetHiveExperiment(hiveExperimentId); if (he.RootJobId.HasValue) { var jobs = GetChildJobs(he.RootJobId.Value, true, true); foreach (var j in jobs) { dao.DeleteJobData(j.Id); dao.DeleteJob(j.Id); } } dao.DeleteHiveExperiment(hiveExperimentId); } } #endregion #region Login Methods public void Hello(Guid slaveId, string name, int cores, int memory) { throw new NotImplementedException(); } public void GoodBye() { throw new NotImplementedException(); } #endregion #region Heartbeat Methods public List Heartbeat(Heartbeat heartbeat) { using (trans.OpenTransaction()) { return lifecycleManager.ProcessHeartbeat(heartbeat); } } #endregion #region Plugin Methods public Guid AddPlugin(Plugin plugin, List pluginDatas) { using (trans.OpenTransaction()) { Guid pluginId = dao.AddPlugin(plugin); foreach (PluginData pluginData in pluginDatas) { pluginData.PluginId = pluginId; dao.AddPluginData(pluginData); } return pluginId; } } public IEnumerable GetPlugins() { return dao.GetPlugins(x => true); } public IEnumerable GetPluginDatas(List pluginIds) { throw new NotImplementedException(); } public Stream GetStreamedPluginDatas(List pluginIds) { throw new NotImplementedException(); } #endregion #region Slave Methods public Guid AddSlave(Slave slave) { using (trans.OpenTransaction()) { return dao.AddSlave(slave); } } public Guid AddSlaveGroup(SlaveGroup slaveGroup) { using (trans.OpenTransaction()) { return dao.AddSlaveGroup(slaveGroup); } } public IEnumerable GetSlaves() { return dao.GetSlaves(x => true); } public IEnumerable GetSlaveGroups() { return dao.GetSlaveGroups(x => true); } public void DeleteSlaveGroup(Guid slaveGroupId) { using (trans.OpenTransaction()) { dao.DeleteSlaveGroup(slaveGroupId); } } public void AddResourceToGroup(Guid slaveGroupId, Resource resource) { using (trans.OpenTransaction()) { throw new NotImplementedException(); } } public void RemoveResourceFromGroup(Guid clientGroupId, Guid resourceId) { using (trans.OpenTransaction()) { throw new NotImplementedException(); } } public void UpdateSlave(Slave slave) { using (trans.OpenTransaction()) { dao.UpdateSlave(slave); } } #endregion #region Helper Methods private IEnumerable GetChildJobs(Guid? parentJobId, bool recursive, bool includeParent) { var jobs = new List(dao.GetJobs(x => parentJobId == null ? !x.ParentJobId.HasValue : x.ParentJobId.Value == parentJobId)); if (includeParent) { jobs.Add(GetJob(parentJobId.Value)); } if (recursive) { var childs = new List(); foreach (var job in jobs) { childs.AddRange(GetChildJobs(job.Id, recursive, false)); } jobs.AddRange(childs); } return jobs; } #endregion } }