Push variuos changes
[music.git] / src / main / java / org / onap / music / main / CronJobManager.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2017 AT&T Intellectual Property
6  * ===================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  * 
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  * 
19  * ============LICENSE_END=============================================
20  * ====================================================================
21  */
22
23 package org.onap.music.main;
24
25 import java.time.LocalDateTime;
26 import java.time.format.DateTimeFormatter;
27 import java.util.Iterator;
28 import java.util.concurrent.Executors;
29 import java.util.concurrent.ScheduledExecutorService;
30 import java.util.concurrent.TimeUnit;
31 import javax.servlet.ServletContextEvent;
32 import javax.servlet.ServletContextListener;
33 import javax.servlet.annotation.WebListener;
34
35 import org.onap.music.datastore.PreparedQueryObject;
36 import org.onap.music.eelf.logging.EELFLoggerDelegate;
37 import org.onap.music.eelf.logging.format.AppMessages;
38 import org.onap.music.eelf.logging.format.ErrorSeverity;
39 import org.onap.music.eelf.logging.format.ErrorTypes;
40 import org.onap.music.exceptions.MusicLockingException;
41 import org.onap.music.exceptions.MusicServiceException;
42 import org.springframework.scheduling.annotation.Scheduled;
43 import org.springframework.stereotype.Component;
44
45 import com.datastax.driver.core.ResultSet;
46 import com.datastax.driver.core.Row;
47
48 @Component
49 public class CronJobManager {
50
51     private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CronJobManager.class);
52
53     private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
54
55    
56     @Scheduled(cron = "0 0 0 * * ?")
57     public void scheduleTaskWithFixedRate() {
58         logger.info("Executing cronjob to cleanup locks..", dateTimeFormatter.format(LocalDateTime.now()) );
59         deleteLocksFromDB();
60     }
61     
62     public void deleteLocksFromDB() {
63         PreparedQueryObject pQuery = new PreparedQueryObject();
64         String consistency = MusicUtil.EVENTUAL;
65         pQuery.appendQueryString("CREATE TABLE IF NOT EXISTS admin.locks ( lock_id text PRIMARY KEY, ctime text)");
66         try {
67             ResultType result = MusicCore.nonKeyRelatedPut(pQuery, consistency);
68             if ( result.equals(ResultType.FAILURE)) {
69                 logger.error(EELFLoggerDelegate.errorLogger,"Error creating Admin.locks table.",AppMessages.QUERYERROR,ErrorSeverity.CRITICAL, ErrorTypes.QUERYERROR);
70             }
71         } catch (MusicServiceException e1) {
72             logger.error(EELFLoggerDelegate.errorLogger,e1.getMessage(),AppMessages.QUERYERROR,ErrorSeverity.CRITICAL, ErrorTypes.QUERYERROR);
73             e1.printStackTrace();
74         }
75         
76         pQuery = new PreparedQueryObject();
77         pQuery.appendQueryString(
78                         "select * from admin.locks");
79             try {
80                 ResultSet rs = MusicCore.get(pQuery);
81                 Iterator<Row> it = rs.iterator();
82                 StringBuilder deleteKeys = new StringBuilder();
83                 Boolean expiredKeys = false;
84                 while (it.hasNext()) {
85                     Row row = (Row) it.next();
86                     String id = row.getString("lock_id");
87                     long ctime = Long.parseLong(row.getString("ctime"));
88                     if(System.currentTimeMillis() >= ctime + 24 * 60 * 60 * 1000) {
89                         expiredKeys = true;
90                         String new_id = id.substring(1);
91                         try {
92                             MusicCore.deleteLock(new_id);
93                         } catch (MusicLockingException e) {
94                             logger.info(EELFLoggerDelegate.applicationLogger,
95                                      e.getMessage());
96                         }
97                         deleteKeys.append("'").append(id).append("'").append(",");
98                     }
99                 }
100                 if(expiredKeys) {
101                     deleteKeys.deleteCharAt(deleteKeys.length()-1);
102                     CachingUtil.deleteKeysFromDB(deleteKeys.toString());
103                }
104             } catch (MusicServiceException e) {
105                 logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(),AppMessages.CACHEERROR,ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
106                 e.printStackTrace();
107             }
108     }
109 }