@@ -29,7 +29,7 @@ func NewCoreHandler(accountRepository repository.AccountRepository, apiService I
2929 }
3030}
3131
32- func (c * CoreHandler ) InitCoreHandlerCoroutine () (int , error ) {
32+ func (c * CoreHandler ) InitCoreHandlerGoroutine () (int , error ) {
3333 // Check if core handler is already running
3434 if c .Status == CoreHandlerStatusRunning {
3535 return c .Status , nil
@@ -39,7 +39,7 @@ func (c *CoreHandler) InitCoreHandlerCoroutine() (int, error) {
3939 c .Status = CoreHandlerStatusInit
4040
4141 // Load all accounts
42- accounts , _ := c .AccountRepository .FetchAllActiveAccounts ()
42+ accounts := c .AccountRepository .FetchAllActiveAccounts ()
4343
4444 // Iterate over accounts and start account handlers
4545 for _ , account := range accounts {
@@ -70,7 +70,9 @@ func (c *CoreHandler) StartAccountHandler(accountId string, gitService IGitServi
7070
7171 // Wait for account to be active
7272 if ! account .Settings .Active {
73- utils .Log (utils .LogLevelInfo , "[%s - %s] Account is not active, waiting for activation" , accountId , account .Domain .Name )
73+ utils .Log (utils .LogLevelInfo , "[%s - %s (%s)] Account is not active, waiting for activation" ,
74+ accountId , account .Domain .Name , account .Environment )
75+
7476 c .updateDomainStatus (* account , model .StatusPending , "Account was deactivated" )
7577 time .Sleep (1 * time .Minute )
7678 continue
@@ -83,7 +85,9 @@ func (c *CoreHandler) StartAccountHandler(accountId string, gitService IGitServi
8385 repositoryData , err := gitService .GetRepositoryData (account .Environment )
8486
8587 if err != nil {
86- utils .Log (utils .LogLevelError , "[%s - %s] Failed to fetch repository data - %s" , accountId , account .Domain .Name , err .Error ())
88+ utils .Log (utils .LogLevelError , "[%s - %s (%s)] Failed to fetch repository data - %s" ,
89+ accountId , account .Domain .Name , account .Environment , err .Error ())
90+
8791 c .updateDomainStatus (* account , model .StatusError , "Failed to fetch repository data - " + err .Error ())
8892 time .Sleep (1 * time .Minute )
8993 continue
@@ -93,7 +97,9 @@ func (c *CoreHandler) StartAccountHandler(accountId string, gitService IGitServi
9397 snapshotVersionPayload , err := c .ApiService .FetchSnapshotVersion (account .Domain .ID , account .Environment )
9498
9599 if err != nil {
96- utils .Log (utils .LogLevelError , "[%s - %s] Failed to fetch snapshot version - %s" , accountId , account .Domain .Name , err .Error ())
100+ utils .Log (utils .LogLevelError , "[%s - %s (%s)] Failed to fetch snapshot version - %s" ,
101+ accountId , account .Domain .Name , account .Environment , err .Error ())
102+
97103 c .updateDomainStatus (* account , model .StatusError , "Failed to fetch snapshot version - " + err .Error ())
98104 time .Sleep (1 * time .Minute )
99105 continue
@@ -111,9 +117,11 @@ func (c *CoreHandler) StartAccountHandler(accountId string, gitService IGitServi
111117}
112118
113119func (c * CoreHandler ) syncUp (account model.Account , repositoryData * model.RepositoryData , gitService IGitService ) {
114- utils .Log (utils .LogLevelInfo , "[%s - %s] Syncing up" , account .ID .Hex (), account .Domain .Name )
120+ utils .Log (utils .LogLevelInfo , "[%s - %s (%s) ] Syncing up" , account .ID .Hex (), account .Domain .Name , account . Environment )
115121
116122 // Update account status: Out of sync
123+ account .Domain .LastCommit = repositoryData .CommitHash
124+ account .Domain .LastDate = repositoryData .CommitDate
117125 c .updateDomainStatus (account , model .StatusOutSync , model .MessageSyncingUp )
118126
119127 // Check for changes
@@ -124,17 +132,19 @@ func (c *CoreHandler) syncUp(account model.Account, repositoryData *model.Reposi
124132 return
125133 }
126134
127- utils .Log (utils .LogLevelDebug , "[%s - %s] SnapshotAPI version: %s - SnapshotRepo version: %s" ,
128- account .ID .Hex (), account .Domain .Name , fmt .Sprint (snapshotApi .Domain .Version ), fmt .Sprint (account .Domain .Version ))
135+ utils .Log (utils .LogLevelDebug , "[%s - %s (%s) ] SnapshotAPI version: %s - SnapshotRepo version: %s" ,
136+ account .ID .Hex (), account .Domain .Name , account . Environment , fmt .Sprint (snapshotApi .Domain .Version ), fmt .Sprint (account .Domain .Version ))
129137
130138 // Apply changes
131139 changeSource := ""
132140 if snapshotApi .Domain .Version > account .Domain .Version {
133141 changeSource = "Repository"
134- if c .isRepositoryOutSync (account , repositoryData , diff ) {
142+ if c .isRepositoryOutSync (repositoryData , diff ) {
135143 account , err = c .applyChangesToRepository (account , snapshotApi , gitService )
136144 } else {
137- utils .Log (utils .LogLevelInfo , "[%s - %s] Repository is up to date" , account .ID .Hex (), account .Domain .Name )
145+ utils .Log (utils .LogLevelInfo , "[%s - %s (%s)] Repository is up to date" ,
146+ account .ID .Hex (), account .Domain .Name , account .Environment )
147+
138148 account .Domain .Version = snapshotApi .Domain .Version
139149 account .Domain .LastCommit = repositoryData .CommitHash
140150 }
@@ -144,7 +154,9 @@ func (c *CoreHandler) syncUp(account model.Account, repositoryData *model.Reposi
144154 }
145155
146156 if err != nil {
147- utils .Log (utils .LogLevelError , "[%s - %s] Failed to apply changes [%s] - %s" , account .ID .Hex (), account .Domain .Name , changeSource , err .Error ())
157+ utils .Log (utils .LogLevelError , "[%s - %s (%s)] Failed to apply changes [%s] - %s" ,
158+ account .ID .Hex (), account .Domain .Name , account .Environment , changeSource , err .Error ())
159+
148160 c .updateDomainStatus (account , model .StatusError , "Failed to apply changes [" + changeSource + "] - " + err .Error ())
149161 return
150162 }
@@ -178,7 +190,7 @@ func (c *CoreHandler) checkForChanges(account model.Account, content string) (mo
178190}
179191
180192func (c * CoreHandler ) applyChangesToAPI (account model.Account , repositoryData * model.RepositoryData , diff model.DiffResult ) model.Account {
181- utils .Log (utils .LogLevelInfo , "[%s - %s] Pushing changes to API" , account .ID .Hex (), account .Domain .Name )
193+ utils .Log (utils .LogLevelInfo , "[%s - %s (%s) ] Pushing changes to API" , account .ID .Hex (), account .Domain .Name , account . Environment )
182194
183195 // Removed deleted if force prune is disabled
184196 if ! account .Settings .ForcePrune {
@@ -195,7 +207,7 @@ func (c *CoreHandler) applyChangesToAPI(account model.Account, repositoryData *m
195207}
196208
197209func (c * CoreHandler ) applyChangesToRepository (account model.Account , snapshot model.Snapshot , gitService IGitService ) (model.Account , error ) {
198- utils .Log (utils .LogLevelInfo , "[%s - %s] Pushing changes to repository" , account .ID .Hex (), account .Domain .Name )
210+ utils .Log (utils .LogLevelInfo , "[%s - %s (%s) ] Pushing changes to repository" , account .ID .Hex (), account .Domain .Name , account . Environment )
199211
200212 // Remove version from domain
201213 snapshotContent := snapshot
@@ -217,17 +229,16 @@ func (c *CoreHandler) applyChangesToRepository(account model.Account, snapshot m
217229func (c * CoreHandler ) isOutSync (account model.Account , lastCommit string , snapshotVersionPayload string ) bool {
218230 snapshotVersion := c .ApiService .NewDataFromJson ([]byte (snapshotVersionPayload )).Snapshot .Domain .Version
219231
220- utils .Log (utils .LogLevelDebug , "[%s - %s] Checking account - Last commit: %s - Domain Version: %d - Snapshot Version: %d" ,
221- account .ID .Hex (), account .Domain .Name , account .Domain .LastCommit , account .Domain .Version , snapshotVersion )
232+ utils .Log (utils .LogLevelDebug , "[%s - %s (%s) ] Checking account - Last commit: %s - Domain Version: %d - Snapshot Version: %d" ,
233+ account .ID .Hex (), account .Domain .Name , account .Environment , account . Domain .LastCommit , account .Domain .Version , snapshotVersion )
222234
223235 return account .Domain .LastCommit == "" || // First sync
224236 account .Domain .LastCommit != lastCommit || // Repository out of sync
225237 account .Domain .Version != snapshotVersion // API out of sync
226238}
227239
228- func (c * CoreHandler ) isRepositoryOutSync (account model.Account , repositoryData * model.RepositoryData , diff model.DiffResult ) bool {
229- return account .Domain .Version == 0 || // First/Force-push sync
230- len (repositoryData .Content ) <= 1 || // File is empty
240+ func (c * CoreHandler ) isRepositoryOutSync (repositoryData * model.RepositoryData , diff model.DiffResult ) bool {
241+ return len (repositoryData .Content ) <= 1 || // File is empty
231242 len (diff .Changes ) > 0 // Changes detected
232243}
233244
0 commit comments