Author: sifars

  • Best way to handle background processes in linux

    Best way to handle background processes in linux

    Reading Time: < 1 minute

    Every programmer soon comes to a point where he requires to setup several jobs to run in the background with 100% uptime. For example a website server, email server, job schedulers etc.

    These processes are more like a daemon process. And as like any other job, these tend to stop due to mishandling or uncatchable exceptions/errors. Rather to put a person to sit and constantly monitor the jobs, we make use of the Supervisor.

    A Supervisor is a process control system that will start, stop and monitor all our processes and will restart any, that fails.

    To install Supervisor on Ubuntu:

    sudo apt-get install supervisor

    Configuring Supervisor

    Let say we have a command that should run indefinitely:

    python run_this_command

    Supervisor configuration files are stored in the /etc/supervisor/conf.d directory. Let’s create a new configuration file as monitor-my-process.conf.

    Inside monitor-my-process.conf write:

    [program:my-worker]
    process_name=%(program_name)s_%(process_num)02d
    command=python run_this_command
    autostart=true
    autorestart=true
    user=linux_user
    numprocs=8
    redirect_stderr=true
    stdout_logfile=/home/linux_user/app.com/worker.log

    In this example, the numprocs directive will instruct Supervisor to run 8 processes of my command and monitor all of them, automatically restarting them if they fail. Also change the ‘linux_user’ to the user you want the process to be run as.

    Starting Supervisor

    Once the configuration file has been created, you may update the Supervisor configuration and start the processes using the following commands:

    sudo supervisorctl reread
    sudo supervisorctl update
    sudo supervisorctl start my-worker:*

    Cheers! Your processes will run indefinitely with auto restart.

    Bonus!

    Few more commands that are likely to be used sometime later:

    Stop all supervisor workers:

    sudo supervisorctl stop my-worker:*

    Restart all supervisor workers:

    sudo supervisorctl restart my-worker:*

    Get the current status of workers:

    sudo supervisorctl status my-worker:*

  • Vuejs: Constants from back end API

    Vuejs: Constants from back end API

    Reading Time: 2 minutes

    Hardcoded values make a developer’s job hectic if these are repeated at several places in the codebase. That is why constants are an indispensable part of an application. It’s easy and intuitive to keep your application constants in a separate file and import them in other files to use.

    Vue Js Constants
    import constants from 'path_to_file/constants.js'
    
    

    But what when your constants reside on the backend and you need to get them by making an ajax request to the server. how would you make sure that these constants are available before any of your components get rendered? Should we write the received constants object into a file and then export it. It doesn’t sound like a real solution. Of course, it is not.

    Object Prototype

    So let’s approach it with a real-world solution. First, we need to understand function constructor and its prototype. In javascript, each object created using a function constructor has access to its prototype.

    /** 
     * A function to create other objects. We captialize its name to distinguish 
     * it from other normal functions.
     */
    function Person(firstname) {
      this.firstname = firstname;
    }
    
    
    // Add a property to its prototype
    Person.prototype.printFirstName = function() {
      console.log(this.firstname);
    }
    
    // Create two different objects using the function as constructor
    let person1 = new Person('Munish')
    let person2 = new Person('John')
    

    Since person1 and person2 are created using a function constructor so it has access to the printFirstName method.

    person1.printFirstName()   // Munish
    person2.printFirstName() // John
    

    Vue Instance Prototype

    We will use the same concept by fetching our constants object from backend and then putting it into Vue prototype. So that each component in our application has access to this property.

    axios.get('http://SERVER_ADDRESS/getConstants')
    .then(function(res) {
    
      Vue.prototype.$constants = res.body.data;
      // Initialize the main instance once you receive the constants from backend
      new Vue({
        el: '#app'
      })
    
    });
    

    Now you can access this constant object in any of your Vue components using the following syntax

    this.$constants.someConstant
    

    Note:

    Make sure your getConstants API doesn’t do any heavy lifting other than returning your constants. Because the front end application will not get bootstrapped until it responds back.