summaryrefslogtreecommitdiffstats
path: root/src/providers/dp_ptask.h
blob: 1b931010bb206285f3ca635f7b0c2399c3050951 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
    Authors:
        Pavel Březina <pbrezina@redhat.com>

    Copyright (C) 2013 Red Hat

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _DP_PTASK_H_
#define _DP_PTASK_H_

#include <tevent.h>
#include <talloc.h>
#include <time.h>

/* solve circular dependency */
struct be_ctx;

struct be_ptask;

/**
 * Defines how should task behave when back end is offline.
 */
enum be_ptask_offline {
    /* current request will be skipped and rescheduled to 'now + period' */
    BE_PTASK_OFFLINE_SKIP,

    /* An offline and online callback is registered. The task is disabled
     * immediately when back end goes offline and then enabled again
     * when back end goes back online */
    BE_PTASK_OFFLINE_DISABLE,

    /* current request will be executed as planned */
    BE_PTASK_OFFLINE_EXECUTE
};

typedef struct tevent_req *
(*be_ptask_send_t)(TALLOC_CTX *mem_ctx,
                   struct tevent_context *ev,
                   struct be_ctx *be_ctx,
                   struct be_ptask *be_ptask,
                   void *pvt);

/**
 * If EOK, task will be scheduled again to 'last_execution_time + period'.
 * If other error code, task will be rescheduled to 'now + period'.
 */
typedef errno_t
(*be_ptask_recv_t)(struct tevent_req *req);

/**
 * If EOK, task will be scheduled again to 'last_execution_time + period'.
 * If other error code, task will be rescheduled to 'now + period'.
 */
typedef errno_t
(*be_ptask_sync_t)(TALLOC_CTX *mem_ctx,
                   struct tevent_context *ev,
                   struct be_ctx *be_ctx,
                   struct be_ptask *be_ptask,
                   void *pvt);

/**
 * The first execution is scheduled first_delay seconds after the task is
 * created.
 *
 * If request does not complete in timeout seconds, it will be
 * cancelled and rescheduled to 'now + period'.
 *
 * If the task is reenabled, it will be scheduled again to
 * 'now + enabled_delay'.
 *
 * The random_offset is maximum number of seconds added to the
 * expected delay. Set to 0 if no randomization is needed.

 * If max_backoff is not 0 then the period is doubled
 * every time the task is scheduled. The maximum value of
 * period is max_backoff. The value of period will be reset to
 * original value when the task is disabled. With max_backoff
 * set to zero, this feature is disabled.
 *
 * If an internal error occurred, the task is automatically disabled.
 */
errno_t be_ptask_create(TALLOC_CTX *mem_ctx,
                        struct be_ctx *be_ctx,
                        time_t period,
                        time_t first_delay,
                        time_t enabled_delay,
                        time_t random_offset,
                        time_t timeout,
                        enum be_ptask_offline offline,
                        time_t max_backoff,
                        be_ptask_send_t send_fn,
                        be_ptask_recv_t recv_fn,
                        void *pvt,
                        const char *name,
                        struct be_ptask **_task);

errno_t be_ptask_create_sync(TALLOC_CTX *mem_ctx,
                             struct be_ctx *be_ctx,
                             time_t period,
                             time_t first_delay,
                             time_t enabled_delay,
                             time_t random_offset,
                             time_t timeout,
                             enum be_ptask_offline offline,
                             time_t max_backoff,
                             be_ptask_sync_t fn,
                             void *pvt,
                             const char *name,
                             struct be_ptask **_task);

void be_ptask_enable(struct be_ptask *task);
void be_ptask_disable(struct be_ptask *task);
void be_ptask_destroy(struct be_ptask **task);

time_t be_ptask_get_period(struct be_ptask *task);

#endif /* _DP_PTASK_H_ */