Update swagger.json and other updates.
[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 package org.onap.music.main;
23
24 import java.util.Iterator;
25 import java.util.Map;
26 import java.util.Map.Entry;
27 import java.util.concurrent.Executors;
28 import java.util.concurrent.ScheduledExecutorService;
29 import java.util.concurrent.TimeUnit;
30 import javax.servlet.ServletContextEvent;
31 import javax.servlet.ServletContextListener;
32 import javax.servlet.annotation.WebListener;
33
34 import org.onap.music.datastore.PreparedQueryObject;
35 import org.onap.music.eelf.logging.EELFLoggerDelegate;
36 import org.onap.music.exceptions.MusicLockingException;
37 import org.onap.music.exceptions.MusicServiceException;
38
39 import com.datastax.driver.core.ResultSet;
40 import com.datastax.driver.core.Row;
41
42 //@WebListener
43 public class CronJobManager implements ServletContextListener {
44
45     private ScheduledExecutorService scheduler;
46     private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CronJobManager.class);
47
48     @Override
49     public void contextInitialized(ServletContextEvent event) {
50         scheduler = Executors.newSingleThreadScheduledExecutor();
51         scheduler.scheduleAtFixedRate(new CachingUtil(), 0, 24, TimeUnit.HOURS);
52         PreparedQueryObject pQuery = new PreparedQueryObject();
53         String consistency = MusicUtil.EVENTUAL;
54         pQuery.appendQueryString("CREATE TABLE IF NOT EXISTS admin.locks ( lock_id text PRIMARY KEY, ctime text)");
55         try {
56             ResultType result = MusicCore.nonKeyRelatedPut(pQuery, consistency);
57         } catch (MusicServiceException e1) {
58             e1.printStackTrace();
59         }
60
61       //Zookeeper cleanup
62         scheduler.scheduleAtFixedRate(new Runnable() {
63             @Override
64             public void run() {
65                 deleteLocksFromDB();
66             }
67         } , 0, 24, TimeUnit.HOURS);
68     }
69
70     @Override
71     public void contextDestroyed(ServletContextEvent event) {
72         scheduler.shutdownNow();
73     }
74
75     public void deleteLocksFromDB() {
76         PreparedQueryObject 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                 e.printStackTrace();
106             }
107     }
108
109 }