3293c117be23d874afdfcdd53588383f2bfe4122
[platform/core/appfw/appcore-agent.git] / src / job-manager.cc
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <aul.h>
18 #include <aul_job_scheduler.h>
19
20 #include <algorithm>
21
22 #include "job-manager.hh"
23 #include "log-private.hh"
24
25 namespace tizen_cpp {
26
27 void JobManager::AddJobHandler(JobHandler* handler) {
28   handlers_.push_back(handler);
29 }
30
31 void JobManager::RemoveJobHandler(JobHandler* handler) {
32   auto found = std::find(handlers_.begin(), handlers_.end(), handler);
33   if (found == handlers_.end())
34     return;
35
36   handlers_.erase(found);
37 }
38
39 bool JobManager::ExistJobHandler(const std::string& job_id) {
40   for (auto* h : handlers_) {
41     if (h->GetJobId() == job_id)
42       return true;
43   }
44
45   return false;
46 }
47
48 bool JobManager::ExistJobHandler(JobHandler* handler) {
49   for (auto* h :  handlers_) {
50     if (h == handler)
51       return true;
52   }
53
54   return false;
55 }
56
57 void JobManager::Do(std::shared_ptr<Job> job) {
58   job->SetEventListener(this);
59   if (!ExistJobHandler(job->GetId())) {
60     job->SetTimer();
61     AddPendingJob(job);
62   } else {
63     job->SetIdler();
64     AddRunningJob(job);
65   }
66 }
67
68 int JobManager::Finish(const std::string& job_id) {
69   RemoveRunningJob(job_id);
70   int ret = aul_job_scheduler_update_job_status(job_id.c_str(),
71       JOB_STATUS_FINISHED);
72   if (ret != AUL_R_OK) {
73     _E("aul_job_scheduler_update_job_status() is failed. error(%d)", ret);
74     return ret;
75   }
76
77   return 0;
78 }
79
80 void JobManager::FinishAllJobs() {
81   running_jobs_.clear();
82   pending_jobs_.clear();
83 }
84
85 void JobManager::FlushPendingJob(const std::string& job_id) {
86   auto iter = pending_jobs_.begin();
87   while (iter != pending_jobs_.end()) {
88     if ((*iter)->GetId() == job_id) {
89       auto job = *iter;
90       iter = pending_jobs_.erase(iter);
91       job->UnsetTimer();
92       AddPendingJob(job);
93       job->SetIdler();
94     } else {
95       iter++;
96     }
97   }
98 }
99
100 void JobManager::AddRunningJob(std::shared_ptr<Job> job) {
101   running_jobs_.push_back(job);
102 }
103
104 void JobManager::RemoveRunningJob(const std::string& job_id) {
105   auto iter = running_jobs_.begin();
106   while (iter != running_jobs_.end()) {
107     if ((*iter)->GetId() == job_id)
108       iter = running_jobs_.erase(iter);
109     else
110       iter++;
111   }
112 }
113
114 void JobManager::AddPendingJob(std::shared_ptr<Job> job) {
115   pending_jobs_.push_back(job);
116 }
117
118 void JobManager::RemovePendingJob(const std::string& job_id) {
119   auto iter = pending_jobs_.begin();
120   while (iter != pending_jobs_.end()) {
121     if ((*iter)->GetId() == job_id)
122       iter = pending_jobs_.erase(iter);
123     else
124       iter++;
125   }
126 }
127
128 void JobManager::OnTimedOut(const Job* job) {
129   _E("[__TIMEDOUT__] Job(%s), Status(%d)",
130       job->GetId().c_str(), job->GetStatus());
131   RemovePendingJob(job->GetId());
132 }
133
134 void JobManager::OnRun(const Job* job) {
135   _D("[__JOB__] START. Job(%s), Status(%d)",
136       job->GetId().c_str(), job->GetStatus());
137   if (job->GetStatus() == SERVICE_APP_JOB_STATUS_START) {
138     aul_job_scheduler_update_job_status(job->GetId().c_str(), JOB_STATUS_START);
139     for (auto* h : handlers_) {
140       if (h->GetJobId() == job->GetId())
141         h->Do(job);
142     }
143   } else {
144     for (auto* h : handlers_) {
145       if (h->GetJobId() == job->GetId())
146         h->Do(job);
147     }
148     aul_job_scheduler_update_job_status(job->GetId().c_str(),
149         JOB_STATUS_STOPPED);
150   }
151   _D("[__JOB__] END. Job(%s), Status(%d)",
152       job->GetId().c_str(), job->GetStatus());
153   RemoveRunningJob(job->GetId());
154 }
155
156 }  // namespace tizen_cpp