dp_ptask.c revision e89c2cb5ec77d57ed93952dae08df51738834faf
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes Pavel Březina <pbrezina@redhat.com>
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes Copyright (C) 2013 Red Hat
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes This program is free software; you can redistribute it and/or modify
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes it under the terms of the GNU General Public License as published by
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes the Free Software Foundation; either version 3 of the License, or
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes (at your option) any later version.
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes This program is distributed in the hope that it will be useful,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes but WITHOUT ANY WARRANTY; without even the implied warranty of
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes GNU General Public License for more details.
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes You should have received a copy of the GNU General Public License
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes along with this program. If not, see <http://www.gnu.org/licenses/>.
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes#define backoff_allowed(ptask) (ptask->max_backoff != 0)
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesstatic void be_ptask_schedule(struct be_ptask *task,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes DEBUG(SSSDBG_FATAL_FAILURE, "BUG: task is NULL\n");
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg DEBUG(SSSDBG_TRACE_FUNC, "Terminating periodic task [%s]\n", task->name);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes DEBUG(SSSDBG_FATAL_FAILURE, "BUG: task is NULL\n");
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes DEBUG(SSSDBG_TRACE_FUNC, "Back end is offline\n");
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesstatic void be_ptask_timeout(struct tevent_context *ev,
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg DEBUG(SSSDBG_OP_FAILURE, "Task [%s]: timed out\n", task->name);
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg be_ptask_schedule(task, BE_PTASK_PERIOD, BE_PTASK_SCHEDULE_FROM_NOW);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesstatic void be_ptask_execute(struct tevent_context *ev,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes task->timer = NULL; /* timer is freed by tevent */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes DEBUG(SSSDBG_TRACE_FUNC, "Back end is offline\n");
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes /* This case is normally handled by offline callback but we
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes * should handle it here as well since we can get here in some
0662ed52e814f8f08ef0e09956413a792584eddffuankg * special cases for example unit tests or tevent events order. */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes /* continue */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes DEBUG(SSSDBG_TRACE_FUNC, "Task [%s]: executing task, timeout %lu "
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes task->req = task->send_fn(task, task->ev, task->be_ctx, task, task->pvt);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes /* skip this iteration and try again later */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes DEBUG(SSSDBG_OP_FAILURE, "Task [%s]: failed to execute task, "
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes be_ptask_schedule(task, BE_PTASK_PERIOD, BE_PTASK_SCHEDULE_FROM_NOW);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes tevent_req_set_callback(task->req, be_ptask_done, task);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes /* schedule timeout */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes tv = tevent_timeval_current_ofs(task->timeout, 0);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes timeout = tevent_add_timer(task->ev, task->req, tv,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes /* If we can't guarantee a timeout,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes * we need to cancel the request. */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes DEBUG(SSSDBG_OP_FAILURE, "Task [%s]: failed to set timeout, "
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes task = tevent_req_callback_data(req, struct be_ptask);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes DEBUG(SSSDBG_TRACE_FUNC, "Task [%s]: finished successfully\n",
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes be_ptask_schedule(task, BE_PTASK_PERIOD, BE_PTASK_SCHEDULE_FROM_LAST);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes DEBUG(SSSDBG_OP_FAILURE, "Task [%s]: failed with [%d]: %s\n",
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes be_ptask_schedule(task, BE_PTASK_PERIOD, BE_PTASK_SCHEDULE_FROM_NOW);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesstatic void be_ptask_schedule(struct be_ptask *task,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes DEBUG(SSSDBG_TRACE_FUNC, "Task [%s]: disabled\n", task->name);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes if (backoff_allowed(task) && task->period * 2 <= task->max_backoff) {
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes /* double the period for the next execution */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes /* add random offset */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes delay = delay + (rand_r(&task->ro_seed) % task->random_offset);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes DEBUG(SSSDBG_TRACE_FUNC, "Task [%s]: scheduling task %lu seconds "
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes "from now [%lu]\n", task->name, delay, tv.tv_sec);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes tv = tevent_timeval_set(task->last_execution + delay, 0);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes DEBUG(SSSDBG_TRACE_FUNC, "Task [%s]: scheduling task %lu seconds "
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes "from last execution time [%lu]\n",
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes DEBUG(SSSDBG_MINOR_FAILURE, "Task [%s]: another timer is already "
void *pvt,
const char *name,
return EINVAL;
goto done;
goto done;
goto done;
goto done;
done:
return ret;
struct be_ptask_sync_ctx {
void *pvt;
struct be_ptask_sync_state {
int dummy;
static struct tevent_req *
void *pvt)
return NULL;
return req;
return EOK;
void *pvt,
const char *name,
goto done;
goto done;
done:
return ret;