Leosac  0.8.0
Open Source Access Control
Task.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "Task.hpp"
22 #include "tools/GenGuid.h"
23 #include "tools/log.hpp"
24 
25 using namespace Leosac::Tasks;
26 
28  : on_completion_([]() {})
29  , on_success_([]() {})
30  , on_failure_([]() {})
31  , success_(false)
32  , eptr_(nullptr)
33  , complete_(false)
34  , guid_(Leosac::gen_uuid())
35 {
36 }
37 
38 bool Task::is_complete() const
39 {
40  return complete_.load(std::memory_order::memory_order_acquire);
41 }
42 
43 void Task::run()
44 {
45  try
46  {
47  success_ = do_run();
48  }
49  catch (std::exception &e)
50  {
51  WARN(
52  "Task throwed an exception. It'll be swallowed but will still be stored "
53  "in `eptr_`");
55  eptr_ = std::current_exception();
56  }
57  if (success_)
58  on_success_();
59  else
60  on_failure_();
62 
63  // Re-initialize the lambdas to an empty lambda.
64  // This is important because it destroys copied parameter, including potential
65  // shared_ptr that would otherwise cause cyclic references.
66  on_success_ = []() {};
67  on_failure_ = []() {};
68  on_completion_ = []() {};
69 
70  {
71  mutex_.lock();
72  complete_.store(true, std::memory_order::memory_order_release);
73  mutex_.unlock();
74  cv_.notify_all();
75  }
76  INFO("Task ~" << guid_ << "~ completed "
77  << (success_ ? "successfully" : "with error."));
78 }
79 
80 void Task::wait()
81 {
82  std::unique_lock<std::mutex> ul(mutex_);
83  cv_.wait(ul, [&]() {
84  return complete_.load(std::memory_order::memory_order_acquire);
85  });
86 }
87 
88 bool Task::succeed() const
89 {
90  return success_;
91 }
92 
93 std::exception_ptr Task::get_exception() const
94 {
95  return eptr_;
96 }
97 
98 const std::string &Task::get_guid() const
99 {
100  return guid_;
101 }
Leosac::print_exception
void print_exception(const std::exception &e, int level=0)
Recursively print the exception trace to std::cerr.
Definition: ExceptionsTools.cpp:44
Leosac::Tasks::Task::on_completion_
std::function< void(void)> on_completion_
Definition: Task.hpp:111
Leosac::Tasks::Task::mutex_
std::mutex mutex_
Definition: Task.hpp:118
WARN
@ WARN
Definition: log.hpp:33
Leosac::Tasks::Task::run
void run()
Definition: Task.cpp:43
INFO
@ INFO
Definition: log.hpp:34
ExceptionsTools.hpp
Leosac::Tasks::Task::do_run
virtual bool do_run()=0
Leosac::Tasks::Task::on_success_
std::function< void(void)> on_success_
Definition: Task.hpp:112
Leosac::Tasks::Task::eptr_
std::exception_ptr eptr_
Definition: Task.hpp:116
Leosac::Tasks::Task::guid_
std::string guid_
Definition: Task.hpp:121
Leosac::Tasks::Task::cv_
std::condition_variable cv_
Definition: Task.hpp:120
GenGuid.h
Leosac::Tasks::Task::wait
void wait()
Instead of spinlocking over is_complete() one can call wait() to hum...
Definition: Task.cpp:80
Leosac::gen_uuid
std::string gen_uuid()
Generate a new UUID.
Definition: GenGuid.cpp:26
Leosac::Tasks::Task::on_failure_
std::function< void(void)> on_failure_
Definition: Task.hpp:113
Leosac::Tasks::Task::Task
Task()
Definition: Task.cpp:27
Leosac::Tasks::Task::complete_
std::atomic_bool complete_
Definition: Task.hpp:119
Leosac::Tasks::Task::success_
bool success_
Definition: Task.hpp:115
Leosac::Tasks::Task::is_complete
bool is_complete() const
Has the tasks completed its execution.
Definition: Task.cpp:38
log.hpp
Task.hpp
Leosac::Tasks
Definition: FetchRemoteConfig.hpp:28
Leosac::Tasks::Task::get_guid
const std::string & get_guid() const
Definition: Task.cpp:98
Leosac::Tasks::Task::get_exception
std::exception_ptr get_exception() const
Definition: Task.cpp:93
Leosac::Tasks::Task::succeed
bool succeed() const
Definition: Task.cpp:88