Git Product home page Git Product logo

heroku-addonpool's Introduction

Manage Addon Pool of an App in Heroku.

# NOTE:
# The app with addons (pool app) must be different from the app
# that uses the addons because heroku resets the pool app each
# time a configuration variable is changed.
# set heroku cli login in environment variables
[email protected]
HEROKU_CLI_PASSWORD=yourpassword

# also as of now you also need to purge cache before build
heroku repo:purge_cache -a yourpoolapp
var Pool = require('heroku-addonpool');
// Pool(<id>, <app>, <opt>)

/* Assume "ci-herokuaddonpool" has 2 postgresql addons provisioned.
 * There are 3 consumers which need access to a postgresql database,
 * for a limited amount of time. Since we have only 2 available, we
 * create a pool to enable consumers to acquire (remove) and release
 * (add) database from the pool. If no database is available in the
 * pool, a consumer will have to wait (Promise) until the resource
 * is released by some other consumer.
 */

var pg = Pool('heroku-postgresql', 'ci-herokuaddonpool', {
  'config': /(HEROKU_POSTGRESQL|DATABASE)\S*URL/g,
  'log': true
});

pg.setup().then((ans) => {
  var cona = 'consumer-a';
  var conb = 'consumer-b';
  var conc = 'consumer-c';
  console.log(ans);      // available addons (Map)
  console.log(ans.size); // number of addons
  pg.remove(cona).then((ans) => {
    console.log(ans.name);        // name of the addon
    console.log(ans.attachments); // app to addon attachments
    console.log(ans.installedAt); // provision date
    console.log(ans.owningApp);   // owner app of this addon
    console.log(ans.plan);        // addon service:plan
    console.log(ans.price);       // addon price
    console.log(ans.state);       // addon state
    console.log(ans.value);       // addon access url
    console.log(`${cona} has acquired ${ans.name}`);
    console.log(`-> connection string: ${ans.value}`);
    // consumer-a uses database for 20s
    setTimeout(() => {
      console.log(`${cona} is releasing ${ans.name}`);
      pg.add(cona);
    }, 20000);
  }).then(() => {
    return pg.remove(conb);
  }).then((ans) => {
    console.log(`${conb} has acquired ${ans.name}`);
    console.log(`-> connection string: ${ans.value}`);
    // consumer-b uses database for 10s
    setTimeout(() => {
      console.log(`${conb} is releasing ${ans.name}`);
      pg.add(conb);
    }, 10000);
  }).then(() => {
    return pg.remove(conc);
  }).then((ans) => {
    // consumer-c waits for 10s until consumer-b releases
    console.log(`${conc} has acquired ${ans.name}`);
    console.log(`-> connection string: ${ans.value}`);
    // consumer-c uses database for 10s
    setTimeout(() => {
      console.log(`${conc} is releasing ${ans.name}`);
      pg.add(conc);
    }, 10000);
  });
});

heroku-addonpool's People

Contributors

wolfram77 avatar

Watchers

 avatar  avatar

Forkers

fishdom888

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.