vuderacha-syrup 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +106 -26
- data/VERSION.yml +1 -1
- data/lib/syrup/manager.rb +6 -1
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -1,18 +1,28 @@
|
|
1
|
-
= Syrup -
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
= Syrup - Micro Application Servers
|
2
|
+
In the ever growing quest for more maintainable applications, it is often becoming enticing to move
|
3
|
+
towards loosely linked micro-applications. Whilst these applications are certainly easier to work with,
|
4
|
+
the configuration economies of the single larger application are often lost. In other worlds, application
|
5
|
+
servers take on the responsibility of providing common resources to applications. This is where Syrup comes
|
6
|
+
in for your micro-applications.
|
5
7
|
|
6
|
-
|
7
|
-
Syrup
|
8
|
-
|
8
|
+
Instead of needing to redefine configuration and setup steps for each of your applications over and over again,
|
9
|
+
Syrup lets you write a single common fabric, and then start each application up within this fabric. The fabric
|
10
|
+
can take care of connecting to your database, configuring your logs, initializing your messaging queueing, and
|
11
|
+
practically any other common activity. Your application can then just worry about being your application.
|
9
12
|
|
10
|
-
|
13
|
+
== Installation
|
14
|
+
Syrup can be installed in one of two ways. Via rubygems, with:
|
15
|
+
gem sources -a http://gems.github.com
|
16
|
+
gem install vuderacha-syrup
|
17
|
+
|
18
|
+
Or, if you prefer to live on the edge:
|
11
19
|
git clone git://github.com/vuderacha/syrup.git
|
12
20
|
cd syrup
|
13
|
-
rake install
|
21
|
+
rake gemspec build install
|
14
22
|
|
15
|
-
|
23
|
+
== A Standalone Service
|
24
|
+
If you're just interested in writing a background service, Syrup can make that easy. Assuming you have a simple
|
25
|
+
application such as:
|
16
26
|
# test_service.rb
|
17
27
|
while true
|
18
28
|
File.open('/tmp/test.txt', 'a') {|f| f << "a\n"}
|
@@ -20,34 +30,100 @@ Assuming you have a simple application such as:
|
|
20
30
|
|
21
31
|
Create a configuration file for Syrup to run it:
|
22
32
|
# config.sy
|
23
|
-
|
33
|
+
load 'test-service.rb'
|
24
34
|
|
25
|
-
For Syrup to
|
35
|
+
For Syrup to run an application, you need to "activate" it. This informs Syrup about the
|
26
36
|
path of the application, and allows it to automatically start the app again later when you
|
27
37
|
may not be around to tell it about it! Assuming that you've put your app into /home/syruptest/app,
|
28
38
|
then you'll just issue the command:
|
29
|
-
syrup activate /home/syruptest/app
|
30
|
-
|
31
|
-
|
39
|
+
syrup activate myapp /home/syruptest/app/config.sy
|
40
|
+
This will create an application record in /home/syruptest/.syrup/myapp/ containing the details of this
|
41
|
+
application.
|
32
42
|
|
33
|
-
|
43
|
+
Now you can start the application with:
|
44
|
+
syrup start myapp
|
45
|
+
And then, when you're done, stop it with:
|
46
|
+
syrup stop myapp
|
47
|
+
If you've registered multiple applications, you can start and stop them all at once with:
|
34
48
|
syrup start
|
35
|
-
|
36
|
-
Conversely, stop it with:
|
37
49
|
syrup stop
|
38
50
|
|
39
|
-
|
40
|
-
|
41
|
-
|
51
|
+
Whilst you're developing applications, running them in the background isn't always the easiest way to work
|
52
|
+
with them. Syrup provides the ability to run an application in the foreground with:
|
53
|
+
syrup run myapp
|
54
|
+
Applications run this way will run in the foreground, and can be terminated with Ctrl-C. You can even run
|
55
|
+
multiple applications by specifying multiple names:
|
56
|
+
syrup run myapp mysecondapp
|
57
|
+
Hitting Ctrl-C will terminate all of these applications at once - great for working on a "product" that consists
|
58
|
+
of a bunch of applications that you need to control all at once!
|
59
|
+
|
60
|
+
== Creating a Fabric
|
61
|
+
Of course, making an application run in the background really isn't all that interesting. Creating a Fabric
|
62
|
+
is what will make Syrup seem a whole lot more useful.
|
42
63
|
|
43
|
-
|
44
|
-
|
45
|
-
|
64
|
+
Start with creating a simple Fabric. Perhaps you just want to initialize a logging framework. To do that, start
|
65
|
+
with a fabric application file:
|
66
|
+
# myfabric.rb
|
67
|
+
require 'rubygems'
|
68
|
+
require 'log4r'
|
69
|
+
|
70
|
+
# Configure a stdout outputter on a top level logger
|
71
|
+
formatter = Log4r::PatternFormatter.new(:pattern => "[%l] [#%c] %d :: %m")
|
72
|
+
logger = Log4r::Logger.new 'MyApps'
|
73
|
+
logger.outputters = Log4r::StdoutOutputter.new 'console', :formatter => formatter
|
74
|
+
|
75
|
+
# Register that this Fabric provided logging features
|
76
|
+
Syrup.fabric_support.register_feature :logging
|
77
|
+
|
78
|
+
# Inform the actual application that it can start
|
79
|
+
Syrup::Runner.run_application
|
80
|
+
|
81
|
+
Now we build an application that sits on tops of this fabric:
|
82
|
+
# config.sy
|
83
|
+
fabric_requirement :logging # Ensure that the Fabric had logging support
|
84
|
+
load 'myapp.rb'
|
85
|
+
|
86
|
+
To get this application running:
|
87
|
+
syrup activate myapp config.sy
|
88
|
+
syrup weave myapp myfabric.rb
|
89
|
+
syrup start myapp
|
90
|
+
|
91
|
+
Syrup will start a process, and execute the Fabric. When the Fabric says
|
92
|
+
Syrup::Runner.run_application
|
93
|
+
the underlying application will be started.
|
94
|
+
|
95
|
+
At this point, you're probably thinking "why don't I just 'require' my Fabric". Well, you could. But what if we
|
96
|
+
made the Fabric do something more interesting?
|
97
|
+
# myemfabric.rb
|
98
|
+
require 'rubygems'
|
99
|
+
require 'eventmachine'
|
100
|
+
|
101
|
+
EM.run {
|
102
|
+
# Do something that has to be started within EventMachine
|
103
|
+
EM.defer { puts "Hello World, from the background!" }
|
104
|
+
|
105
|
+
# Register that this Fabric is providing EventMachine support
|
106
|
+
Syrup.fabric_support.register_feature :logging
|
107
|
+
|
108
|
+
# Run the application
|
109
|
+
Syrup::Runner.run_application
|
110
|
+
}
|
111
|
+
|
112
|
+
# config.sy
|
113
|
+
fabric_requirement :eventmachine
|
114
|
+
load 'myemapp.rb'
|
115
|
+
|
116
|
+
# myemapp.rb
|
117
|
+
EM.defer { puts "Hello World. Again from the background." }
|
118
|
+
|
119
|
+
In this application, we're making the Fabric do something that is a little harder just to require. The Fabric is now
|
120
|
+
doing something where the application needs to run "in the middle". Hopefully, it would be easily imaginable that the
|
121
|
+
Fabric would be able to startup and teardown various resources around the application running.
|
46
122
|
|
47
123
|
== Persistent Configuration
|
48
124
|
Often, applications need per deployment configuration. One example is configuring whether a given host is
|
49
|
-
production or development. Whilst there are many ways to do this, Syrup adds yet another.
|
50
|
-
|
125
|
+
production or development. Whilst there are many ways to do this, Syrup adds yet another. To add global configuration
|
126
|
+
for any application run as the given user, then you can simply state:
|
51
127
|
syrup set PROP=VALUE
|
52
128
|
This property will be permanently stored (in ~/.syrup/props), and will be applied into the environment of
|
53
129
|
all applications loaded through Syrup at their next start. So, for example, if your app had a line that read
|
@@ -55,3 +131,7 @@ all applications loaded through Syrup at their next start. So, for example, if y
|
|
55
131
|
then executing
|
56
132
|
syrup set RACK_ENV=production
|
57
133
|
would result in the application reporting "production" upon reaching the aforementioned line.
|
134
|
+
|
135
|
+
For applications that need their own individual configuration,
|
136
|
+
syrup --application myapp set RACK_ENV=special
|
137
|
+
will result in configuration being added for just your application.
|
data/VERSION.yml
CHANGED
data/lib/syrup/manager.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'syrup/config_store'
|
2
2
|
require 'yaml'
|
3
|
+
require 'fileutils'
|
3
4
|
|
4
5
|
module Syrup
|
5
6
|
# Manager for controlling the Syrup functionality
|
@@ -38,10 +39,14 @@ module Syrup
|
|
38
39
|
# Record our PID
|
39
40
|
app.pid = Process.pid
|
40
41
|
|
42
|
+
# Calculate the log filename for the application, and make sure the directory exists
|
43
|
+
log_fn = File.join(File.dirname(app.app), 'log', "#{app_name}.log")
|
44
|
+
FileUtils.mkdir_p File.dirname(log_fn)
|
45
|
+
|
41
46
|
#Dir.chdir @working_dir
|
42
47
|
File.umask 0000
|
43
48
|
STDIN.reopen "/dev/null"
|
44
|
-
STDOUT.reopen
|
49
|
+
STDOUT.reopen log_fn, "a"
|
45
50
|
STDERR.reopen STDOUT
|
46
51
|
trap("TERM") {exit}
|
47
52
|
|