@@ -25,7 +25,7 @@ type CoreHandler struct {
2525}
2626
2727func NewCoreHandler (accountRepository repository.AccountRepository , apiService IAPIService , comparatorService IComparatorService ) * CoreHandler {
28- timeWindow , unitWindow := getTimeWindow (config .GetEnv ("HANDLER_WAITING_TIME" ))
28+ timeWindow , unitWindow := utils . GetTimeWindow (config .GetEnv ("HANDLER_WAITING_TIME" ))
2929 waitingTime := time .Duration (timeWindow ) * unitWindow
3030
3131 return & CoreHandler {
@@ -64,21 +64,21 @@ func (c *CoreHandler) InitCoreHandlerGoroutine() (int, error) {
6464}
6565
6666func (c * CoreHandler ) StartAccountHandler (accountId string , gitService IGitService ) {
67- utils .Log ( utils . LogLevelInfo , "[%s] Starting account handler" , accountId )
67+ utils .LogInfo ( "[%s] Starting account handler" , accountId )
6868
6969 for {
7070 // Refresh account settings
7171 account , _ := c .accountRepository .FetchByAccountId (accountId )
7272
7373 if account == nil {
7474 // Terminate the goroutine (account was deleted)
75- utils .Log ( utils . LogLevelInfo , "[%s] Account was deleted, terminating account handler" , accountId )
75+ utils .LogInfo ( "[%s] Account was deleted, terminating account handler" , accountId )
7676 return
7777 }
7878
7979 // Wait for account to be active
8080 if ! account .Settings .Active {
81- utils .Log ( utils . LogLevelInfo , "[%s - %s (%s)] Account is not active, waiting for activation" ,
81+ utils .LogInfo ( "[%s - %s (%s)] Account is not active, waiting for activation" ,
8282 accountId , account .Domain .Name , account .Environment )
8383
8484 c .updateDomainStatus (* account , model .StatusPending , "Account was deactivated" )
@@ -93,7 +93,7 @@ func (c *CoreHandler) StartAccountHandler(accountId string, gitService IGitServi
9393 repositoryData , err := gitService .GetRepositoryData (account .Environment )
9494
9595 if err != nil {
96- utils .Log ( utils . LogLevelError , "[%s - %s (%s)] Failed to fetch repository data - %s" ,
96+ utils .LogError ( "[%s - %s (%s)] Failed to fetch repository data - %s" ,
9797 accountId , account .Domain .Name , account .Environment , err .Error ())
9898
9999 c .updateDomainStatus (* account , model .StatusError , "Failed to fetch repository data - " + err .Error ())
@@ -105,7 +105,7 @@ func (c *CoreHandler) StartAccountHandler(accountId string, gitService IGitServi
105105 snapshotVersionPayload , err := c .apiService .FetchSnapshotVersion (account .Domain .ID , account .Environment )
106106
107107 if err != nil {
108- utils .Log ( utils . LogLevelError , "[%s - %s (%s)] Failed to fetch snapshot version - %s" ,
108+ utils .LogError ( "[%s - %s (%s)] Failed to fetch snapshot version - %s" ,
109109 accountId , account .Domain .Name , account .Environment , err .Error ())
110110
111111 c .updateDomainStatus (* account , model .StatusError , "Failed to fetch snapshot version - " + err .Error ())
@@ -119,13 +119,13 @@ func (c *CoreHandler) StartAccountHandler(accountId string, gitService IGitServi
119119 }
120120
121121 // Wait for the next cycle
122- timeWindow , unitWindow := getTimeWindow (account .Settings .Window )
122+ timeWindow , unitWindow := utils . GetTimeWindow (account .Settings .Window )
123123 time .Sleep (time .Duration (timeWindow ) * unitWindow )
124124 }
125125}
126126
127127func (c * CoreHandler ) syncUp (account model.Account , repositoryData * model.RepositoryData , gitService IGitService ) {
128- utils .Log ( utils . LogLevelInfo , "[%s - %s (%s)] Syncing up" , account .ID .Hex (), account .Domain .Name , account .Environment )
128+ utils .LogInfo ( "[%s - %s (%s)] Syncing up" , account .ID .Hex (), account .Domain .Name , account .Environment )
129129
130130 // Update account status: Out of sync
131131 account .Domain .LastCommit = repositoryData .CommitHash
@@ -136,6 +136,9 @@ func (c *CoreHandler) syncUp(account model.Account, repositoryData *model.Reposi
136136 diff , snapshotApi , err := c .checkForChanges (account , repositoryData .Content )
137137
138138 if err != nil {
139+ utils .LogError ("[%s - %s (%s)] Failed to check for changes - %s" ,
140+ account .ID .Hex (), account .Domain .Name , account .Environment , err .Error ())
141+
139142 c .updateDomainStatus (account , model .StatusError , "Failed to check for changes - " + err .Error ())
140143 return
141144 }
@@ -144,7 +147,7 @@ func (c *CoreHandler) syncUp(account model.Account, repositoryData *model.Reposi
144147 changeSource , account , err := c .applyChanges (snapshotApi , account , repositoryData , diff , gitService )
145148
146149 if err != nil {
147- utils .Log ( utils . LogLevelError , "[%s - %s (%s)] Failed to apply changes [%s] - %s" ,
150+ utils .LogError ( "[%s - %s (%s)] Failed to apply changes [%s] - %s" ,
148151 account .ID .Hex (), account .Domain .Name , account .Environment , changeSource , err .Error ())
149152
150153 c .updateDomainStatus (account , model .StatusError , "Failed to apply changes [" + changeSource + "] - " + err .Error ())
@@ -181,7 +184,7 @@ func (c *CoreHandler) checkForChanges(account model.Account, content string) (mo
181184
182185func (c * CoreHandler ) applyChanges (snapshotApi model.Snapshot , account model.Account ,
183186 repositoryData * model.RepositoryData , diff model.DiffResult , gitService IGitService ) (string , model.Account , error ) {
184- utils .Log ( utils . LogLevelDebug , "[%s - %s (%s)] SnapshotAPI version: %s - SnapshotRepo version: %s" ,
187+ utils .LogDebug ( "[%s - %s (%s)] SnapshotAPI version: %s - SnapshotRepo version: %s" ,
185188 account .ID .Hex (), account .Domain .Name , account .Environment , fmt .Sprint (snapshotApi .Domain .Version ), fmt .Sprint (account .Domain .Version ))
186189
187190 err := error (nil )
@@ -192,7 +195,7 @@ func (c *CoreHandler) applyChanges(snapshotApi model.Snapshot, account model.Acc
192195 if c .isRepositoryOutSync (repositoryData , diff ) {
193196 account , err = c .applyChangesToRepository (account , snapshotApi , gitService )
194197 } else {
195- utils .Log ( utils . LogLevelInfo , "[%s - %s (%s)] Repository is up to date" ,
198+ utils .LogInfo ( "[%s - %s (%s)] Repository is up to date" ,
196199 account .ID .Hex (), account .Domain .Name , account .Environment )
197200
198201 account .Domain .Version = snapshotApi .Domain .Version
@@ -206,7 +209,7 @@ func (c *CoreHandler) applyChanges(snapshotApi model.Snapshot, account model.Acc
206209}
207210
208211func (c * CoreHandler ) applyChangesToAPI (account model.Account , repositoryData * model.RepositoryData , diff model.DiffResult ) model.Account {
209- utils .Log ( utils . LogLevelInfo , "[%s - %s (%s)] Pushing changes to API" , account .ID .Hex (), account .Domain .Name , account .Environment )
212+ utils .LogInfo ( "[%s - %s (%s)] Pushing changes to API" , account .ID .Hex (), account .Domain .Name , account .Environment )
210213
211214 // Removed deleted if force prune is disabled
212215 if ! account .Settings .ForcePrune {
@@ -223,7 +226,7 @@ func (c *CoreHandler) applyChangesToAPI(account model.Account, repositoryData *m
223226}
224227
225228func (c * CoreHandler ) applyChangesToRepository (account model.Account , snapshot model.Snapshot , gitService IGitService ) (model.Account , error ) {
226- utils .Log ( utils . LogLevelInfo , "[%s - %s (%s)] Pushing changes to repository" , account .ID .Hex (), account .Domain .Name , account .Environment )
229+ utils .LogInfo ( "[%s - %s (%s)] Pushing changes to repository" , account .ID .Hex (), account .Domain .Name , account .Environment )
227230
228231 // Remove version from domain
229232 snapshotContent := snapshot
@@ -245,7 +248,7 @@ func (c *CoreHandler) applyChangesToRepository(account model.Account, snapshot m
245248func (c * CoreHandler ) isOutSync (account model.Account , lastCommit string , snapshotVersionPayload string ) bool {
246249 snapshotVersion := c .apiService .NewDataFromJson ([]byte (snapshotVersionPayload )).Snapshot .Domain .Version
247250
248- utils .Log ( utils . LogLevelDebug , "[%s - %s (%s)] Checking account - Last commit: %s - Domain Version: %d - Snapshot Version: %d" ,
251+ utils .LogDebug ( "[%s - %s (%s)] Checking account - Last commit: %s - Domain Version: %d - Snapshot Version: %d" ,
249252 account .ID .Hex (), account .Domain .Name , account .Environment , account .Domain .LastCommit , account .Domain .Version , snapshotVersion )
250253
251254 return account .Domain .LastCommit == "" || // First sync
@@ -264,8 +267,3 @@ func (c *CoreHandler) updateDomainStatus(account model.Account, status string, m
264267 account .Domain .LastDate = time .Now ().Format (time .ANSIC )
265268 c .accountRepository .Update (& account )
266269}
267-
268- func getTimeWindow (window string ) (int , time.Duration ) {
269- duration , _ := time .ParseDuration (window )
270- return 1 , duration
271- }
0 commit comments