-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule1.vb
More file actions
269 lines (269 loc) · 11.1 KB
/
Module1.vb
File metadata and controls
269 lines (269 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
Imports System.Configuration.Install
Imports System.ComponentModel
Imports System.ServiceProcess
Imports System.IO
Imports System.Threading
Public Class IndexMonitorService
Inherits System.ServiceProcess.ServiceBase
Private DirToMonitor As String
Private DirsToSync As List(Of String)
Private indexWatcher As FileSystemWatcher
Private components As System.ComponentModel.IContainer
Public Sub New()
MyBase.New()
InitializeComponent()
Me.CanStop = True
Me.CanPauseAndContinue = True
Me.CanShutdown = True
End Sub
Protected Overrides Sub OnStart(ByVal args() As String)
If BadConfiguration() Then
Me.Stop()
Exit Sub
End If
indexWatcher = New FileSystemWatcher
indexWatcher.Path = DirToMonitor
indexWatcher.NotifyFilter = (NotifyFilters.FileName Or NotifyFilters.LastWrite Or NotifyFilters.CreationTime Or NotifyFilters.DirectoryName)
indexWatcher.Filter = "finished"
indexWatcher.InternalBufferSize = 131072
indexWatcher.IncludeSubdirectories = True
AddHandler indexWatcher.Created, AddressOf OnCreated
indexWatcher.EnableRaisingEvents = True
LogThis("Index Monitor Started.")
End Sub
Protected Overrides Sub OnStop()
LogThis("Index Monitor Stopped.")
End Sub
Protected Overrides Sub OnPause()
indexWatcher.EnableRaisingEvents = False
LogThis("Index Monitor Paused.")
End Sub
Protected Overrides Sub OnContinue()
indexWatcher.EnableRaisingEvents = True
LogThis("Index Monitor Continued.")
End Sub
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
<MTAThread()> <System.Diagnostics.DebuggerNonUserCode()> _
Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase
ServicesToRun = New System.ServiceProcess.ServiceBase() {New IndexMonitorService}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.ServiceName = "IndexMonitorService"
End Sub
Private Sub RunMonitor()
Dim watcher As New FileSystemWatcher()
watcher.Path = DirToMonitor
watcher.NotifyFilter = (NotifyFilters.FileName Or NotifyFilters.LastWrite Or NotifyFilters.CreationTime Or NotifyFilters.DirectoryName)
watcher.Filter = "finished"
watcher.InternalBufferSize = 131072
watcher.IncludeSubdirectories = True
AddHandler watcher.Created, AddressOf OnCreated
watcher.EnableRaisingEvents = True
'While started
'End While
End Sub
Private Sub OnCreated(ByVal source As Object, ByVal e As FileSystemEventArgs)
If File.Exists(e.FullPath) Then
LogThis("CREATED: " & e.FullPath)
If NewIndexOnlyHere(e.FullPath) Then
Dim parentFolder As DirectoryInfo = Directory.GetParent(e.FullPath)
Dim aMove As New CopyFolder(parentFolder, DirsToSync, DirToMonitor)
Dim moveThread As New Thread(AddressOf aMove.Copy)
moveThread.Start()
LogThis("QUEUED : " & parentFolder.FullName & " for move.")
End If
End If
End Sub
Private Function NewIndexOnlyHere(ByVal anIndex As String) As Boolean
For Each dirToSyc In DirsToSync
If File.Exists(anIndex.Replace(DirToMonitor, dirToSyc)) Then
Return False
End If
Next
Return True
End Function
Function BadConfiguration() As Boolean
Dim ConfigFile As String = My.Application.Info.DirectoryPath & "\" & My.Application.Info.ProductName & ".conf"
If My.Computer.FileSystem.FileExists(ConfigFile) Then
LogThis("Found Configuration file: " & ConfigFile)
Else
LogThis("Configuration file not found at: " & ConfigFile)
LogThis("Progam will not continue.")
Return True
End If
DirsToSync = New List(Of String)
Dim sr As New StreamReader(ConfigFile)
Try
Do
Dim s As String = sr.ReadLine
Dim ss() As String = s.Split(">")
Select Case ss(0)
Case "DirToMonitor"
DirToMonitor = ss(1).Trim
If My.Computer.FileSystem.DirectoryExists(DirToMonitor) Then
LogThis("Found Directory to Monitor: " & DirToMonitor)
Else
LogThis("Did not find Directory to Monitor: " & DirToMonitor)
LogThis("Progam will not continue.")
sr.Close()
Return True
End If
Case "DirToSync1"
Dim Dir1 As String = ss(1).Trim
If Directory.Exists(Dir1) Then
DirsToSync.Add(Dir1)
LogThis("Found Directory to Synchronize 1: " & Dir1)
Else
LogThis("Did not find Directory to Synchronize 1: " & Dir1)
End If
Case "DirToSync2"
Dim Dir2 As String = ss(1).Trim
If Directory.Exists(Dir2) Then
DirsToSync.Add(Dir2)
LogThis("Found Directory to Synchronize 2: " & Dir2)
Else
LogThis("Did not find Directory to Synchronize 2: " & Dir2)
End If
Case "DirToSync3"
Dim Dir3 As String = ss(1).Trim
If Directory.Exists(Dir3) Then
DirsToSync.Add(Dir3)
LogThis("Found Directory to Synchronize 2: " & Dir3)
Else
LogThis("Did not find Directory to Synchronize 2: " & Dir3)
End If
Case "DirToSync4"
Dim Dir4 As String = ss(1).Trim
If Directory.Exists(Dir4) Then
DirsToSync.Add(Dir4)
LogThis("Found Directory to Synchronize 2: " & Dir4)
Else
LogThis("Did not find Directory to Synchronize 2: " & Dir4)
End If
End Select
Loop Until sr.Peek = -1
Catch ex As Exception
LogThis(ConfigFile & " is empty. Program will not continue.")
Return True
Finally
sr.Close()
End Try
If String.IsNullOrEmpty(DirToMonitor) Then
LogThis("Directory to Monitor directive not configured. Program will not continue.")
Return True
End If
If DirsToSync.Count < 1 Then
LogThis("None of the Synchronization directories were found.")
LogThis("Progam will not continue.")
Return True
End If
Return False
End Function
Private Sub LogThis(ByVal info As String)
Dim logfile As String = My.Application.Info.DirectoryPath & "\" & My.Application.Info.ProductName & ".log"
Dim timestamp As String = "[" & Now.ToString("yyyy-MM-dd HH:mm:ss") & "] "
My.Computer.FileSystem.WriteAllText(logfile, timestamp & info & vbCrLf, True)
End Sub
End Class
<RunInstaller(True)> _
Public Class IndexMonitorServiceInstaller
Inherits Installer
Private myServiceInstaller As ServiceInstaller
Private myServiceProcessInstaller As ServiceProcessInstaller
Public Sub New()
MyBase.New()
myServiceInstaller = New ServiceInstaller
myServiceProcessInstaller = New ServiceProcessInstaller
myServiceProcessInstaller.Account = ServiceAccount.NetworkService
myServiceInstaller.StartType = ServiceStartMode.Manual
myServiceInstaller.ServiceName = "Roknet Index Monitor"
myServiceInstaller.Description = "Service that monitors and replicates Lucene index changes to other Roknet web servers"
Installers.Add(myServiceInstaller)
Installers.Add(myServiceProcessInstaller)
End Sub
Public Overrides Sub Install(ByVal savedState As IDictionary)
MyBase.Install(savedState)
Console.WriteLine("On Installer, Install")
End Sub
Public Overrides Sub Commit(ByVal savedState As IDictionary)
MyBase.Commit(savedState)
Console.WriteLine("On Installer, Commit")
End Sub
Public Overrides Sub Rollback(ByVal savedState As IDictionary)
MyBase.Rollback(savedState)
End Sub
Public Overrides Sub Uninstall(ByVal savedState As IDictionary)
MyBase.Uninstall(savedState)
End Sub
End Class
Public Class CopyFolder
Private folderToCopy As DirectoryInfo
Private DirsToSync As List(Of String)
Private DirToMonitor As String
Public Sub New(ByVal aFolder As DirectoryInfo, ByVal FoldersToSych As List(Of String), ByVal FolderToMonitor As String)
folderToCopy = aFolder
DirsToSync = FoldersToSych
DirToMonitor = FolderToMonitor
End Sub
Public Sub Copy()
Thread.Sleep(5000)
If Directory.Exists(folderToCopy.FullName) Then
For Each DirToSyc As String In DirsToSync
Dim newDir As String = folderToCopy.FullName.Replace(DirToMonitor, DirToSyc)
Try
Directory.CreateDirectory(newDir)
Catch ex As Exception
LogThis("Error creating directory " & newDir & ": " & ex.Message)
End Try
LogThis("Copying: " & folderToCopy.FullName & " to " & newDir & "...")
For Each oneFile As FileInfo In folderToCopy.GetFiles
Try
Dim newFile As New FileInfo(oneFile.FullName.Replace(DirToMonitor, DirToSyc))
If oneFile.Exists Then
oneFile.CopyTo(newFile.FullName, True)
End If
Catch ex As Exception
LogThis("Error copying file " & oneFile.FullName & " to " & newDir & ": " & ex.Message)
End Try
Next
LogThis("COPIED : " & newDir)
Next
End If
RemoveExtraFolders()
End Sub
Private Sub RemoveExtraFolders()
Dim baseFolder As New DirectoryInfo(folderToCopy.FullName.Replace(folderToCopy.Name, ""))
For Each oneDir As String In DirsToSync
Dim aSycDir As New DirectoryInfo(baseFolder.FullName.Replace(DirToMonitor, oneDir))
For Each subDir As DirectoryInfo In aSycDir.GetDirectories
If Not Directory.Exists(subDir.FullName.Replace(oneDir, DirToMonitor)) Then
Try
LogThis("Deleting extra directory: " & subDir.FullName)
If subDir.Exists Then
subDir.Delete(True)
End If
Catch ex As Exception
'LogThis("Error deleting extra directory: " & subDir.FullName & ": " & ex.Message)
End Try
End If
Next
Next
End Sub
Private Sub LogThis(ByVal info As String)
Dim logfile As String = My.Application.Info.DirectoryPath & "\" & My.Application.Info.ProductName & ".log"
Dim timestamp As String = "[" & Now.ToString("yyyy-MM-dd HH:mm:ss") & "] "
My.Computer.FileSystem.WriteAllText(logfile, timestamp & info & vbCrLf, True)
End Sub
End Class