Esempio n. 1
0
        private async Task <bool> ProcessDir(string dir)
        {
            Log.Trace("Started processing folder: " + dir + ", Thread Id: " + Thread.CurrentThread.ManagedThreadId);

            try
            {
                // Process sub dirs
                IEnumerable <string> directories = Directory.EnumerateDirectories(dir);

                // Store all the sub dirs in the data store wiht flag - need to traverse
                if (directories.Count() > 0)
                {
                    Log.Trace("Sent to BulkAdd " + directories.Count() + " records" + ", Thread Id: " + Thread.CurrentThread.ManagedThreadId);

                    List <string> lst = new List <string>();

                    foreach (string d in directories)
                    {
                        try
                        {
                            DirectoryInfo di = new DirectoryInfo(d);
                            lst.Add(di.FullName);
                            Log.Trace("Added folder: " + di.FullName + ", Thread Id: " + Thread.CurrentThread.ManagedThreadId);
                        }
                        catch (Exception ex)
                        {
                            Log.Info(ex, "User doesn't have permissions to folder: " + d);
                        }
                    }

                    await SMBDal.BulkAddFolders(lst);

                    Log.Trace("Line after BulkAdd of " + directories.Count() + " records" + ", Thread Id: " + Thread.CurrentThread.ManagedThreadId);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Failed to process folder: " + dir);
            }
            return(true);
        }
Esempio n. 2
0
        private async Task <bool> TraverseTree(string root, int maxTasks)
        {
            string currentDir = root;

            using (var throttler = new SemaphoreSlim(maxTasks))
            {
                var postTaskTasks = new List <PFTask>();

                while (currentDir != null)
                {
                    if (currentDir == root)
                    {
                        // First time get the directories syncroniously to populate the DB with directories to run on
                        IEnumerable <string> directories = Directory.EnumerateDirectories(currentDir);
                        List <string>        lst         = new List <string>();

                        foreach (string dir in directories)
                        {
                            try
                            {
                                DirectoryInfo di = new DirectoryInfo(dir);
                                lst.Add(di.FullName);
                                Log.Trace("Added folder: " + di.FullName + ", Thread Id: " + Thread.CurrentThread.ManagedThreadId);
                            }
                            catch (Exception ex)
                            {
                                Log.Info(ex, "User doesn't have permissions to folder: " + dir);
                            }
                        }
                        //await SMBDal.BulkAddFolders(directories.ToList());
                        await SMBDal.BulkAddFolders(lst);
                    }
                    else
                    {
                        Log.Trace("Before wait: " + currentDir + ", wait: " + throttler.CurrentCount + ", Thread Id: " + Thread.CurrentThread.ManagedThreadId);
                        await throttler.WaitAsync();

                        Log.Trace("After wait: " + currentDir + ", wait: " + throttler.CurrentCount + ", Thread Id: " + Thread.CurrentThread.ManagedThreadId);
                        string value = currentDir.ToString();
                        PFTask t     = new PFTask();
                        t.Task = Task.Run(() => ProcessDir(value), t.CancellationToken).ContinueWith(tsk => release(throttler));
                        postTaskTasks.Add(t);
                        Log.Trace("After add task: " + currentDir + ", Thread Id: " + Thread.CurrentThread.ManagedThreadId);
                    }

                    // Fetch the next directory to traverse and do it again
                    currentDir = await SMBIterator.GetNextFolderForTraverse();

                    Log.Trace("GetNextFolderForTraverse returned: " + currentDir + ", Thread Id: " + Thread.CurrentThread.ManagedThreadId);

                    // Clean the completed tasks from the array
                    foreach (PFTask t in postTaskTasks)
                    {
                        if (t.Task.IsCompleted || t.Task.IsCanceled || t.Task.IsFaulted)
                        {
                            _tasksToRemove.Add(t);
                        }
                    }
                    foreach (PFTask t in _tasksToRemove)
                    {
                        postTaskTasks.Remove(t);
                    }
                    _tasksToRemove = new List <PFTask>();

                    if (currentDir == null)
                    {
                        Log.Trace("WaitAll" + ", Thread Id: " + Thread.CurrentThread.ManagedThreadId);
                        Task.WaitAll(postTaskTasks.Select(t => t.Task).ToArray());
                        currentDir = await SMBIterator.GetNextFolderForTraverse();

                        Log.Trace("Got directory after wait all: " + currentDir + ", Thread Id: " + Thread.CurrentThread.ManagedThreadId);
                    }
                }
            }

            return(true);
        }