Beispiel #1
0
        private void DoWork(CommerceInstance instance)
        {
            IndexStore store = null;

            try
            {
                var rebuildDirectory = IndexStores.GetDirectory(Context.Instance, Context.Culture, Context.ModelType, true);
                var liveDirectory    = IndexStores.GetDirectory(Context.Instance, Context.Culture, Context.ModelType, false);

                // Ensure temp folder are deleted (last rebuild might encounter errors when deleting the temp folder)
                Kooboo.IO.IOUtility.DeleteDirectory(rebuildDirectory, true);
                Kooboo.IO.IOUtility.DeleteDirectory(liveDirectory + "-tmp", true);

                var total        = _source.Count(instance, Context.Culture);
                var totalRebuilt = 0;

                Progress = 0;

                store = new IndexStore(Context.ModelType, FSDirectory.Open(rebuildDirectory), Analyzers.GetAnalyzer(Context.Culture));

                foreach (var data in _source.Enumerate(instance, Context.Culture))
                {
                    if (_cancelling)
                    {
                        break;
                    }

                    store.Index(data);

                    totalRebuilt++;
                    Progress = (int)Math.Round(totalRebuilt * 100 / (double)total);
                }

                if (_cancelling)
                {
                    store.Dispose();

                    UpdateTaskInfo(info =>
                    {
                        info.ClearError();
                        info.LastRebuildStatus = RebuildStatus.Cancelled;
                    });

                    _cancelling = false;
                    _cancelledEvent.Set();

                    Status = RebuildStatus.Cancelled;
                }
                else
                {
                    store.Commit();
                    store.Dispose();

                    UpdateTaskInfo(info =>
                    {
                        info.ClearError();
                        info.LastRebuildStatus           = RebuildStatus.Success;
                        info.LastSucceededRebuildTimeUtc = DateTime.UtcNow;
                    });

                    // Replace old index files with the new ones

                    IndexStores.Close(Context.Instance, Context.Culture, Context.ModelType);

                    var liveDirectoryExists = System.IO.Directory.Exists(liveDirectory);
                    if (liveDirectoryExists)
                    {
                        System.IO.Directory.Move(liveDirectory, liveDirectory + "-tmp");
                        Kooboo.IO.IOUtility.DeleteDirectory(liveDirectory, true);
                    }

                    System.IO.Directory.Move(rebuildDirectory, liveDirectory);

                    if (liveDirectoryExists)
                    {
                        Kooboo.IO.IOUtility.DeleteDirectory(liveDirectory + "-tmp", true);
                    }

                    Status = RebuildStatus.Success;
                }

                Progress = 0;
            }
            catch (Exception ex)
            {
                if (store != null)
                {
                    store.Dispose();
                }

                UpdateTaskInfo(info =>
                {
                    info.LastRebuildStatus      = RebuildStatus.Failed;
                    info.LastRebuildError       = ex.Message;
                    info.LastRebuildErrorDetail = ex.StackTrace;
                });

                Status = RebuildStatus.Failed;
            }
        }