Skip to content
This repository has been archived by the owner on Oct 1, 2020. It is now read-only.

Environment vars not accessible by service #84

Closed
alissonperez opened this issue Oct 31, 2017 · 7 comments
Closed

Environment vars not accessible by service #84

alissonperez opened this issue Oct 31, 2017 · 7 comments

Comments

@alissonperez
Copy link

Hi guys!

I'm trying to fill systemd Environment variables from celery_worker. I saw it has ServiceMixin from poise-service interface but unfortunately I can't find a way to pass my env vars to service environment.

I'm trying to do like this:

application 'blabla' do
   environment ({
     :REMAP_SIGTERM => 'SIGQUIT'
   })

   celery_worker do
     app_module 'tasks'
     user 'celery'
     service_name "myapp"
   end
end

But my Environment entry on systemd service conf still empty.

Am I doing something wrong?

Thanks in advance!
Alisson

@coderanger
Copy link
Member

Ruby syntax oops, remove the space between environment and (. With that space there it's a block argument. Or just do environment REMAP_SIGTERM: 'SIGQUIT' to use all the various optional syntax bits :)

@alissonperez
Copy link
Author

O.o! Now works (sometimes I forgot that it's Ruby, haha).

And it needs to be just at beginning of application block (It was in the middle here).

Thank you @coderanger !

@coderanger
Copy link
Member

Ordering shouldn't matter in theory, though I would have to step through the code to see if I screwed up a copy or something.

@alissonperez
Copy link
Author

Yes, strange @coderanger . Here is my full recipe, with this config (environment in the middle) it do not pass my env var to systemd conf, but if it is just after start application block it works.

node['python-app'].each do |app|
  application '/srv/' + app['name'] do
    git do
      repository app['repo']
      revision app['branch'] if app['branch']
      deploy_key data_bag_item('ssh_keys', app['deploy_key'])['key'] if app['deploy_key']
    end

    virtualenv

    pip_requirements

    # Setup environment file
    env_filename = "/srv/#{app['name']}/env.prod.py"

    # Just to not break deploy
    file env_filename do
      content ''
      mode '0777'
      action :create_if_missing
    end

    execute 'create env modue' do
      command "cp #{env_filename} /srv/#{app['name']}/env.py"
    end

    # We need this to do a warm shutdown with Celery, with this it's possible to
    # use auto scalling and other features that needs to send a SIGTERM to celery worker
    # and not lose current tasks in process.
    #
    # For more information about this ENV VAR: https://github.com/celery/celery/issues/2839
    #
    # With this env var it's good to use a DefaultTimeoutStopSec as infinity at default config or a
    # TimeoutStopSec with same value at your service.
    environment REMAP_SIGTERM: 'SIGQUIT'

    # SystemD default conf
    template '/etc/systemd/system.conf' do
      source 'systemdconf.erb'
      mode '0644'
      owner 'root'
      group 'root'
    end

    # Setup celery
    if app.fetch('celery', true)
      celery_config do
        options do
          celeryd_concurrency app.fetch('concurrency', 1)
          celery_default_queue app.fetch('queue_name', 'celery') if app.key? 'queue_name'

          # http://docs.celeryproject.org/en/latest/userguide/configuration.html#worker-prefetch-multiplier
          celeryd_prefetch_multiplier 1
        end
      end

      file "/srv/#{app['name']}/celeryconfig.py" do
        mode '0777'
      end

      celery_worker do
        app_module 'tasks'
        user 'celery'
        service_name "s#{app['name']}"
      end
    end
  end
end

@coderanger
Copy link
Member

Aha, yep, order does matter because I ||= things here https://github.com/poise/application/blob/master/lib/poise_application/resources/application.rb#L90

So if something tries to use the app state before you set the environment, it would be too late. I should figure out a better way to handle that.

@alissonperez
Copy link
Author

hum, understood @coderanger ! I'm using a lot of this recipes, If I figure out a good way to improve this I'll comment on issue or contribute by other way.
Thank you for your help!

@coderanger
Copy link
Member

Specifically in this case you need to set the property before using the virtualenv resource because that does a compile time change to the app state.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants