From e600aebaae4ea6dffdac06a9b902f89b821bb15d Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Thu, 28 Sep 2023 07:47:06 +0200 Subject: [PATCH] add a RequestJobBase base class for requests jobs --- winixd/core/job.cpp | 75 ++++++++++++++++++--------- winixd/core/job.h | 13 +++-- winixd/core/pluginmsg.h | 7 --- winixd/requestjobs/requestjobbase.cpp | 72 +++++++++++++++++++++++++ winixd/requestjobs/requestjobbase.h | 71 +++++++++++++++++++++++++ 5 files changed, 203 insertions(+), 35 deletions(-) create mode 100644 winixd/requestjobs/requestjobbase.cpp create mode 100644 winixd/requestjobs/requestjobbase.h diff --git a/winixd/core/job.cpp b/winixd/core/job.cpp index f67fc31..b332333 100644 --- a/winixd/core/job.cpp +++ b/winixd/core/job.cpp @@ -150,6 +150,11 @@ void Job::Add(long job_id, Request * request, pt::Space & job, size_t priority) +void Job::RegisterRequestJob(long job_id, RequestJobBase & request_job) +{ + request_jobs.insert(std::make_pair(job_id, &request_job)); +} + size_t Job::Size(size_t priority) const { @@ -270,30 +275,7 @@ void Job::DoJob(JobTask & task, size_t priority) if( task.job_type == JobTask::JOB_TYPE_REQUEST_CONTINUATION ) { - if( task.request ) - { - Cur local_cur; - local_cur.request = task.request; - local_cur.request->run_state = Request::RunState::job_run; - local_cur.session = task.request->session; - local_cur.mount = task.request->mount; - - res = plugin->Call(model_connector, &log, &local_cur, WINIX_JOB, &task.job, nullptr, task.job_type, task.job_id); - log << logsave; - - { - Winix::Lock lock(synchro); - Cur old_cur = *cur; - *cur = local_cur; - DoRequestContinuationJob(task, priority); - *cur = old_cur; - } - } - else - { - log << log2 << "Job: request continuation task doesn't have a request set, skipping the job and request continuation" << logend; - log << log2 << "Job: this is an internal error, the request if exists in the queue will never be removed" << logend; - } + res = DoRequestJobs(task, task.job_id, priority); } else { @@ -314,6 +296,50 @@ void Job::DoJob(JobTask & task, size_t priority) } +// second thread (objects not locked) +PluginRes Job::DoRequestJobs(JobTask & task, long job_id, size_t priority) +{ + PluginRes res; + + if( task.request ) + { + auto jobs_iterator = request_jobs.lower_bound(job_id); + + while( jobs_iterator != request_jobs.end() && jobs_iterator->first == job_id ) + { + task.request->run_state = Request::RunState::job_run; + jobs_iterator->second->set_dependency(this); + jobs_iterator->second->set_request(task.request); + jobs_iterator->second->do_job(); + jobs_iterator++; + } + + Cur local_cur; + local_cur.request = task.request; + local_cur.request->run_state = Request::RunState::job_run; + local_cur.session = task.request->session; + local_cur.mount = task.request->mount; + + { + Winix::Lock lock(synchro); + log << logsave; + Cur old_cur = *cur; + *cur = local_cur; + DoRequestContinuationJob(task, priority); + *cur = old_cur; + } + } + else + { + log << log2 << "Job: request continuation task doesn't have a request set, skipping the job and request continuation" << logend; + log << log2 << "Job: this is an internal error, the request if exists in the queue will never be removed" << logend; + } + + return res; +} + + + // second thread (objects locked) // use main_log (after locking) for the logs to be in the correct order void Job::DoRequestContinuationJob(JobTask & job_task, size_t priority) @@ -396,7 +422,6 @@ void Job::RemoveOldRequest(Request * request) - } // namespace Winix diff --git a/winixd/core/job.h b/winixd/core/job.h index 6645163..110aa6e 100644 --- a/winixd/core/job.h +++ b/winixd/core/job.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2012-2022, Tomasz Sowa + * Copyright (c) 2012-2023, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -43,7 +43,7 @@ #include "cur.h" #include "loadavg.h" #include "mounts.h" - +#include "requestjobs/requestjobbase.h" namespace Winix @@ -84,6 +84,11 @@ public: void Add(long job_id, pt::Space & job, size_t priority = PRIORITY_DEFAULT); void Add(long job_id, Request * request, pt::Space & job, size_t priority = PRIORITY_DEFAULT); + /* + * register a new request job worker + */ + void RegisterRequestJob(long job_id, RequestJobBase & request_job); + /* queue size, and size of all jobs in any priority */ @@ -107,9 +112,10 @@ private: Mounts * mounts; std::list * req_tab; - typedef std::queue JobsQueue; + typedef std::queue JobsQueue; typedef std::vector JobsQueueTab; JobsQueueTab jobs_queue_tab; + std::multimap request_jobs; void CheckPriority(size_t & priority) const; void SaveToFile(); @@ -132,6 +138,7 @@ private: void DoRequestContinuationJob(JobTask & job_task, size_t priority); void DoWinixJob(pt::Space & job); void RemoveOldRequest(Request * request); + PluginRes DoRequestJobs(JobTask & task, long job_id, size_t priority); }; diff --git a/winixd/core/pluginmsg.h b/winixd/core/pluginmsg.h index 49a1802..12697bf 100644 --- a/winixd/core/pluginmsg.h +++ b/winixd/core/pluginmsg.h @@ -333,13 +333,6 @@ namespace Winix // session is null // if you process the job then return 'true' from the processing method (from plugin call) // so this prevent to make a standard (system) job -// in l1 there is a job type (from JobTask structure), e.g. if you want to know whether -// the job is a request continuation you should compare l1 == JobTask::JOB_TYPE_REQUEST_CONTINUATION -// in l2 there is a job_id - the values passed to the Job::Add(long job_id, ...) method -// if you have called Add(...) without long job_id parameter then the default value JobTask::JOB_ID_DEFAULT is used -// if the job type is JobTask::JOB_TYPE_REQUEST_CONTINUATION then a pointer to Session can be set, -// such a Session has allow_to_delete flag set to false but of course you have to Lock/Unlock when -// you are using this structure #define WINIX_JOB 31200 diff --git a/winixd/requestjobs/requestjobbase.cpp b/winixd/requestjobs/requestjobbase.cpp new file mode 100644 index 0000000..05c80f8 --- /dev/null +++ b/winixd/requestjobs/requestjobbase.cpp @@ -0,0 +1,72 @@ +/* + * This file is a part of Winix + * and is distributed under the 2-Clause BSD licence. + * Author: Tomasz Sowa + */ + +/* + * Copyright (c) 2023, Tomasz Sowa + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "requestjobbase.h" + + +namespace Winix +{ + + +RequestJobBase::RequestJobBase() +{ + req = nullptr; +} + + +RequestJobBase::~RequestJobBase() +{ +} + + + +void RequestJobBase::set_request(Request * req) +{ + this->req = req; +} + + +void RequestJobBase::do_job() +{ +} + + + + + + + +} // namespace Winix + + diff --git a/winixd/requestjobs/requestjobbase.h b/winixd/requestjobs/requestjobbase.h new file mode 100644 index 0000000..3f3abec --- /dev/null +++ b/winixd/requestjobs/requestjobbase.h @@ -0,0 +1,71 @@ +/* + * This file is a part of Winix + * and is distributed under the 2-Clause BSD licence. + * Author: Tomasz Sowa + */ + +/* + * Copyright (c) 2023, Tomasz Sowa + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef headerfile_winix_requestjobs_requestjobbase +#define headerfile_winix_requestjobs_requestjobbase + +#include "core/winixmodeldeprecated.h" +#include "core/request.h" + + +namespace Winix +{ + + +class RequestJobBase : public WinixModelDeprecated +{ +public: + + RequestJobBase(); + virtual ~RequestJobBase(); + + void set_request(Request * req); + virtual void do_job(); + + +protected: + + Request * req; + + +}; + + + +} // namespace Winix + + + +#endif +