libamxp  1.4.0
Patterns C Implementation
Scheduler

Data Structures

struct  _scheduler_item
 Structure containing a schedule item. More...
 
struct  _scheduler
 Structure containing a scheduler. More...
 

Typedefs

typedef struct _scheduler_item amxp_scheduler_item_t
 Structure containing a schedule item. More...
 
typedef struct _scheduler amxp_scheduler_t
 Structure containing a scheduler. More...
 

Functions

int amxp_scheduler_new (amxp_scheduler_t **scheduler)
 Allocates a amxp_scheduler_t structures and initializes to an empty scheduler. More...
 
void amxp_scheduler_delete (amxp_scheduler_t **scheduler)
 Frees the previously allocated amxp_scheduler_t structure. More...
 
int amxp_scheduler_init (amxp_scheduler_t *scheduler)
 Initializes a amxp_scheduler_t to an empty scheduler. More...
 
void amxp_scheduler_clean (amxp_scheduler_t *scheduler)
 Cleans the scheduler. More...
 
int amxp_scheduler_enable (amxp_scheduler_t *scheduler, bool enable)
 Enables or disable the scheduler. More...
 
int amxp_scheduler_use_local_time (amxp_scheduler_t *scheduler, bool use_local_time)
 Use local time or UTC time in calculation for next trigger times. More...
 
int amxp_scheduler_update (amxp_scheduler_t *scheduler)
 Forces recalculation of the schedule items' next occurrence time. More...
 
int amxp_scheduler_connect (amxp_scheduler_t *scheduler, const char *id, amxp_slot_fn_t fn, void *priv)
 Connects a callback function to the scheduler. More...
 
int amxp_scheduler_disconnect (amxp_scheduler_t *scheduler, const char *id, amxp_slot_fn_t fn)
 Disconnects a callback function from the scheduler. More...
 
int amxp_scheduler_set_cron_item (amxp_scheduler_t *scheduler, const char *id, const char *cron_expr, uint32_t duration)
 Adds a schedule item or updates a schedule item using a cron expression. More...
 
int amxp_scheduler_set_cron_begin_end_item (amxp_scheduler_t *scheduler, const char *id, const char *cron_begin, const char *cron_end)
 Adds a schedule item or updates a schedule item using a cron expressions. More...
 
int amxp_scheduler_set_weekly_item (amxp_scheduler_t *scheduler, const char *id, const char *time, const char *days_of_week, uint32_t duration)
 Adds a schedule item or updates a schedule item using a time and list of week days. More...
 
int amxp_scheduler_set_weekly_begin_end_item (amxp_scheduler_t *scheduler, const char *id, const char *start_time, const char *end_time, const char *days_of_week)
 Adds a schedule item or updates a schedule item using a start time, end time and list of week days. More...
 
int amxp_scheduler_remove_item (amxp_scheduler_t *scheduler, const char *id)
 Removes a schedule item from the scheduler. More...
 
int amxp_scheduler_enable_item (amxp_scheduler_t *scheduler, const char *id, bool enable)
 Enables or disable a schedule item. More...
 
amxp_signal_mngr_tamxp_scheduler_get_sigmngr (amxp_scheduler_t *scheduler)
 Gets the signal manager of a scheduler. More...
 

Detailed Description

Ambiorix Scheduler.

A scheduler takes schedule items, which can be defined using a cron expression. A schedule item triggers at some point in time, when a schedule is triggered the scheduler will emit the signal "trigger:ID".

Each schedule item has an identifier (which must be unique within the allocated scheduler), this identifier is passed as data in the signal and is also used in the name of the signal.

A schedule item can have a duration (in seconds). When the schedule item triggers and has a duration different from 0, the scheduler will emit the signal "start:ID" and when the duration expires the scheduler will emit the signal "stop:ID".

Typedef Documentation

◆ amxp_scheduler_item_t

Structure containing a schedule item.

This structure defines a schedule item. A schedule item defines points in time and can optionally have duration. The points in time are defined using a cron expression. The duration can either be a fixed time in seconds or another point in time defined using a second cron expression.

A schedule item can be enabled or disabled at any moment.

A schedule item has a unique identifier, which can be any string, and must be unique within a scheduler. The schedule item identifier is used in the signal names.

To activate a schedule item it must be added to a scheduler. Whenever the defined point in time is readched the scheduler will emit or trigger a signal (event).

Multiple callback functions (slots) can be added to the scheduler, either on all schedule items or on a specific item.

◆ amxp_scheduler_t

typedef struct _scheduler amxp_scheduler_t

Structure containing a scheduler.

A scheduler can contain multiple schedule items (amxp_scheduler_item_t). Each time an item is added (using amxp_scheduler_set_cron_item, amxp_scheduler_set_weekly_item, amxp_scheduler_set_cron_begin_end_item, amxp_scheduler_set_weekly_begin_end_item), the list of items is updated and sorted again. The first item in the list is the item the will be triggered first.

A scheduler always works on the current time (UTC or local time). So make sure that time synchronization has happend before using a scheduler.

When NTP synchronization didn't happen yet, it is recommened to disable the scheduler and enable it again when time synchronization has happened.

A scheduler can be enabled or disabled using amxp_scheduler_enable.

A scheduler contains a signal manager. This signal manager will be used to emit the signals "trigger:ID", "start:ID", "stop:ID". (Here the ID is the schedule item identifier)

When these signals are emitted, the signal data will contain the schedule item identifier and the reason (trigger, start, stop). When the "start" signal is emitted, the signal data will also contain the duration in seconds before the "stop" signal is emitted.

Callback functions (slots) can be added using amxp_scheduler_connect or by using one of the amxp_slot_connect functions, and removed again using one of the amxp_slot_disconnect functions.

Function Documentation

◆ amxp_scheduler_clean()

void amxp_scheduler_clean ( amxp_scheduler_t scheduler)

Cleans the scheduler.

All schedule items will be removed, all running timers will be stopped and deleted.

When a schedule item with duration was started, a "stop" signal will be emitted before the schedule item is deleted.

Before using the scheduler again, make sure it is initialized again using amxp_scheduler_init.

Parameters
schedulerpointer to an amxp_cron_expr_t structure

Definition at line 364 of file amxp_scheduler.c.

364  {
365  when_null(scheduler, exit);
366  amxp_sigmngr_clean(&scheduler->sigmngr);
367  amxp_timer_delete(&scheduler->timer);
368  amxc_llist_for_each(it, &scheduler->ordered_items) {
369  amxp_scheduler_item_t* item = amxc_container_of(it, amxp_scheduler_item_t, lit);
370  if((item->timer != NULL) && (item->timer->state == amxp_timer_running)) {
371  amxp_scheduler_emit(scheduler, item, "stop", 0, true);
372  }
373  }
374  amxc_llist_clean(&scheduler->ordered_items, amxp_schedule_item_delete);
375  amxc_htable_clean(&scheduler->items, NULL); // items are freed when cleaning ordered list.
376 
377 exit:
378  return;
379 }
static void amxp_schedule_item_delete(amxc_llist_it_t *it)
static void amxp_scheduler_emit(amxp_scheduler_t *scheduler, amxp_scheduler_item_t *item, const char *signal, uint32_t duration, bool trigger)
int amxp_sigmngr_clean(amxp_signal_mngr_t *sig_mngr)
Clean-up functions, cleans-up all members of a amxp_signal_mngr_t structure.
Definition: amxp_signal.c:405
void amxp_timer_delete(amxp_timer_t **timer)
Deletes a timer.
Definition: amxp_timer.c:247
@ amxp_timer_running
Definition: amxp_timer.h:151
amxp_timer_state_t state
Definition: amxp_timer.h:166
Structure containing a schedule item.
amxp_timer_t * timer
amxc_htable_t items
amxp_signal_mngr_t sigmngr
amxp_timer_t * timer
amxc_llist_t ordered_items

◆ amxp_scheduler_connect()

int amxp_scheduler_connect ( amxp_scheduler_t scheduler,
const char *  id,
amxp_slot_fn_t  fn,
void *  priv 
)

Connects a callback function to the scheduler.

A callback functions can be added to a scheduler for a specific schedule item identifier or for all currently known schedule items.

Alternativly the amxp_slot_connection functions can be used to connect to the scheduler signals.

Multiple callback functions can be added to the scheduler.

The possible scheduler signals are:

  • trigger:ID - only for schedule items without a duration
  • start:ID - only for schedule items with a duration, will be followed by a stop signal
  • stop:ID - only for schedule items with a duration, is always preceded with a start signal

The ID in above signal names is replaced by the identifier of the scheduler item.

A pointer to private data can be passed as well. This pointer is passed to the callback function. The scheduler itself will not use this private data and will not take ownership of the pointer. If memory was allocated it is up to the caller to manage the allocated memory.

Parameters
schedulerpointer to an amxp_scheduler_t structure
id(optional) the identifier for which the callback funcion (slot) must be called
fnthe pointer to the callback function
privprivate data, will be passed to the callback function
Returns
When successfull, this functions returns 0.

Definition at line 448 of file amxp_scheduler.c.

451  {
452 
453  int rv = -1;
454  amxc_string_t signal;
455  amxc_htable_it_t* hit = NULL;
456  amxp_scheduler_item_t* item = NULL;
457 
458  amxc_string_init(&signal, 0);
459  when_null(scheduler, exit);
460 
461  if((id == NULL) || (*id == 0)) {
462  // connect to all signals
463  rv = amxp_slot_connect(&scheduler->sigmngr, "*", NULL, fn, priv);
464  goto exit;
465  }
466 
467  hit = amxc_htable_get(&scheduler->items, id);
468  when_null(hit, exit);
469  item = amxc_container_of(hit, amxp_scheduler_item_t, hit);
470  when_null(item, exit);
471 
472  amxc_string_setf(&signal, "start:%s", id);
473  rv = amxp_slot_connect(&scheduler->sigmngr, amxc_string_get(&signal, 0), NULL, fn, priv);
474  when_failed(rv, exit);
475  amxc_string_setf(&signal, "stop:%s", id);
476  rv = amxp_slot_connect(&scheduler->sigmngr, amxc_string_get(&signal, 0), NULL, fn, priv);
477  when_failed(rv, exit);
478  amxc_string_setf(&signal, "trigger:%s", id);
479  rv = amxp_slot_connect(&scheduler->sigmngr, amxc_string_get(&signal, 0), NULL, fn, priv);
480 
481 exit:
482  amxc_string_clean(&signal);
483  return rv;
484 }
int amxp_slot_connect(amxp_signal_mngr_t *const sig_mngr, const char *const sig_name, const char *const expression, amxp_slot_fn_t fn, void *const priv)
Connects a slot (function) to a named signal of a signal manager.
Definition: amxp_slot.c:300

◆ amxp_scheduler_delete()

void amxp_scheduler_delete ( amxp_scheduler_t **  scheduler)

Frees the previously allocated amxp_scheduler_t structure.

Frees the allocated memory and sets the pointer to NULL.

All schedule items will be deleted as well, all running timers will be stopped and deleted.

When a schedule item with duration was started, a "stop" signal will be emitted before the schedule item is deleted.

Note
Only call this function for amxp_scheduler_t structures that are allocated on the heap using amxp_scheduler_new.
Parameters
schedulerpointer to a pointer that points to the allocated amxp_scheduler_t structure

Definition at line 332 of file amxp_scheduler.c.

332  {
333  when_null(scheduler, exit);
334  when_null(*scheduler, exit);
335 
336  amxp_scheduler_clean(*scheduler);
337  free(*scheduler);
338  *scheduler = NULL;
339 
340 exit:
341  return;
342 }
void amxp_scheduler_clean(amxp_scheduler_t *scheduler)
Cleans the scheduler.

◆ amxp_scheduler_disconnect()

int amxp_scheduler_disconnect ( amxp_scheduler_t scheduler,
const char *  id,
amxp_slot_fn_t  fn 
)

Disconnects a callback function from the scheduler.

A previously added callback functions can be removed from a scheduler for a specific schedule item identifier or for all currently known schedule items.

Alternativly the amxp_slot_disconnect functions can be used to disconnect from the scheduler signals.

If a NULL pointer is used as the id the function will be removed from all signals from the scheduler.

Parameters
schedulerpointer to an amxp_scheduler_t structure
id(optional) the identifier for which the callback funcion (slot) must be removed
fnthe pointer to the callback function
Returns
When successfull, this functions returns 0.

Definition at line 486 of file amxp_scheduler.c.

488  {
489 
490  int rv = -1;
491  amxc_string_t signal;
492  amxc_htable_it_t* hit = NULL;
493  amxp_scheduler_item_t* item = NULL;
494 
495  amxc_string_init(&signal, 0);
496  when_null(scheduler, exit);
497 
498  if((id == NULL) || (*id == 0)) {
499  // connect to all signals
501  rv = 0;
502  goto exit;
503  }
504 
505  hit = amxc_htable_get(&scheduler->items, id);
506  when_null(hit, exit);
507  item = amxc_container_of(hit, amxp_scheduler_item_t, hit);
508  when_null(item, exit);
509 
510  amxc_string_setf(&signal, "start:%s", id);
511  rv = amxp_slot_disconnect(&scheduler->sigmngr, amxc_string_get(&signal, 0), fn);
512  when_failed(rv, exit);
513  amxc_string_setf(&signal, "stop:%s", id);
514  rv = amxp_slot_disconnect(&scheduler->sigmngr, amxc_string_get(&signal, 0), fn);
515  when_failed(rv, exit);
516  amxc_string_setf(&signal, "trigger:%s", id);
517  rv = amxp_slot_disconnect(&scheduler->sigmngr, amxc_string_get(&signal, 0), fn);
518 
519 exit:
520  amxc_string_clean(&signal);
521  return rv;
522 }
void amxp_slot_disconnect_all(amxp_slot_fn_t fn)
Disconnects a slot from all signals it was connected to.
Definition: amxp_slot.c:459
int amxp_slot_disconnect(amxp_signal_mngr_t *const sig_mngr, const char *const sig_name, amxp_slot_fn_t fn)
Disconnects a slot from (a) signal(s).
Definition: amxp_slot.c:380

◆ amxp_scheduler_enable()

int amxp_scheduler_enable ( amxp_scheduler_t scheduler,
bool  enable 
)

Enables or disable the scheduler.

When enabling a previously disabled scheduler, a recalculation is done of all schedule items and a timer is started that will expires when the first schedule triggers.

Enabling an empty scheduler (that is a scheduler without any schedule items), will have no effect.

When disabling a scheduler no signals will be emitted anymore for that scheduler, except the "stop" signals. This signal will be emitted for schedules that have a duration and were already started.

Parameters
schedulerpointer to an amxp_scheduler_t structure
enablewhen set to true, the scheduler will be enable.
Returns
When successfull, this functions returns 0.

Definition at line 381 of file amxp_scheduler.c.

381  {
382  int rv = -1;
383 
384  when_null(scheduler, exit);
385  when_true_status(scheduler->sigmngr.enabled == enable, exit, rv = 0);
386 
387  amxp_sigmngr_enable(&scheduler->sigmngr, enable);
388  if(enable) {
389  amxp_scheduler_update(scheduler);
390  amxp_scheduler_reset_timer(scheduler);
391  }
392  if(!enable) {
393  amxp_scheduler_update(scheduler);
394  amxp_timer_stop(scheduler->timer);
395  }
396 
397  rv = 0;
398 
399 exit:
400  return rv;
401 }
static void amxp_scheduler_reset_timer(amxp_scheduler_t *scheduler)
int amxp_scheduler_update(amxp_scheduler_t *scheduler)
Forces recalculation of the schedule items' next occurrence time.
int amxp_sigmngr_enable(amxp_signal_mngr_t *const sig_mngr, bool enable)
Enables or disables the signal manager.
Definition: amxp_signal.c:561
int amxp_timer_stop(amxp_timer_t *timer)
Stops the timer.
Definition: amxp_timer.c:319

◆ amxp_scheduler_enable_item()

int amxp_scheduler_enable_item ( amxp_scheduler_t scheduler,
const char *  id,
bool  enable 
)

Enables or disable a schedule item.

When a schedule item is added to the scheduler, it will be enabled by default.

By disabling the schedule item, the scheduler will still take the item into account for time calculation, but will not emit signals for it.

Parameters
schedulerpointer to an amxp_scheduler_t structure
idthe identifier for schedule item
enablefalse to disable to schedule item, true to enable it.
Returns
When successfull, this functions returns 0.

Definition at line 680 of file amxp_scheduler.c.

682  {
683  int rv = -1;
684  amxc_htable_it_t* hit = NULL;
685  amxp_scheduler_item_t* item = NULL;
686 
687  when_null(scheduler, exit);
688  when_str_empty(id, exit);
689 
690  hit = amxc_htable_get(&scheduler->items, id);
691  when_null(hit, exit);
692  item = amxc_container_of(hit, amxp_scheduler_item_t, hit);
693  when_true_status(enable == item->enabled, exit, rv = 0);
694 
695  item->enabled = enable;
696  if(!enable) {
697  if((item->timer != NULL) && (item->timer->state == amxp_timer_running)) {
698  amxp_scheduler_emit(scheduler, item, "stop", 0, false);
699  amxp_timer_delete(&item->timer);
700  }
701  } else {
702  amxp_scheduler_check_item(scheduler, item);
703  amxp_scheduler_reset_timer(scheduler);
704  }
705 
706  rv = 0;
707 
708 exit:
709  return rv;
710 }
static void amxp_scheduler_check_item(amxp_scheduler_t *scheduler, amxp_scheduler_item_t *item)

◆ amxp_scheduler_get_sigmngr()

amxp_signal_mngr_t* amxp_scheduler_get_sigmngr ( amxp_scheduler_t scheduler)

Gets the signal manager of a scheduler.

Returns the signal manager of the scheduler. This can be used to connect or disconnect slots (callback functions)

Parameters
schedulerpointer to an amxp_scheduler_t structure
Returns
Pointer to the signal manager of the scheduler or NULL.

Definition at line 712 of file amxp_scheduler.c.

712  {
713  amxp_signal_mngr_t* sigmngr = NULL;
714  when_null(scheduler, exit);
715 
716  sigmngr = &scheduler->sigmngr;
717 
718 exit:
719  return sigmngr;
720 }
Structure containing the signal manager information.
Definition: amxp_signal.h:103
static amxp_signal_mngr_t * sigmngr

◆ amxp_scheduler_init()

int amxp_scheduler_init ( amxp_scheduler_t scheduler)

Initializes a amxp_scheduler_t to an empty scheduler.

This function initializes it to an empty scheduler.

An empty scheduler will be inactive (no timer is started) until the first schedule item is added.

By default the scheduler will be enabled. When adding schedule items they will be taken into account immediatly. If more control is needed, first disable the scheduler by calling amxp_scheduler_enable.

By default the scheduler will use UTC time, if all calculations needs to be done on local time call the function amxp_scheduler_use_local_time.

Use amxp_scheduler_set_cron_item, amxp_scheduler_set_cron_begin_end_item, amxp_scheduler_set_weekly_item or amxp_scheduler_set_weekly_begin_end_item to add schedule items.

Note
When the scheduler is not used any more, the function amxp_scheduler_clean must be called. It will do the scheduler clean-up (removes all schedule items and timers).
Parameters
schedulerpointer to a amxp_schedule_t structure
Returns
When initialization is successfull, this functions returns 0.

Definition at line 344 of file amxp_scheduler.c.

344  {
345  int rv = -1;
346 
347  when_null(scheduler, exit);
348 
349  amxp_sigmngr_init(&scheduler->sigmngr);
350  amxp_sigmngr_enable(&scheduler->sigmngr, true);
351 
352  scheduler->use_local_time = false;
353  scheduler->timer = NULL;
354  amxp_timer_new(&scheduler->timer, amxp_scheduler_trigger, scheduler);
355  amxc_htable_init(&scheduler->items, 5);
356  amxc_llist_init(&scheduler->ordered_items);
357 
358  rv = 0;
359 
360 exit:
361  return rv;
362 }
static void amxp_scheduler_trigger(amxp_timer_t *timer, void *priv)
int amxp_sigmngr_init(amxp_signal_mngr_t *sig_mngr)
Initializing function, initializes members of the amxp_signal_mngr_t structure.
Definition: amxp_signal.c:377
int amxp_timer_new(amxp_timer_t **timer, amxp_timer_cb_t cb, void *priv)
Allocate and initializes a new timer.
Definition: amxp_timer.c:229
bool use_local_time

◆ amxp_scheduler_new()

int amxp_scheduler_new ( amxp_scheduler_t **  scheduler)

Allocates a amxp_scheduler_t structures and initializes to an empty scheduler.

This function allocates a amxp_scheduler_t structure and initializes it to an empty scheduler.

An empty scheduler will be inactive (no timer is started) until the first schedule item is added.

By default the scheduler will be enabled. When adding schedule items they will be taken into account immediatly. If more control is needed, first disable the scheduler by calling amxp_scheduler_enable.

By default the scheduler will use UTC time, if all calculations needs to be done on local time call the function amxp_scheduler_use_local_time.

Use amxp_scheduler_set_cron_item, amxp_scheduler_set_cron_begin_end_item, amxp_scheduler_set_weekly_item or amxp_scheduler_set_weekly_begin_end_item to add schedule items.

Note
The allocated memory must be freed when not used anymore, use amxp_scheduler_delete to free the memory
Parameters
schedulerpointer to a pointer that will point to the new allocated amxp_schedule_t structure
Returns
When allocation is successfull, this functions returns 0.

Definition at line 319 of file amxp_scheduler.c.

319  {
320  int rv = -1;
321 
322  when_null(scheduler, exit);
323  *scheduler = (amxp_scheduler_t*) calloc(1, sizeof(amxp_scheduler_t));
324  when_null(*scheduler, exit);
325 
326  rv = amxp_scheduler_init(*scheduler);
327 
328 exit:
329  return rv;
330 }
int amxp_scheduler_init(amxp_scheduler_t *scheduler)
Initializes a amxp_scheduler_t to an empty scheduler.
Structure containing a scheduler.

◆ amxp_scheduler_remove_item()

int amxp_scheduler_remove_item ( amxp_scheduler_t scheduler,
const char *  id 
)

Removes a schedule item from the scheduler.

Removes a schedule item with the provided identifier from the scheduler.

If the schedule item has a duration time which was started, the "stop:ID" signal be emitted when the item is removed.

When an identifier (ID) is specified, but no scheduler item with that identifier is available in this scheduler, the function succeeds but nothing happended.

Parameters
schedulerpointer to an amxp_scheduler_t structure
idthe identifier for schedule item
Returns
When successfull, this functions returns 0.

Definition at line 647 of file amxp_scheduler.c.

648  {
649 
650  int rv = -1;
651  amxc_htable_it_t* hit = NULL;
652  amxp_scheduler_item_t* item = NULL;
653 
654  when_null(scheduler, exit);
655  when_str_empty(id, exit);
656 
657  hit = amxc_htable_get(&scheduler->items, id);
658  when_null_status(hit, exit, rv = 0);
659  item = amxc_container_of(hit, amxp_scheduler_item_t, hit);
660 
661  amxc_htable_it_take(hit);
662  if((item->timer != NULL) && (item->timer->state == amxp_timer_running)) {
663  amxp_timer_stop(item->timer);
664  amxp_scheduler_emit(scheduler, item, "stop", 0, true);
665  }
666 
667  amxp_scheduler_remove_signal(scheduler, "start", id);
668  amxp_scheduler_remove_signal(scheduler, "stop", id);
669  amxp_scheduler_remove_signal(scheduler, "trigger", id);
670 
671  amxc_llist_it_clean(&item->lit, amxp_schedule_item_delete);
672  amxp_scheduler_reset_timer(scheduler);
673 
674  rv = 0;
675 
676 exit:
677  return rv;
678 }
static void amxp_scheduler_remove_signal(amxp_scheduler_t *scheduler, const char *signal, const char *id)
amxc_llist_it_t lit

◆ amxp_scheduler_set_cron_begin_end_item()

int amxp_scheduler_set_cron_begin_end_item ( amxp_scheduler_t scheduler,
const char *  id,
const char *  cron_begin,
const char *  cron_end 
)

Adds a schedule item or updates a schedule item using a cron expressions.

Using a cron expression (see amxp_cron_expr_t) a schedule item is added to the scheduler. If a schedule item exists with the given identifier, it will be updated.

The scheduler will calculate the next time the schedule should be triggered using the cron expression provided with argument cron_begin.

A duration can be specified using a second cron expression (using argument cron_end). When the schedule item is triggered, the duration is calculated using the second cron expression.

For schedule items added with this function, the scheduler will emit the signal "start:ID" when the schedule is triggered and "stop:ID" when the next end time (duration) is reached.

Each signal will contain the schedule item identifier in the signal data and the reason (start, stop).

The signal "start:ID" will also contain the duration (in seconds) before the "stop:ID" will be send.

Schedule items with a duration for which the "start" signal was emitted and which are deleted before the duration is over, the scheduler will send a "stop" signal at the moment they are deleted.

Parameters
schedulerpointer to an amxp_scheduler_t structure
idthe identifier for schedule item
cron_begina string containing a valid cron expression (see amxp_cron_expr_t)
cron_enda string containing a valid cron expression (see amxp_cron_expr_t)
Returns
When successfull, this functions returns 0.

Definition at line 550 of file amxp_scheduler.c.

553  {
554  int rv = -1;
555  amxp_scheduler_item_t* item = NULL;
556 
557  when_str_empty(cron_begin, exit);
558  when_str_empty(cron_end, exit);
559 
560  item = amxp_scheduler_create_or_fetch(scheduler, id, 0);
561  when_null(item, exit);
562 
563  item->end_time_is_set = true;
564 
565  rv = amxp_cron_parse_expr(&item->cron, cron_begin, NULL);
566  when_failed(rv, exit);
567  rv = amxp_cron_parse_expr(&item->end_cron, cron_end, NULL);
568  when_failed(rv, exit);
569 
570  amxp_scheduler_insert(scheduler, id, item);
571 
572 exit:
573  if((rv != 0) && (item != NULL)) {
574  amxc_llist_it_clean(&item->lit, amxp_schedule_item_delete);
575  }
576  return rv;
577 }
static void amxp_scheduler_insert(amxp_scheduler_t *scheduler, const char *id, amxp_scheduler_item_t *item)
static amxp_scheduler_item_t * amxp_scheduler_create_or_fetch(amxp_scheduler_t *scheduler, const char *id, uint32_t duration)
int amxp_cron_parse_expr(amxp_cron_expr_t *target, const char *expression, const char **error)
Allocates and initializes an amxp_cron_expr_t structures and parses the given cron expression.
Definition: amxp_cron.c:486
amxp_cron_expr_t end_cron
amxp_cron_expr_t cron

◆ amxp_scheduler_set_cron_item()

int amxp_scheduler_set_cron_item ( amxp_scheduler_t scheduler,
const char *  id,
const char *  cron_expr,
uint32_t  duration 
)

Adds a schedule item or updates a schedule item using a cron expression.

Using a cron expression (see amxp_cron_expr_t) a schedule item is added to the scheduler. If a schedule item exists with the given identifier, it will be updated.

The scheduler will calculate the next time the schedule item should be triggered.

A duration can be specified. When the duration is not zero (0), the scheduler will emit the signal "start:ID" when the schedule is triggered, and starts a timer for the defined duration of the schedule item. When the timer expires the scheduler will emit the signal "stop:ID".

For schedule items that have a duration of zero (0), the scheduler will emit the signal "trigger:ID" when the schedule item is triggered.

Each signal will contain the schedule item identifier in the signal data and the reason (trigger, start, stop).

The signal "start:ID" will also contain the duration (in seconds) before the "stop:ID" will be send.

Schedule items with a duration for which the "start" signal was emitted and which are deleted before the duration is over, the scheduler will send a "stop" signal at the moment they are deleted.

Parameters
schedulerpointer to an amxp_scheduler_t structure
idthe identifier for schedule item
cron_expra string containing a valid cron expression (see amxp_cron_expr_t)
durationduration in seconds
Returns
When successfull, this functions returns 0.

Definition at line 524 of file amxp_scheduler.c.

527  {
528  int rv = -1;
529  amxp_scheduler_item_t* item = NULL;
530 
531  when_str_empty(cron_expr, exit);
532 
533  item = amxp_scheduler_create_or_fetch(scheduler, id, duration);
534  when_null(item, exit);
535 
536  item->end_time_is_set = false;
537 
538  rv = amxp_cron_parse_expr(&item->cron, cron_expr, NULL);
539  when_failed(rv, exit);
540 
541  amxp_scheduler_insert(scheduler, id, item);
542 
543 exit:
544  if((rv != 0) && (item != NULL)) {
545  amxc_llist_it_clean(&item->lit, amxp_schedule_item_delete);
546  }
547  return rv;
548 }

◆ amxp_scheduler_set_weekly_begin_end_item()

int amxp_scheduler_set_weekly_begin_end_item ( amxp_scheduler_t scheduler,
const char *  id,
const char *  start_time,
const char *  end_time,
const char *  days_of_week 
)

Adds a schedule item or updates a schedule item using a start time, end time and list of week days.

Using a time in "HH:MM:SS" (seconds are optional) and a list of week days a schedule item is added to the scheduler. If a schedule item exists with the given identifier, it will be updated.

Weekdays must be specified with their name and are case insensitive

  • sunday
  • monday
  • tuesday
  • wednesday
  • thursday
  • friday
  • saturday

Optionaly a range of weekdays can be specified. (sunday is considered the first day of the week).

Examples for days_of_Week argument:

"monday,tuesday,wednesday,thursday,friday"
"monday-friday"

The scheduler will calculate the next time the schedule should be triggered.

The duration is calculated using the end time. If the end time is smaller then the start time, the stop signal will be emitted the next valid day.

Example:

amxp_scheduler_set_weekly_begin_end_item(scheduler, "MyItem", "15:00", "12:00", "saturday,sunday");
int amxp_scheduler_set_weekly_begin_end_item(amxp_scheduler_t *scheduler, const char *id, const char *start_time, const char *end_time, const char *days_of_week)
Adds a schedule item or updates a schedule item using a start time, end time and list of week days.

When above schedule item is triggered (started) at a sunday at 15:00, the stop signal will be emitted on the next saterday at 12:00. So the item stays active for almost a full week.

Each signal will contain the schedule item identifier in the signal data and the reason (start, stop).

The signal "start:ID" will also contain the duration (in seconds) before the "stop:ID" signal will be send.

Schedule items with a duration for which the "start" signal was emitted and which are deleted before the duration is over, the scheduler will send a "stop" signal at the moment they are deleted.

Parameters
schedulerpointer to an amxp_scheduler_t structure
idthe identifier for schedule item
start_timea string containing a valid time in "HH:MM:SS" format, seconds are optional.
end_timea string containing a valid time in "HH:MM:SS" format, seconds are optional.
days_of_weeka string containing comma separated list of week days or a range.
Returns
When successfull, this functions returns 0.

Definition at line 610 of file amxp_scheduler.c.

614  {
615  int rv = -1;
616  amxp_scheduler_item_t* item = NULL;
617 
618  when_str_empty(days_of_week, exit);
619 
620  if((start_time == NULL) || (*start_time == 0)) {
621  start_time = "00:00";
622  }
623 
624  if((end_time == NULL) || (*end_time == 0)) {
625  end_time = "23:59";
626  }
627 
628  item = amxp_scheduler_create_or_fetch(scheduler, id, 0);
629  when_null(item, exit);
630 
631  item->end_time_is_set = true;
632 
633  rv = amxp_cron_build_weekly(&item->cron, start_time, days_of_week);
634  when_failed(rv, exit);
635  rv = amxp_cron_build_weekly(&item->end_cron, end_time, days_of_week);
636  when_failed(rv, exit);
637 
638  amxp_scheduler_insert(scheduler, id, item);
639 
640 exit:
641  if((rv != 0) && (item != NULL)) {
642  amxc_llist_it_clean(&item->lit, amxp_schedule_item_delete);
643  }
644  return rv;
645 }
int amxp_cron_build_weekly(amxp_cron_expr_t *target, const char *time, const char *days_of_week)
Builds a weekly cron expression that is triggered at a certain time on certain days of the week.
Definition: amxp_cron.c:531

◆ amxp_scheduler_set_weekly_item()

int amxp_scheduler_set_weekly_item ( amxp_scheduler_t scheduler,
const char *  id,
const char *  time,
const char *  days_of_week,
uint32_t  duration 
)

Adds a schedule item or updates a schedule item using a time and list of week days.

Using a time in "HH:MM:SS" (seconds are optional) and a list of week days a schedule item is added to the scheduler. If a schedule item exists with the given identifier, it will be updated.

Weekdays must be specified with their name and are case insensitive

  • sunday
  • monday
  • tuesday
  • wednesday
  • thursday
  • friday
  • saturday

Optionaly a range of weekdays can be specified. (sunday is considered the first day of the week).

Examples for days_of_Week argument:

"monday,tuesday,wednesday,thursday,friday"
"monday-friday"

The scheduler will calculate the next time the schedule should be triggered.

A duration can be specified. When the duration is not zero (0), the scheduler will emit the signal "start:ID" when the schedule is triggered, and starts a timer for the duration. When the timer expires the scheduler will emit the signal "stop:ID".

For schedule items that have a duration of zero (0), the scheduler will emit the signal "trigger:ID" when the schedule is triggered.

Each signal will contain the schedule item identifier in the signal data and the reason (start, stop).

The signal "start:ID" will also contain the duration (in seconds) before the "stop:ID" will be send.

Schedule items with a duration for which the "start" signal was emitted and which are deleted before the duration is over, the scheduler will send a "stop" signal at the moment they are deleted.

Parameters
schedulerpointer to an amxp_scheduler_t structure
idthe identifier for schedule item
timea string containing a valid time in "HH:MM:SS" format, seconds are optional.
days_of_weeka string containing comma separated list of week days or a range.
durationduration in seconds
Returns
When successfull, this functions returns 0.

Definition at line 579 of file amxp_scheduler.c.

583  {
584  int rv = -1;
585  amxp_scheduler_item_t* item = NULL;
586 
587  when_str_empty(days_of_week, exit);
588 
589  if((time == NULL) || (*time == 0)) {
590  time = "00:00";
591  }
592 
593  item = amxp_scheduler_create_or_fetch(scheduler, id, duration);
594  when_null(item, exit);
595 
596  item->end_time_is_set = false;
597 
598  rv = amxp_cron_build_weekly(&item->cron, time, days_of_week);
599  when_failed(rv, exit);
600 
601  amxp_scheduler_insert(scheduler, id, item);
602 
603 exit:
604  if((rv != 0) && (item != NULL)) {
605  amxc_llist_it_clean(&item->lit, amxp_schedule_item_delete);
606  }
607  return rv;
608 }

◆ amxp_scheduler_update()

int amxp_scheduler_update ( amxp_scheduler_t scheduler)

Forces recalculation of the schedule items' next occurrence time.

Under normal circumstances there will be no need to call this function. The scheduler itself will do recalculation when needed by itself.

However this function can be called when system time changes, like NTP synchronizaton has happened.

Parameters
schedulerpointer to an amxp_scheduler_t structure
Returns
When successfull, this functions returns 0.

Definition at line 418 of file amxp_scheduler.c.

418  {
419  int rv = -1;
420  amxc_ts_t now;
421  amxp_scheduler_item_t* item = NULL;
422 
423  amxc_ts_now(&now);
424  when_null(scheduler, exit);
425 
426  if(scheduler->use_local_time) {
427  amxc_ts_to_local(&now);
428  }
429 
430  amxc_llist_for_each(item_it, &scheduler->ordered_items) {
431  item = amxc_container_of(item_it, amxp_scheduler_item_t, lit);
432  amxp_cron_next(&item->cron, &now, &item->next);
433  if(scheduler->sigmngr.enabled) {
434  amxp_scheduler_check_item(scheduler, item);
435  } else {
436  amxp_timer_delete(&item->timer);
437  }
438  }
439 
440  amxp_scheduler_reset_timer(scheduler);
441 
442  rv = 0;
443 
444 exit:
445  return rv;
446 }
int amxp_cron_next(const amxp_cron_expr_t *expr, const amxc_ts_t *ref, amxc_ts_t *next)
Calculates the next trigger time for a parsed cron expression.
Definition: amxp_cron.c:631

◆ amxp_scheduler_use_local_time()

int amxp_scheduler_use_local_time ( amxp_scheduler_t scheduler,
bool  use_local_time 
)

Use local time or UTC time in calculation for next trigger times.

A scheduler calculates for each schedule item when the next occurence will be triggered, By default UTC time is used. Using this function the scheduler can be switched to use local time.

Parameters
schedulerpointer to an amxp_scheduler_t structure
use_local_timewhen set to true, the scheduler will use local time, when set to fals UTC time is used,
Returns
When successfull, this functions returns 0.

Definition at line 403 of file amxp_scheduler.c.

403  {
404  int rv = -1;
405 
406  when_null(scheduler, exit);
407 
408  when_true_status(use_local_time == scheduler->use_local_time, exit, rv = 0);
409  scheduler->use_local_time = use_local_time;
410  amxp_scheduler_update(scheduler);
411 
412  rv = 0;
413 
414 exit:
415  return rv;
416 }