Add OrphanedJobCleaner to remove the jobs owned by uninstalled apps 88/143088/2
authorMu-Woong Lee <muwoong.lee@samsung.com>
Tue, 8 Aug 2017 10:50:23 +0000 (19:50 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Tue, 8 Aug 2017 11:04:09 +0000 (20:04 +0900)
Change-Id: I8176b3558e915ad18f41c6852f39f41b011c8791
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
include/private/JobSchedulerService.h
src/server/JobManager.cpp
src/server/JobManager.h
src/server/JobSchedulerService.cpp
src/server/OrphanedJobCleaner.cpp [new file with mode: 0644]
src/server/OrphanedJobCleaner.h [new file with mode: 0644]

index 5f76842985110d8d878fed031a4d840b9e5ea07d..31af550f69d8ad37f70003e13509cc56eaad95c4 100644 (file)
@@ -17,6 +17,7 @@
 #ifndef __CONTEXT_JOB_SCHEDULER_SERVICE_H__
 #define __CONTEXT_JOB_SCHEDULER_SERVICE_H__
 
+#include <vector>
 #include <ISystemService.h>
 #include <IServiceRunner.h>
 #include <IClient.h>
@@ -45,6 +46,7 @@ namespace ctx {
 
                // Own members
                JobManager* getJobManager(uid_t uid);
+               std::vector<JobManager*> getAllJobManager();
 
        private:
                IServiceRunner* __serviceRunner;
index 306b03df0ff93439072ac06a15b454c3a19db995..c07d31753474e51703262b65245800465e45144b 100644 (file)
@@ -283,6 +283,29 @@ std::vector<JobInfo*> JobManager::getAllJobInfo(IClient* owner)
        return jobInfos;
 }
 
+void JobManager::purgeJobOf(const std::string& ownerId)
+{
+       _I("Purge all jobs of %s from %u", ownerId.c_str(), __uid);
+
+       std::vector<JobRunner*> toPurge;
+
+       for (auto& runner : __jobRunners) {
+               if (runner->getOwner() == ownerId) {
+                       toPurge.push_back(runner);
+               }
+       }
+
+       for (auto& runner : toPurge) {
+               if (runner->isStarted())
+                       runner->stop();
+
+               if (runner->isPersistent())
+                       __jobInfoDatabase.remove(runner->getJobId());
+
+               __removeRunner(runner);
+       }
+}
+
 void JobManager::addCustom(const std::string& uri, const std::string& ownerId)
 {
        std::string key = uri + "@" + ownerId;
index 3ac2270d21251d9262c5a9a6cb325a5aa25aa260..86920b9ba3a779c8ab26930f6076acf994d5dfcb 100644 (file)
@@ -50,6 +50,8 @@ namespace ctx {
                JobInfo* getJobInfo(int jobId, IClient* owner);
                std::vector<JobInfo*> getAllJobInfo(IClient* owner);
 
+               void purgeJobOf(const std::string& ownerId);
+
                // For supporting Context-Trigger's custom context registration APIs
                void addCustom(const std::string& uri, const std::string& ownerId);
                void removeCustom(const std::string& uri, const std::string& ownerId);
index 78eedcb80621a54c2e9c84063c6939fdc1bdde8f..d23bcb98f2af1f0423c7033e68c536b86fbc1fee 100644 (file)
 #include "MethodCallHandler.h"
 #include "ContextManager.h"
 #include "SchedTimer.h"
+#include "OrphanedJobCleaner.h"
 
 #define SYSTEM_UID     0
 
 using namespace ctx;
 
 static std::map<uid_t, JobManager*> __jobManagers;
+static OrphanedJobCleaner* __orphanedJobCleaner = NULL;
 
 JobSchedulerService::JobSchedulerService() :
        __serviceRunner(NULL)
@@ -63,11 +65,13 @@ bool JobSchedulerService::prepare()
        ContextManager::init();
        SchedTimer::init();
        __jobManagers.emplace(SYSTEM_UID, new JobManager(SYSTEM_UID));
+       __orphanedJobCleaner = new OrphanedJobCleaner(this);
        return true;
 }
 
 void JobSchedulerService::cleanup()
 {
+       delete __orphanedJobCleaner;
        for (auto& it : __jobManagers) {
                delete it.second;
        }
@@ -100,3 +104,14 @@ JobManager* JobSchedulerService::getJobManager(uid_t uid)
 
        return it->second;
 }
+
+std::vector<JobManager*> JobSchedulerService::getAllJobManager()
+{
+       std::vector<JobManager*> managers;
+
+       for (auto& it : __jobManagers) {
+               managers.push_back(it.second);
+       }
+
+       return managers;
+}
diff --git a/src/server/OrphanedJobCleaner.cpp b/src/server/OrphanedJobCleaner.cpp
new file mode 100644 (file)
index 0000000..66eabe9
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <ServerUtil.h>
+#include <SharedUtil.h>
+#include <JobSchedulerService.h>
+#include "JobManager.h"
+#include "OrphanedJobCleaner.h"
+
+#define GLOBAL_APP_UID 376
+
+using namespace ctx;
+
+OrphanedJobCleaner::OrphanedJobCleaner(JobSchedulerService* hostService) :
+       __hostService(hostService)
+{
+       __dbusMonitor.subscribe(NULL,
+                       "/org/tizen/pkgmgr/signal", "org.tizen.pkgmgr.signal", "uninstall", this);
+}
+
+OrphanedJobCleaner::~OrphanedJobCleaner()
+{
+}
+
+void OrphanedJobCleaner::onSignal(const std::string& sender, const std::string& objPath,
+               const std::string& interface, const std::string& signalName, GVariant *param)
+{
+       uint32_t uid = 0;
+       const char *pkgId = NULL;
+       const char *key = NULL;
+       const char *val = NULL;
+
+       g_variant_get(param, "(u&s&s&s&s&s&s)", &uid, NULL, NULL, &pkgId, NULL, &key, &val);
+       IF_FAIL_VOID(pkgId && STR_EQ(key, "end") && STR_EQ(val, "ok"));
+
+       _I("%s has been removed from %u", pkgId, uid);
+
+       if (uid != GLOBAL_APP_UID) {
+               JobManager* manager = __hostService->getJobManager(static_cast<uid_t>(uid));
+               if (manager)
+                       manager->purgeJobOf(pkgId);
+               return;
+       }
+
+       std::vector<JobManager*> managers = __hostService->getAllJobManager();
+
+       for (auto& manager : managers) {
+               manager->purgeJobOf(pkgId);
+       }
+}
diff --git a/src/server/OrphanedJobCleaner.h b/src/server/OrphanedJobCleaner.h
new file mode 100644 (file)
index 0000000..58c52cd
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __CONTEXT_JOB_SCHEDULER_ORPHANED_JOB_CLEANER_H__
+#define __CONTEXT_JOB_SCHEDULER_ORPHANED_JOB_CLEANER_H__
+
+#include <DBusMonitor.h>
+#include <IDBusSignalListener.h>
+
+namespace ctx {
+
+       class JobSchedulerService;
+
+       class OrphanedJobCleaner : public IDBusSignalListener {
+       public:
+               OrphanedJobCleaner(JobSchedulerService* hostService);
+               ~OrphanedJobCleaner();
+
+               void onSignal(const std::string& sender, const std::string& objPath,
+                               const std::string& interface, const std::string& signalName, GVariant *param);
+
+       private:
+               JobSchedulerService* __hostService;
+               DBusMonitor __dbusMonitor;
+       };
+
+}
+
+#endif