Giter Club home page Giter Club logo

sugar-glider's Introduction

sugar-glider

A way of executing a piece of code on a remote clojure instance and retrieving its result.

Usage

Starting the server on one instance

(use 'sugar-glider.server)

(def server 
    (glider-server { :port 4443 }))

(glider-server-start! server)

;(glider-server-stop! server) stops the server whenever you want

Connecting to that server from another instance

(use 'sugar-glider.client)

(def connection
    (glider-connect   { :host "localhost"
                        :port 4443        
                        :ns   'project.shared
                        :env  [ '(use 'lamina.core)
                                '(require '[aleph.http :as ahttp])
                                '(require '[aleph.tcp :as atcp])
                                '(import 'java.util.Date)         ] }))

Lets look at the glider-connect params individually. First:

{ :host "localhost"
  :port 4443

Pretty self explanatory; the host and port of the sugar-glider server you're connecting to


:ns   'project.shared

This indicates the namespace you want the connection to "live" in on the remote node. If you haven't already you may want to scroll down in the docs a bit to get an idea of what exactly sugar-glider does. Basically you're going to be executing arbitrary code on the remote server; this field indicates the namespace you want that code to run in. You can make up your own or use an existing one. Be warned, however, that commands like def will work, so it's possible to clobber and corrupt an existing namespace.


:env  [ '(use 'lamina.core)
        '(require '[aleph.http :as ahttp])
        '(require '[aleph.tcp :as atcp])
        '(import 'java.util.Date)         ]

This is the environment you want the connection to exist in. Along with the namespace specified previously, here is where you specify all the uses/requires/imports you want that namespace to have on it.

Additionally if you want to use an already existing namespace in your :ns field you will have to add a use on the namespace here in order to make that namespace's pre-existing definitions available to the connection.

I recommend that you don't use this for anything except useing the connection's namespace. If you put all code in the location where it will actually be run (on the remote server, in this instance) then things will be simpler and easier to track with version control. They will also be faster since you won't be sending whole defs over the wire, just calls to them, which will certainly be faster.

Using the connection

Any arbitrary code can be sent over and evaluated. This uses a macro on the backend to quote the entirety of the second argument, serialize it, and send it over. The call to glide will block until the evaluated result is returned, and returns that result directly

(glide connection (+ 1 2))
;3

glide-async can be used if you don't care about the return value. It will return nil immediately, the expression to be evaluated will be added to a queue and sent in the background.

(glide-async connection (+ 1 2))
;nil

License

Copyright © 2012 Brian Picciano

Distributed under the Eclipse Public License, the same as Clojure.

sugar-glider's People

Watchers

James Cloos avatar Brian Picciano 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.