Skip to content

Commit eb4415e

Browse files
authored
Feature/cleanup (#7)
* Add cleanup previews step. * Add cleanup events and refactor progress display. * Load content versions using a custom script. Add snadmin tool manifest. * Add cleanup step docs. * Blocksize and paralallel degree became customizable.
1 parent 57d9c5a commit eb4415e

File tree

8 files changed

+686
-61
lines changed

8 files changed

+686
-61
lines changed

docs/preview.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,31 @@ You can customize the following values here:
161161

162162
> You may have different preview settings on lower levels in your content tree - e.g. under every workspace. See [Settings](/docs/settings) for more details.
163163
164+
## Cleaning up preview images
165+
There is an [SnAdmin tool](https://community.sensenet.com/docs/snadmin-tools) for deleting unnecessary preview images and preview folders from the content repository. You may use it to shrink the size of your database as preview images for large or many documents tend to occupy lots of space. You have the option to delete preview images:
166+
167+
- in the whole repository or just in a subtree
168+
- for old document versions or all of them
169+
- keep only a certain number of images (e.g. the first 5) per file
170+
- delete only empty preview folders but keep all images
171+
172+
The tool wraps an SnAdmin step that you may use in your own packages:
173+
174+
```xml
175+
<StartRepository StartWorkflowEngine="false" />
176+
<CleanupPreviews path="@path" maxIndex="@maxIndex" mode="@mode" />
177+
```
178+
179+
Available values for the `mode` parameter:
180+
181+
- **All**: default
182+
- **KeepLastVersions**: deletes previews for old versions
183+
- **EmptyFoldersOnly**: only deletes empty preview folders but retains preview images
184+
185+
> This step removes preview images from the database and preview folders from both the db and the index - this is why it needs the **repository started**.
186+
187+
Depending on the number of files in the target subtree this step may run for a while (although it uses direct SQL queries so it is fairly efficient), but it periodically writes the progress to the console.
188+
164189
## Integrating a custom preview generator
165190
To integrate a custom document preview generator you'll need to implement your preview provider that will define the supported types and the [custom task](/docs/task-management) that will generate the images. To learn more on this topic read this article:
166191

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System.Threading;
2+
using SenseNet.Packaging.Steps;
3+
using ExecutionContext = SenseNet.Packaging.ExecutionContext;
4+
5+
namespace SenseNet.Preview.Packaging.Steps
6+
{
7+
public enum CleanupMode
8+
{
9+
AllVersions,
10+
KeepLastVersions,
11+
EmptyFoldersOnly
12+
}
13+
14+
public class CleanupPreviews : Step
15+
{
16+
public string Path { get; set; }
17+
public int MaxIndex { get; set; }
18+
public CleanupMode Mode { get; set; }
19+
20+
public int MaxDegreeOfParallelism { get; set; } = 10;
21+
public int BlockSize { get; set; } = 500;
22+
23+
private ExecutionContext _context;
24+
private int _folderCount;
25+
private int _imageCount;
26+
27+
public override void Execute(ExecutionContext context)
28+
{
29+
_context = context;
30+
31+
using (new Timer(state => WriteProgress(), null, 1000, 2000))
32+
{
33+
var pc = new PreviewCleaner(Path, Mode, MaxIndex, MaxDegreeOfParallelism, BlockSize);
34+
pc.OnFolderDeleted += (s, e) => { Interlocked.Increment(ref _folderCount);};
35+
pc.OnImageDeleted += (s, e) => { Interlocked.Increment(ref _imageCount); };
36+
37+
pc.Execute();
38+
}
39+
40+
WriteProgress();
41+
_context?.Console?.WriteLine();
42+
}
43+
44+
private void WriteProgress()
45+
{
46+
_context?.Console?.Write($" Deleted folders: {_folderCount}, images: {_imageCount} \r");
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)