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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
{ Directory cleaning service - service module
Copyright (C) 2007 Michael Van Canneyt
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version with the following modification:
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent modules,and
to copy and distribute the resulting executable under terms of your choice,
provided that you also meet, for each linked independent module, the terms
and conditions of the license of that module. An independent module is a
module which is not derived from or based on this library. If you modify
this library, you may extend this exception to your version of the library,
but you are not obligated to do so. If you do not wish to do so, delete this
exception statement from your version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
for more details.
You should have received a copy of the GNU Library General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.
}
unit svccleandirs;
{$mode objfpc}{$H+}
interface
{$R *.lfm}
uses
Classes, SysUtils, daemonapp, eventlog, dircleaner, LazFileUtils, LazUTF8;
type
{ TCleanDirsThread }
TCleanDirsThread = Class(TThread)
Private
FConfigFile : String;
FLog : TEventLog;
FCleaner : TCleanDirs;
procedure DoClean(T: TDateTime);
procedure DoLog(Msg: String);
function GetNextTime(LastTime: TDateTime): TDateTime;
procedure SetupCleaner;
Public
Constructor Create(AConfigFile : String; ALog : TEventLog; ATerminate : TNotifyEvent);
Procedure Execute; override;
end;
{ TCleanDirsDaemon }
TCleanDirsDaemon = class(TDaemon)
procedure CleanDirsDaemonCreate(Sender: TObject);
procedure CleanDirsDaemonStart(Sender: TCustomDaemon; var OK: Boolean);
procedure CleanDirsDaemonStop(Sender: TCustomDaemon; var OK: Boolean);
private
{ private declarations }
FConfigFile : String;
FThread : TCleanDirsThread;
FLog : TEventLog;
procedure StartLog;
procedure ThreadStopped(Sender: TObject);
public
{ public declarations }
end;
var
CleanDirsDaemon: TCleanDirsDaemon;
implementation
uses dateutils;
resourcestring
SStartExecute = 'Start cleaning directories for schedule time: %s';
SEndExecute = 'End of cleaning directories for schedule time: %s. Duration: %s';
SErrCleaning = 'An error occurred when cleaning: %s';
SNextCleanTime = 'Next cleaning run: %s';
SRunningDailyAt = 'Running in daily mode, clean time: %s';
SRunningHourlyAt = 'Running in hourly mode, at minute: %d';
SErrNoConfigFile = 'No configuration file found !';
// Include windows messages for eventlog component.
{$ifdef mswindows}
{ $r fclel.res}
{$endif}
procedure RegisterDaemon;
begin
RegisterDaemonClass(TCleanDirsDaemon)
end;
{ TCleanDirsDaemon }
procedure TCleanDirsDaemon.StartLog;
begin
FLog:=Self.Logger;
If (FConfigFile='') then
Flog.Error(SErrNoConfigFile);
end;
procedure TCleanDirsDaemon.CleanDirsDaemonCreate(Sender: TObject);
begin
If Application.Hasoption('c','config') then
begin
FConfigFile:=Application.GetOptionValue('c','config');
end
else
begin
FConfigFile:=GetAppConfigFile(false,false);
If Not FileExistsUTF8(FConfigFile) then
begin
FConfigFile:=GetAppConfigFile(True,false);
If Not FileExistsUTF8(FConfigFile) then
FConfigFile:='';
end;
end;
end;
procedure TCleanDirsDaemon.ThreadStopped(Sender : TObject);
begin
FThread:=Nil;
end;
procedure TCleanDirsDaemon.CleanDirsDaemonStart(Sender: TCustomDaemon;
var OK: Boolean);
begin
StartLog;
{ Start should return immediatly. Therefore, a thread is started if
one is not yet running. Usually, here one opens a TCP/IP socket
and starts listening }
OK:=(FThread=Nil) and (FConfigFile<>'');
If OK then
FThread:=TCleanDirsThread.Create(FConfigFile,FLog,@ThreadStopped);
end;
procedure TCleanDirsDaemon.CleanDirsDaemonStop(Sender: TCustomDaemon;
var OK: Boolean);
Var
I : Integer;
begin
If Assigned(FThread) then
begin
FThread.Terminate;
I:=0;
// Wait at most 5 seconds.
While (FThread<>Nil) and (I<50) do
begin
Sleep(100);
ReportStatus;
end;
// Let the thread die silently.
If (FThread<>Nil) then
FThread.OnTerminate:=Nil;
end;
OK:=FThread=Nil;
end;
{ TCleanDirsThread }
constructor TCleanDirsThread.Create(AConfigFile: String; ALog: TEventLog; ATerminate : TNotifyEvent);
begin
FConfigFile:=AConfigFile;
FLog:=Alog;
FreeOnTerminate:=False;
OnTerminate:=ATerminate;
Inherited Create(False);
end;
procedure TCleanDirsThread.DoLog(Msg : String);
begin
If Assigned(FLog) then
FLog.Info(Msg);
If Terminated then
FCleaner.Cancel;
end;
Function TCleanDirsThread.GetNextTime (LastTime : TDateTime) : TDateTime;
// Number of days to add till day is in allowed days.
Function NextOKDay(D : TDateTime) : Integer;
Const
ScheduleDays : array [1..7] of TScheduleDay
= (sdMonday,sdTuesday,sdwednesday,sdThursday,
sdFriday,sdSaturday,sdSunday);
begin
Result:=0;
If FCleaner.ScheduleDays<>[] then
While Not (ScheduleDays[DayOfTheWeek(D+Result)] in FCleaner.ScheduleDays) do
Inc(Result);
end;
Var
DT : TDateTime;
h,m,s,ms : Word;
begin
Case FCleaner.ScheduleMode of
smDaily: begin
Result:=Date;
// Too late for today.
if (Time>FCleaner.DailyAt) then
Result:=Result+1;
Result:=Result+NextOKDay(Result)+FCleaner.DailyAt
end;
smHourly : begin
DT:=Now;
H:=NextOKDay(DT);
If (H<>0) then
Result:=ComposeDateTime(Date+H,EncodeTime(0,FCleaner.HourlyAt,0,0))
else
begin // Still today
DecodeTime(DT,H,M,S,MS);
Result:=Date;
If (M>=FCleaner.HourlyAt) then
begin // Next hour
H:=H+1;
If (H=24) then
begin
H:=0; // Tomorrow, check next day OK.
Result:=Result+1+NextOKDay(Result+1);
end;
end;
DT:=EncodeTime(H,FCleaner.HourlyAt,0,0);
Result:=ComposeDateTime(Result,DT);
end;
end;
end;
DoLog(Format(SNextCleanTime,[DateTimeToStr(Result)]));
end;
procedure TCleanDirsThread.DoClean(T : TDateTime);
Var
S,ST : String;
DT : TDateTime;
begin
try
S:=DateTimeToStr(T);
DoLog(Format(SStartExecute,[S]));
// FCleaner.Execute;
DT:=FCleaner.EndTime-FCleaner.StartTime;
ST:=TimeToStr(DT);
DoLog(Format(SEndExecute,[S,ST]));
except
On E : Exception do
If Assigned(FLog) then
FLog.Error(SErrCleaning,[E.Message]);
end;
end;
procedure TCleanDirsThread.SetupCleaner;
begin
FCleaner.OnLog:=@Self.DoLog;
FCleaner.LoadFromFile(UTF8ToSys(FConfigFile));
If FCleaner.ScheduleMode=smDaily then
DoLog(Format(SRunningDailyAt,[TimeToStr(FCleaner.DailyAt)]))
else
DoLog(Format(SRunningHourlyAt,[FCleaner.HourlyAt]));
end;
procedure TCleanDirsThread.Execute;
Function GetDelta (T : TDateTime) : Int64;
begin
Result:=SecondsBetween(Now,T)*1000;
If (Result>3000) then
Result:=3000;
end;
Var
T : TDateTime;
Delta : Int64;
begin
FCleaner:=TCleanDirs.Create(Nil);
try
SetupCleaner;
T:=Now;
Repeat
T:=GetNextTime(T);
Repeat
Delta:=GetDelta(T); // 3 seconds or less
If Not Terminated then
Sleep(Delta);
Until (Now>=T) or Terminated;
If Not Terminated then
DoClean(T);
Until Terminated;
finally
FreeAndNil(FCleaner);
end;
end;
initialization
RegisterDaemon;
end.
|