Git Product home page Git Product logo

c_forever's Introduction

forever

CI Release license

Ensure the program runs continuously.

Overview

This library provides few utility functions which enable you to invoke a function and ensure it continues to execute.
In case the function exists due to a crash or simply because it finished, the forever library will reinvoke it.
The forever function provides control over the amount of retries and time to wait between intervals via parameters or via callback for a more fine grained control.
The provided function is invoked as a forked process, so any crash is caught and when restarts, it will be started as a new process with a new state.

Usage

#include "forever.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>


void my_program(void *context)
{
  if (context != NULL)
  {
    // do something with the context
  }

  sleep(1);
  exit(0);
}


int callback(void *context, const unsigned char started, int stat_loc)
{
  if (context != NULL)
  {
    // do something with the context
  }

  if (stat_loc == 0 || !started)
  {
    return(-1); // no more retries
  }

  return(500); // wait 500 millies before next invocation, 0 for no wait.
}


int main()
{
  void *context = NULL;

  // call 'my_program' and when it ends/crashes invoke it again, up
  // to 10 times and wait 250 millies between invocations.
  // counter will hold the amount of times 'my_program' was invoked.
  unsigned int counter = forever_with_options(
    my_program, // function to invoke
    context,    // context that is passed to the function on every invocation
    10,         // max amount of retries. 0 for unlimited retries.
    250         // amount of millies to wait between invocations. 0 for no wait.
    );

  printf("Invoked %u time/s.\n", counter);

  // call 'my_program' and when it ends/crashes call the provided callback
  counter = forever_with_callback(my_program, context, callback);
  printf("Invoked %u time/s.\n", counter);

  // run with unlimited retries and no delay between invocations
  forever(my_program, context);
}

Contributing

See contributing guide

Release History

See Changelog

License

Developed by Sagie Gur-Ari and licensed under the Apache 2 open source license.

c_forever's People

Contributors

sagiegurari avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.