1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef SCHEDULER_MANAGER_H 17 #define SCHEDULER_MANAGER_H 18 19 #include <memory> 20 21 #include "db_delegate.h" 22 #include "executor_pool.h" 23 #include "subscriber_managers/rdb_subscriber_manager.h" 24 25 namespace OHOS::DataShare { 26 class SchedulerManager { 27 public: 28 static SchedulerManager &GetInstance(); 29 void Execute(const std::string &uri, const int32_t userId, DistributedData::StoreMetaData &metaData); 30 void Execute(const Key &key, const int32_t userId, const DistributedData::StoreMetaData &metaData); 31 void ReExecuteAll(); 32 void SetTimer(const int32_t userId, DistributedData::StoreMetaData &metaData, const Key &key, int64_t reminderTime); 33 void RemoveTimer(const Key &key); 34 void ClearTimer(); 35 void SetExecutorPool(std::shared_ptr<ExecutorPool> executor); 36 void Start(const Key &key, int32_t userId, const DistributedData::StoreMetaData &metaData); 37 void Stop(const Key &key); 38 void Enable(const Key &key, int32_t userId, const DistributedData::StoreMetaData &metaData); 39 void Disable(const Key &key); 40 41 private: 42 static constexpr const char *REMIND_TIMER_FUNC = "remindTimer("; 43 static constexpr int REMIND_TIMER_FUNC_LEN = 12; 44 SchedulerManager() = default; 45 ~SchedulerManager() = default; 46 static void GenRemindTimerFuncParams(const int32_t userId, DistributedData::StoreMetaData &metaData, const Key &key, 47 std::string &schedulerSQL); 48 void ExecuteSchedulerSQL(const int32_t userId, DistributedData::StoreMetaData &metaData, const Key &key, 49 std::shared_ptr<DBDelegate> delegate); 50 bool SetTimerTask(uint64_t &timerId, const std::function<void()> &callback, int64_t reminderTime); 51 void DestoryTimerTask(int64_t timerId); 52 void ResetTimerTask(int64_t timerId, int64_t reminderTime); 53 int64_t EraseTimerTaskId(const Key &key); 54 bool GetSchedulerStatus(const Key &key); 55 56 std::mutex mutex_; 57 std::map<Key, int64_t> timerCache_; 58 std::map<Key, bool> schedulerStatusCache_; 59 std::shared_ptr<ExecutorPool> executor_ = nullptr; 60 }; 61 } // namespace OHOS::DataShare 62 #endif // SCHEDULER_MANAGER_H 63