Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Gems with Persistent data

Tags:

file

linux

ruby

gem

I want to create ruby application (not rails). This is a console app which will need to persist some data. I'm using pstore as the database. I want to deploy this application as a gem.

My question is: where does my data live?

Currently I've created a data directory as a sibling to the bin directory in a standard gem layout. I would therefore, expect that the gem would be storing its data "inside itself" after it gets deployed. But when I do a local gem install to test, I find that the data is being stored locally to the project files, not somewhere inside the gems directory.

Of course it could be I simply mis-understand what "rake install_gem" is doing. Also, I am vaguely worried that if I need to sudo to install the gem, that it will actually be able to create the data file "inside itself" in the gem directory.

Can someone clarify this a little bit?

Thank you. John Schank

@makevoid - thanks for the reply. Here is the entirety of my main script. In /bin directory... (I added it to the main question because I'm not familiar with how to format content in a comment - and the pasted code looked awful.

#!/usr/bin/env ruby

$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'

require 'timesheet'

begin
  command_hash = TimesheetParser.parse
  store = YAML::Store.new("data/time_entries.yaml")
  tl = TimeLog.new(store)
  ts = Timesheet.new(tl)
  ts.process(command_hash)
rescue Exception => e
  raise if command_hash[:debug]
  puts e.message
like image 445
jschank Avatar asked Jan 02 '10 07:01

jschank


1 Answers

On Linux there are two common used locations for storing variable data.

/home/user/.application

If every user needs it's own storage this is usually done in the users home directory. The path for your storage in the users home directory should be

ENV["HOME"] + "/." + $application_name

/var/lib/application

If all users share the storage, or the application is intended to be run by only one user (most daemons), /var is the right place to store all kind of data.

  • /var/log for logs
  • /var/run for pid's
  • /var/lock for lock files
  • /var/www for httpservers
  • /var/tmp for not important but persistant data
  • /var/lib for all other data

The path for your storage in /var should be

"/var/lib/" + $application_name

Make sure, the permissions for this directory are such, that you don't have to let your application run as root.

like image 65
johannes Avatar answered Sep 28 '22 20:09

johannes