public Guid SendJob(Base activity) { if (_ConnectionToManager == null || !_ConnectionToManager.IsConnected()) throw new Exception("Not connected to the manager"); while (true) { BinaryFormatter binaryFormatter = new BinaryFormatter(); MemoryStream datastream = new MemoryStream(); binaryFormatter.Serialize(datastream, activity); Packet p = new Packet(1000); Byte[] data= datastream.ToArray(); p.Add(data,true); _ConnectionToManager.SendPacket(p); Stopwatch sendTime = new Stopwatch(); sendTime.Start(); while (sendTime.ElapsedMilliseconds < _CommsTimeout) { if (_ConnectionToManager.GetPacketsToProcessCount() > 0) { foreach (Guid jobGuid in from packet in _ConnectionToManager.GetPacketsToProcess() where packet.Type == 1001 select new Guid((Byte[]) packet.GetObjects()[0])) { return jobGuid; } } Thread.Sleep(1); } if (_ConnectionToManager.IsConnected()) _ConnectionToManager.Disconnect(); _ConnectionToManager.Connect(_IpAddress, _Port, 20480 * 1024); } throw new Exception("Mananger unavailable or busy"); }
public static void AddJob(Base work) { lock (_PendingWork) { _PendingWork.Add(work.GetGuid()); BinaryFormatter binaryFormatter = new BinaryFormatter(); if (!Directory.Exists("Pending")) Directory.CreateDirectory("Pending"); FileStream stream = File.Create("Pending/" + work.GetGuid() + ".dat"); binaryFormatter.Serialize(stream, work); stream.Close(); } }
public static void RegisterCompletedWork(Base completedWork) { lock (_CompleteWork) { lock (_WorkBeingProcessed) { if (_WorkBeingProcessed.Count(a => a.Item1.GetGuid() == completedWork.GetGuid()) <= 0) return; _CompleteWork.Add(completedWork.GetGuid()); BinaryFormatter binaryFormatter = new BinaryFormatter(); if (!Directory.Exists("Completed")) Directory.CreateDirectory("Completed"); if (File.Exists("Pending/" + completedWork.GetGuid() + ".dat")) File.Delete("Pending/" + completedWork.GetGuid() + ".dat"); FileStream stream = File.Create("Completed/" + completedWork.GetGuid() + ".dat"); binaryFormatter.Serialize(stream, completedWork); stream.Close(); _WorkBeingProcessed.RemoveAll(a => a.Item1.GetGuid() == completedWork.GetGuid()); Console.WriteLine("Completed Job Registered " + completedWork.GetGuid()); } } }
protected void LogicLoop() { try { while (_Running) { if (_CurrentWork == null) { if (_ParentWorker.ThreadCountCheck()) { _Running = false; break; } _CurrentWork = _ParentWorker.GetPendingWork(); //Gets pending work from the main program } else { _ParentWorker.AnnounceStatus("Worker service starting job " + _CurrentWork.GetGuid()); Lib.Activity.Training.Base work = _CurrentWork as Lib.Activity.Training.Base; if (work != null) { Lib.Activity.Training.Base trainingWork = work; trainingWork.StartSynchronous(); _ParentWorker.AddToCompletedWork(trainingWork); } _ParentWorker.AnnounceStatus("Worker service Completed job " + _CurrentWork.GetGuid()); _CurrentWork = null; } Thread.Sleep(100); } } catch(Exception e) { _ParentWorker.AnnounceStatus("Worker exception"); _ParentWorker.AnnounceStatus(e.ToString()); } _Running = false; _ParentWorker.DecrementCurrentThreads(); }
/// <summary> /// adds a peice of work to the work completed list ready to be sent to the manager /// </summary> /// <param name="work"></param> public void AddToCompletedWork(Base work) { lock (_CompletedWork) { _CompletedWork.Add(work); if (!Directory.Exists(_Settings.CompletedWorkDirectory)) Directory.CreateDirectory(_Settings.CompletedWorkDirectory); work.SaveToDisk(_Settings.CompletedWorkDirectory + "/" + work.GetGuid()); } }