public async Task AssignTask(int taskId) { await _locker.WaitAsync(); try { int?workerId = await WorkerRepository.FindLessLoadedServer(); var server = workerId != 0 ? await WorkerRepository.GetWorkerServer(workerId.Value) : null; bool isServerConnected = server != null && _connectionService.IsConnected(server.IpAddress, server.Port); if (workerId != 0 && isServerConnected) { await TaskRepository.AssignTask(taskId, workerId); } } finally { _locker.Release(); } }