- Changes in logging and debug output
[public/netxms.git] / src / server / core / syncer.cpp
1 /*
2 ** Project X - Network Management System
3 ** Copyright (C) 2003 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 **
19 ** $module: syncer.cpp
20 **
21 **/
22
23 #include "nms_core.h"
24
25
26 //
27 // Save objects to database
28 //
29
30 void SaveObjects(void)
31 {
32 DWORD i;
33
34 ObjectsGlobalLock();
35
36 // Delete objects marked for deletion
37 for(i = 0; i < g_dwIdIndexSize; i++)
38 if (g_pIndexById[i].pObject->IsDeleted())
39 if (g_pIndexById[i].pObject->RefCount() == 0)
40 {
41 DbgPrintf(AF_DEBUG_HOUSEKEEPER, "* Syncer * Object %d \"%s\" deleted",
42 g_pIndexById[i].pObject->Id(), g_pIndexById[i].pObject->Name());
43 g_pIndexById[i].pObject->DeleteFromDB();
44 NetObjDelete(g_pIndexById[i].pObject);
45 i = 0xFFFFFFFF; // Restart loop
46 }
47 else
48 {
49 DbgPrintf(AF_DEBUG_HOUSEKEEPER, "* Syncer * Unable to delete object with id %d because it is being referenced %d time(s)",
50 g_pIndexById[i].pObject->Id(), g_pIndexById[i].pObject->RefCount());
51 }
52
53 // Save objects
54 for(i = 0; i < g_dwIdIndexSize; i++)
55 if (g_pIndexById[i].pObject->IsModified())
56 g_pIndexById[i].pObject->SaveToDB();
57
58 ObjectsGlobalUnlock();
59 }
60
61
62 //
63 // Syncer thread
64 //
65
66 THREAD_RESULT THREAD_CALL Syncer(void *arg)
67 {
68 int iSyncInterval;
69 DWORD dwWatchdogId;
70
71 // Read configuration
72 iSyncInterval = ConfigReadInt("SyncInterval", 60);
73 DbgPrintf(AF_DEBUG_HOUSEKEEPER, "Syncer thread started, sync_interval = %d", iSyncInterval);
74 dwWatchdogId = WatchdogAddThread("Syncer Thread", iSyncInterval * 2 + 10);
75
76 // Main syncer loop
77 while(!ShutdownInProgress())
78 {
79 if (SleepAndCheckForShutdown(iSyncInterval))
80 break; // Shutdown time has arrived
81 WatchdogNotify(dwWatchdogId);
82 SaveObjects();
83 SaveUsers();
84 }
85 DbgPrintf(AF_DEBUG_HOUSEKEEPER, "Syncer thread terminated");
86 return THREAD_OK;
87 }