Delay server ready timing until creating cache
[platform/core/appfw/pkgmgr-info.git] / src / server / request_handler / create_cache_request_handler.cc
1 // Copyright (c) 2021 - 2022 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache-2.0 license that can be
3 // found in the LICENSE file.
4
5 #include "create_cache_request_handler.hh"
6
7 #include <sys/resource.h>
8
9 #include <string>
10
11 #include "cache_db_handler.hh"
12 #include "utils/logging.hh"
13
14 namespace psd = pkgmgr_server::database;
15
16 namespace pkgmgr_server {
17 namespace request_handler {
18
19 CreateCacheRequestHandler::Scheduler::Scheduler(pid_t tid)
20     : tid_(tid), priority_(0) {
21   if (Get(&priority_) != 0)
22     priority_ = 0;
23 }
24
25 void CreateCacheRequestHandler::Scheduler::ChangePriority() {
26   if (Set(-10) != 0)
27     return;
28
29   int priority = 0;
30   if (Get(&priority) == 0)
31     LOG(WARNING) << "Current priority: " << priority;
32 }
33
34 void CreateCacheRequestHandler::Scheduler::ResetPriority() {
35   if (Set(priority_) != 0)
36     return;
37
38   int priority = 0;
39   if (Get(&priority) == 0)
40     LOG(WARNING) << "Current priority: " << priority;
41 }
42
43 int CreateCacheRequestHandler::Scheduler::Get(int* priority) {
44   errno = 0;
45   *priority = getpriority(PRIO_PROCESS, 0);
46   if (errno != 0) {
47     LOG(ERROR) << "getpriority() is failed. errno: " << errno;
48     return -1;
49   }
50
51   return 0;
52 }
53
54 int CreateCacheRequestHandler::Scheduler::Set(int priority) {
55   if (setpriority(PRIO_PROCESS, 0, priority) != 0) {
56     LOG(ERROR) << "setpriority() is failed. , errno: " << errno;
57     return -1;
58   }
59
60   LOG(WARNING) << "priority: " << priority;
61   return 0;
62 }
63
64 CreateCacheRequestHandler::CreateCacheRequestHandler(): scheduler_(gettid()) {
65 }
66
67 bool CreateCacheRequestHandler::HandleRequest(const unsigned char* data, int size,
68                                             const std::string& locale) {
69   psd::CacheDBHandler db(GetUID(), GetPID());
70   db.SetLocale(locale);
71
72   success_ = (db.Execute() == PMINFO_R_OK);
73   return success_;
74 }
75
76 std::vector<uint8_t> CreateCacheRequestHandler::ExtractResult() {
77   return { static_cast<uint8_t>(success_) };
78 }
79
80 void CreateCacheRequestHandler::PreExec() {
81   scheduler_.ChangePriority();
82 }
83
84 void CreateCacheRequestHandler::PostExec() {
85   scheduler_.ResetPriority();
86 }
87
88 }  // namespace request_handler
89 }  // namespace pkgmgr_server