source: nscp/modules/Scheduler/simple_scheduler.cpp @ 6822839

0.4.00.4.10.4.2
Last change on this file since 6822839 was 6822839, checked in by Michael Medin <michael@…>, 3 years ago

Fixed some unix issue

  • Property mode set to 100644
File size: 3.6 KB
Line 
1#include "simple_scheduler.hpp"
2
3#include <boost/bind.hpp>
4
5#include <unicode_char.hpp>
6
7namespace scheduler {
8
9        int simple_scheduler::add_task(target item) {
10                {
11                        boost::mutex::scoped_lock l(mutex_);
12                        item.id = ++target_id_;
13                        targets_[item.id] = item;
14                }
15                reschedule(item);
16                return item.id;
17        }
18        void simple_scheduler::remove_task(int id) {
19                boost::mutex::scoped_lock l(mutex_);
20                target_list_type::iterator it = targets_.find(id);
21                targets_.erase(it);
22        }
23        boost::optional<target> simple_scheduler::get_task(int id) {
24                boost::mutex::scoped_lock l(mutex_);
25                target_list_type::iterator it = targets_.find(id);
26                if (it == targets_.end())
27                        return boost::optional<target>();
28                return boost::optional<target>((*it).second);
29        }
30
31        void simple_scheduler::start() {
32                running_ = true;
33                if (!queue_.empty())
34                        start_thread();
35        }
36        void simple_scheduler::stop() {
37                running_ = false;
38                //if (!thread_)
39                //      return;
40                stop_requested_ = true;
41                threads_.interrupt_all();
42                threads_.join_all();
43                /*
44                if (!threads.join_all(boost::posix_time::seconds(5))) {
45                        std::wcout << _T("FAILED TO TERMINATE!!!") << std::endl;
46                } else {
47                        std::wcout << _T("THREAD TERMINATED NICELY!") << std::endl;
48                }
49                */
50        }
51
52        void simple_scheduler::start_thread() {
53                if (!running_)
54                        return;
55                stop_requested_ = false;
56                int missing_threads = thread_count_ - threads_.size();
57                if (missing_threads > 0 && missing_threads <= thread_count_) {
58                        for (int i=0;i<missing_threads;i++) {
59                                std::wcout << _T("***START_THREAD***") << std::endl;
60                                threads_.create_thread(boost::bind(&simple_scheduler::thread_proc, this));
61                        }
62                }
63                //thread_ = boost::shared_ptr<boost::thread>(new boost::thread(boost::bind(&simple_scheduler::thread_proc, this)));
64        }
65
66        void simple_scheduler::thread_proc() {
67                int iteration = 0;
68                schedule_queue_type::value_type instance;
69                while (!stop_requested_) {
70                        instance = queue_.pop();
71                        if (!instance)
72                                return;
73
74                        try {
75
76                                boost::posix_time::time_duration off = now() - (*instance).time;
77                                if (off.total_seconds() > 0) {
78                                        std::wcout << _T("MISSED IT!") << off.total_seconds() << std::endl;
79                                }
80                                boost::thread::sleep((*instance).time);
81                        } catch (boost::thread_interrupted  &e) {
82                                if (!queue_.push(*instance))
83                                        std::wcout << _T("ERROR") << std::endl;
84                                if (stop_requested_)
85                                        return;
86                                continue;
87                        } catch (...) {
88                                if (!queue_.push(*instance))
89                                        std::wcout << _T("ERROR") << std::endl;
90                                std::wcout << _T("ERROR!!!") << std::endl;
91                                return;
92                        }
93
94                        boost::posix_time::ptime now_time = now();
95                        boost::optional<target> item = get_task((*instance).schedule_id);
96                        if (item) {
97                                try {
98                                        if (handler_)
99                                                handler_->handle_schedule(*item);
100                                        reschedule(*item,now_time);
101                                } catch (...) {
102                                        std::wcout << _T("UNKNOWN ERROR RUNING TASK: ") << std::endl;
103                                        reschedule(*item);
104                                }
105                        } else {
106                                std::wcout << _T("Task not found: ") << (*instance).schedule_id << std::endl;
107                        }
108                }
109        }
110
111        void simple_scheduler::reschedule(target item) {
112                reschedule_wnext(item, now() + boost::posix_time::seconds(rand()%item.duration.total_seconds()));
113        }
114        void simple_scheduler::reschedule(target item, boost::posix_time::ptime now) {
115                reschedule_wnext(item, now + item.duration);
116        }
117        void simple_scheduler::reschedule_wnext(target item, boost::posix_time::ptime next) {
118                schedule_instance instance;
119                instance.schedule_id = item.id;
120                instance.time = next;
121                if (!queue_.push(instance)) {
122                        std::wcout << _T("ERROR") << std::endl;
123                }
124                start_thread();
125        }
126}
127
128
129
Note: See TracBrowser for help on using the repository browser.