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 #include "cycle_task_runner.h"
17 #include <cstdint>
18 #include <iomanip>
19 #include <sstream>
20 #include "cloud_pref_impl.h"
21 #include "cycle_task.h"
22 #include "dfs_error.h"
23 #include "tasks/optimize_storage_task.h"
24 #include "tasks/periodic_check_task.h"
25 #include "tasks/save_subscription_task.h"
26 #include "utils_log.h"
27
28 #include "os_account_manager.h"
29
30 namespace OHOS {
31 namespace FileManagement {
32 namespace CloudSync {
33 using namespace std;
34
35 const std::string CycleTaskRunner::FILE_PATH = CLOUDFILE_DIR + "/cycletask";
36 const int32_t CycleTaskRunner::DEFAULT_VALUE = 0;
37 const int32_t CycleTaskRunner::DEFAULT_USER_ID = 100;
38
CycleTaskRunner(std::shared_ptr<DataSyncManager> dataSyncManager)39 CycleTaskRunner::CycleTaskRunner(std::shared_ptr<DataSyncManager> dataSyncManager)
40 {
41 dataSyncManager_ = dataSyncManager;
42 cloudPrefImpl_ = std::make_unique<CloudPrefImpl>(FILE_PATH);
43 vector<int32_t> activeUsers;
44 if (AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeUsers) != E_OK || activeUsers.empty()) {
45 LOGE("query active user failed");
46 return;
47 }
48
49 userId_ = activeUsers.front();
50 setUpTime_ = std::time(nullptr);
51 InitTasks();
52 }
53
StartTask(string & reason)54 void CycleTaskRunner::StartTask(string &reason)
55 {
56 if (userId_ == DEFAULT_VALUE) {
57 LOGI("defaukt userId skip tasks");
58 cloudPrefImpl_->SetInt("userId", DEFAULT_USER_ID);
59 return;
60 }
61
62 for (const auto &task_data : cycleTasks_) {
63 time_t lastRunTime = DEFAULT_VALUE;
64 GetLastRunTime(task_data->GetTaskName(), lastRunTime);
65
66 if (difftime(setUpTime_, lastRunTime) > task_data->GetIntervalTime()) {
67 if (lastRunTime == DEFAULT_VALUE) {
68 LOGI("skip first run task, taskName is %{public}s", task_data->GetTaskName().c_str());
69 SetLastRunTime(task_data->GetTaskName(), setUpTime_);
70 continue;
71 }
72 } else if (task_data->GetTaskName() == PeriodicCheckTaskName) {
73 bool force;
74 cloudPrefImpl_->GetBool(ForcePeriodicCheck, force);
75 if (!force) {
76 continue;
77 } else {
78 cloudPrefImpl_->SetBool(ForcePeriodicCheck, false);
79 }
80 }
81
82 LOGI("run task, task name is %{public}s", task_data->GetTaskName().c_str());
83 int32_t ret = task_data->RunTask(userId_);
84 if (ret == E_OK) {
85 LOGI("task run success, taskName is %{public}s, ret = %{public}d",
86 task_data->GetTaskName().c_str(), ret);
87 SetLastRunTime(task_data->GetTaskName(), setUpTime_);
88 } else {
89 LOGE("task run fail, taskName is %{public}s, ret = %{public}d",
90 task_data->GetTaskName().c_str(), ret);
91 }
92 }
93 }
94
InitTasks()95 void CycleTaskRunner::InitTasks()
96 {
97 //push tasks here
98 cycleTasks_.push_back(std::make_shared<OptimizeStorageTask>(dataSyncManager_));
99 cycleTasks_.push_back(std::make_shared<PeriodicCheckTask>(dataSyncManager_));
100 cycleTasks_.push_back(std::make_shared<SaveSubscriptionTask>(dataSyncManager_));
101 }
102
GetLastRunTime(std::string taskName,std::time_t & time)103 void CycleTaskRunner::GetLastRunTime(std::string taskName, std::time_t &time)
104 {
105 cloudPrefImpl_->GetLong("lastRunTime-" + taskName, time);
106 }
107
SetLastRunTime(std::string taskName,std::time_t time)108 void CycleTaskRunner::SetLastRunTime(std::string taskName, std::time_t time)
109 {
110 cloudPrefImpl_->SetLong("lastRunTime-" + taskName, time);
111 }
112
113 } // namespace CloudSync
114 } // namespace FileManagement
115 } // namespace OHOS