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