2 * ============LICENSE_START==========================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * ============LICENSE_END=============================================
20 * ====================================================================
23 package org.onap.music.main;
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;
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;
45 import com.datastax.driver.core.ResultSet;
46 import com.datastax.driver.core.Row;
49 public class CronJobManager {
51 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CronJobManager.class);
53 private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
56 @Scheduled(cron = "0 0 0 * * ?")
57 public void scheduleTaskWithFixedRate() {
58 logger.info("Executing cronjob to cleanup locks..", dateTimeFormatter.format(LocalDateTime.now()) );
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)");
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);
71 } catch (MusicServiceException e1) {
72 logger.error(EELFLoggerDelegate.errorLogger,e1.getMessage(),AppMessages.QUERYERROR,ErrorSeverity.CRITICAL, ErrorTypes.QUERYERROR);
76 pQuery = new PreparedQueryObject();
77 pQuery.appendQueryString(
78 "select * from admin.locks");
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) {
90 String new_id = id.substring(1);
92 MusicCore.deleteLock(new_id);
93 } catch (MusicLockingException e) {
94 logger.info(EELFLoggerDelegate.applicationLogger,
97 deleteKeys.append("'").append(id).append("'").append(",");
101 deleteKeys.deleteCharAt(deleteKeys.length()-1);
102 CachingUtil.deleteKeysFromDB(deleteKeys.toString());
104 } catch (MusicServiceException e) {
105 logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(),AppMessages.CACHEERROR,ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);