MongoDB  2.6.0
background.h
1 // @file background.h
2 
3 /* Copyright 2009 10gen Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 
20 #include <boost/scoped_ptr.hpp>
21 #include <string>
22 #include <vector>
23 
24 #include "mongo/base/status.h"
25 
26 namespace mongo {
27 
44  class BackgroundJob : boost::noncopyable {
45  protected:
52  explicit BackgroundJob(bool selfDelete = false);
53 
54  virtual std::string name() const = 0;
55 
66  virtual void run() = 0;
67 
68  public:
69  enum State {
70  NotStarted,
71  Running,
72  Done
73  };
74 
75  virtual ~BackgroundJob();
76 
84  void go();
85 
86 
94  Status cancel();
95 
105  bool wait( unsigned msTimeOut = 0 );
106 
107  // accessors. Note that while the access to the internal state is synchronized within
108  // these methods, there is no guarantee that the BackgroundJob is still in the
109  // indicated state after returning.
110  State getState() const;
111  bool running() const;
112 
113  private:
114  const bool _selfDelete;
115 
116  struct JobStatus;
117  const boost::scoped_ptr<JobStatus> _status;
118 
119  void jobBody();
120  };
121 
131  class PeriodicTask {
132  public:
133  PeriodicTask();
134  virtual ~PeriodicTask();
135 
136  virtual void taskDoWork() = 0;
137  virtual std::string taskName() const = 0;
138 
146  static void startRunningPeriodicTasks();
147 
157  static Status stopRunningPeriodicTasks( int gracePeriodMillis );
158  };
159 
160 
161 
162 
163 } // namespace mongo
static void startRunningPeriodicTasks()
Starts the BackgroundJob that runs PeriodicTasks.
Definition: background.cpp:256
BackgroundJob(bool selfDelete=false)
sub-class must instantiate the BackgroundJob
Definition: background.cpp:131
Background thread dispatching.
Definition: background.h:44
bool wait(unsigned msTimeOut=0)
wait for completion.
Definition: background.cpp:209
Status cancel()
If the job has not yet started, transitions the job to the &#39;done&#39; state immediately, such that subsequent calls to &#39;go&#39; are ignored, and notifies any waiters waiting in &#39;wait&#39;.
Definition: background.cpp:194
void go()
starts job.
Definition: background.cpp:180
these run &quot;roughly&quot; every minute instantiate statically class MyTask : public PeriodicTask { public: ...
Definition: background.h:131
virtual void run()=0
define this to do your work.
static Status stopRunningPeriodicTasks(int gracePeriodMillis)
Waits &#39;gracePeriodMillis&#39; for the BackgroundJob responsible for PeriodicTask execution to finish any ...
Definition: background.cpp:267