Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing Sinatra application scope from Rake task
    primarykey
    data
    text
    <p>I have a global variable in a Sinatra application that I want to update with a scheduled task from my Rakefile. Note that the application is hosted on Heroku. I have set up helpers in order to access this variable.</p> <pre><code>get '/' do @@var end helpers do def get_var return @@var end def set_var(value) @@var = value end end </code></pre> <p>Here is the task in my Rakefile:</p> <pre><code>task :do_something do Sinatra::Application.set_var(get_data) end def get_data # Retrieve value from external source ... return val end </code></pre> <p>The problem I run into is that the task executes properly, but the variable in the Sinatra application is never updated. I assume this is because calling <code>Sinatra::Application</code> from within the Rakefile actually creates a separate instance of the application from the primary instance that I am trying to update.</p> <p>I want to know if their is a way to access the scope of the running Sinatra web app from within a Rakefile task.</p> <p>*Note: I could just write the value retrieved in the scheduled task to a database and then access it from the Sinatra app, but that would be overkill because this variable is updated so infrequently but retrieved so regularly that I would prefer to keep it in memory for easier access. I have looked into Memcache and Redis in order to avoid turning to a database, but again I feel that this would be excessive for a single value. Feel free to disagree with this.</p> <p>EDIT: In regards to Alexey Sukhoviy's comment, Heroku does not allow writing to files outside of the tmp directories, and these are not kept alive long enough to satisfy the needs of the application.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload