2 * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <aul_job_scheduler.h>
22 #include "job-manager.hh"
23 #include "log-private.hh"
27 void JobManager::AddJobHandler(JobHandler* handler) {
28 handlers_.push_back(handler);
31 void JobManager::RemoveJobHandler(JobHandler* handler) {
32 auto found = std::find(handlers_.begin(), handlers_.end(), handler);
33 if (found == handlers_.end())
36 handlers_.erase(found);
39 bool JobManager::ExistJobHandler(const std::string& job_id) {
40 for (auto* h : handlers_) {
41 if (h->GetJobId() == job_id)
48 bool JobManager::ExistJobHandler(JobHandler* handler) {
49 for (auto* h : handlers_) {
57 void JobManager::Do(std::shared_ptr<Job> job) {
58 job->SetEventListener(this);
59 if (!ExistJobHandler(job->GetId())) {
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(),
72 if (ret != AUL_R_OK) {
73 _E("aul_job_scheduler_update_job_status() is failed. error(%d)", ret);
80 void JobManager::FinishAllJobs() {
81 running_jobs_.clear();
82 pending_jobs_.clear();
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) {
90 iter = pending_jobs_.erase(iter);
100 void JobManager::AddRunningJob(std::shared_ptr<Job> job) {
101 running_jobs_.push_back(job);
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);
114 void JobManager::AddPendingJob(std::shared_ptr<Job> job) {
115 pending_jobs_.push_back(job);
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);
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());
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())
144 for (auto* h : handlers_) {
145 if (h->GetJobId() == job->GetId())
148 aul_job_scheduler_update_job_status(job->GetId().c_str(),
151 _D("[__JOB__] END. Job(%s), Status(%d)",
152 job->GetId().c_str(), job->GetStatus());
153 RemoveRunningJob(job->GetId());
156 } // namespace tizen_cpp