Coder Social home page Coder Social logo

copleykj / socialize-server-presence Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 5.0 35 KB

A Meteor package to keep track of your running app instances and help you clean up the mess when they fail.

License: MIT License

JavaScript 100.00%
sponsor meteorjs meteor-package presence servers meteor

socialize-server-presence's Introduction

Server Presence

This packages keeps track of servers running your application and provides a way to run cleanup tasks when they die

Supporting The Project

Finding the time to maintain FOSS projects can be quite difficult. I am myself responsible for over 30 personal projects across 2 platforms, as well as Multiple others maintained by the Meteor Community Packages organization. Therfore, if you appreciate my work, I ask that you either sponsor my work through GitHub, or donate via Paypal or Patreon. Every dollar helps give cause for spending my free time fielding issues, feature requests, pull requests and releasing updates. Info can be found in the "Sponsor this project" section of the GitHub Repo

Installation

meteor add socialize:server-presence

API

ServerPresence.serverId(); - Get the current Id of the server.

Meteor.publish(null, function(){
    if(this.userId && this._session){
        var id = UserSessions.insert({serverId:ServerPresence.serverId(), userId:this.userId, sessionId:this._session.id});

        this.onStop(function(){
            UserSessions.remove(id);
        });
    }
}, {is_auto:true});

ServerPresence.registerCleanupFunction(function) - registers a function to clean up data related to a particular server. If the server died cleanly the function will be passed the ID of the server and you should take action to clean up data just for this particular server. If the server died without cleaning up after itself and was the only server instance running the cleanup function will be called without a serverId parameter and depending on your app you may want to do a global cleanup.

ServerPresence.onCleanup(function(serverId){
    if(serverId){
        UserSessions.remove({serverId:serverId});
    }else{
        UserSessions.remove({});
    }
});

socialize-server-presence's People

Contributors

copleykj avatar dependabot[bot] avatar storytellercz avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

socialize-server-presence's Issues

ServerPresence.onCleanup not always being called

ServerPresence.onCleanup was not always being called when meteor was killed and then restarted.

We fixed this by changing the observe function

var observe = function(){
    var self = this;
    observeHandle = Servers.find().observe({
        removed: function(document){
            if(document._id === serverId){
                if(!isWatcher){
                    throw new Meteor.Error("Server Presence Timeout", "The server-presence package has detected inconsistent presence state. To avoid inconsistent database state your application is exiting.");
                    process.exit();
                }else{
                    insert();
                }
            }else if(isWatcher){
                if(!document.graceful){
                    runCleanupFunctions(document._id);
                }
            }else if(document.watcher){
                if(!document.graceful){
                    runCleanupFunctions(document._id);                //Cleanup here as well
                }
                updateWatcher();
            }
        }
    });
};

Incompatible with Meteor's DDPGracefulShutdown

Hey, super cool package! We are currently using it in a very large scale Meteor app and it's causing us issues with Meteor's DDPGracefulShutdown.

DDPGracefulShutdown aims to disconnect users slowly on a SIGTERM to ease deployments.

The problem seems to be coming from the process.exit() in the server collection observer which kills servers during deployments, cancelling DDPGracefulShutdown.

Would it be possible to terminate the process in a graceful manner?
Or to remove process.exit() altogether knowing that the server will write to DB on the next heartbeat anyway?

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.