public void Run(TaskRunContext task)
        {
            _resourcesLock.EnterReadLock();

            try
            {
                Log.Info("Running task " + task.ToString());

                string resourceName  = task.NodesConfig.First().ResourceName;
                var    resourceCache =
                    ResourceCache.GetByName(resourceName);

                lock (resourceCache.StateLock)
                {
                    CheckNodeConfigConsistency(task.TaskId, task.NodesConfig, resourceCache.Resource);

                    task.Resource   = resourceCache.Resource;
                    task.Controller = resourceCache.Controller;
                }

                try
                {
                    resourceCache.Acquire(task.NodesConfig);  // todo : m.b. move under resourceCache.StateLock?

                    Log.Info(String.Format("Trying to run task {0} on resource {1}", task.TaskId, task.Resource.ResourceName));

                    task.LocalId = task.Controller.Run(task);

                    Log.Info(String.Format("Task {0} ({1}) started on resource {2} with localId = {3}",
                                           task.TaskId, task.PackageName, task.Resource.ResourceName, task.LocalId
                                           ));

                    var state = new TaskStateInfo(TaskState.Started, task.LocalId.ToString());
                    TaskCache.AddTask(task, state);
                }
                catch (Exception e)
                {
                    resourceCache.Release(task.NodesConfig);

                    Log.Error(String.Format("Unable to run task {0}: {1}", task.TaskId, e));
                    throw;
                }
            }
            catch (Exception e)
            {
                Log.Error(String.Format("Exception on Farm.Run(task {0}): {1}", task.TaskId, e));
                throw;
            }
            finally
            {
                _resourcesLock.ExitReadLock();
            }


            //todo for mock
            if (CacheCollectorFactory.CheckMockMode())
            {
                CacheCollectorFactory.GetInstance().SendTask(task);
            }
        }
        ControllerFarmService()
        {
            bool loaded = false;

            //for (int retries = 0; !loaded && retries < 3; retries++)
            {
                try
                {
                    ReloadAllResources();
                    loaded = true;
                }
                catch (Exception e)
                {
                    Log.Error("Could not load resources on server start. Retying. " + e.ToString());
                    System.Threading.Thread.Sleep(200);
                }
            }

            System.Threading.Tasks.TaskScheduler.UnobservedTaskException += (sender, e) =>
            {
                Log.Error("Exception in some TPL's task: " + e.ToString());
                e.SetObserved();
                //throw e.Exception;
            };

            //todo it was made for a mock creating task #1267
            if (CacheCollectorFactory.CheckMockMode())
            {
                CacheCollectorFactory.GetInstance().RunWithFarm(this);
            }
        }
        public Dictionary <string, Dictionary <string, List <NodeInfo> > > GetAllCacheableResourcesInfoStartedWith(DateTime date)
        {
            if (CacheCollectorFactory.CheckMockMode())
            {
                Common.Utility.LogInfo("Mock Mode enabled. nothing to return.");
                return(new Dictionary <string, Dictionary <string, List <NodeInfo> > >());
            }

            return(GetAllResourcesInfoStartWith(date));
        }
        public Dictionary <ulong, TaskStatInfo> GetAllCacheableTaskInfoStartedWith(DateTime date)
        {
            if (CacheCollectorFactory.CheckMockMode())
            {
                Common.Utility.LogInfo("Mock Mode enabled. nothing to return.");
                return(new Dictionary <ulong, TaskStatInfo>());
            }

            ulong[] ids = GetActiveTaskIds();
            Dictionary <ulong, TaskStatInfo> data = new Dictionary <ulong, TaskStatInfo>();

            //todo uncomment it later when rex will have been repaired
            return(data);

            foreach (var id in ids)
            {
                var statInfo = GetCacheableInfoForTask(id, date);
                if (statInfo != null)
                {
                    data.Add(id, statInfo);
                }
            }
            return(data);
        }