Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RedmineReceivingEmails and the cronjob #64

Open
Inkimar opened this issue Apr 18, 2017 · 18 comments
Open

RedmineReceivingEmails and the cronjob #64

Inkimar opened this issue Apr 18, 2017 · 18 comments
Labels
Request Request for image modification or feature

Comments

@Inkimar
Copy link

Inkimar commented Apr 18, 2017

Following the guide here:
http://www.redmine.org/projects/redmine/wiki/RedmineReceivingEmails
Using the approach 'fetching emails from an IMAP server' and that is working great.
My project is here: https://github.com/DINA-Web/redmine-docker/tree/task760
I have created a script with the 'rake'-command to harvest my mailbox : receive_imap.sh with the following content

#!/bin/bash
source .env
echo "Mail $(date)" >>/var/log/cron-mail.log 2>&1 && rake -f /usr/src/redmine/Rakefile redmine:email:receive_imap RAILS_ENV=production host=imap.gmail.com port=993 ssl=1 username=$user password=$psw project=myproject tracker=Support

The user and the psw are in the .env-file
running the script manually works fine and an issue is created in my project 'myproject'.

The entry in the crontab (root account ) is like this >
* * * * * /usr/src/redmine/mail-script/receive_imap.sh

For the sake of testing I am running it right now every minute.
The only thing that I am seeing is the /var/log/cron-mail.log - no 'harvesting of my mailbox'.

Summary
Being new to Ruby.
One thing I see is that the crontab cannot find the environment for Ruby, it says it cannot find rake version 12.
As I understand this, this question boils down to 'the environment variables are not present with me when running crontab' - I tried to copy every environmental variable for root to a file and running source on that file when starting the script, but this does not help.

regards, Ingimar

@kebyn
Copy link

kebyn commented Jul 31, 2017

@yosifkit Hello, this docker does not have a planned mission environment, so
http://www.redmine.org/projects/redmine/wiki/RedmineReminderEmails
can not be done , I need your help, thank you very much .

@ToniIltanen
Copy link

I have the same issue, any progress on this?

@maximkrusina
Copy link

Yep, need some nice way to add cron jobs! E-mail fetfer and regular notifications...

@flantel
Copy link

flantel commented Apr 25, 2018

Hi

I have just done this using the rufus-scheduler method and it appears to work fine.

It requires that you create your own Docker image, but really this is required if you are adding any plugins or to configure your outbound email settings.

My Dockerfile looks like this:

FROM redmine:3.4

ENV TZ=Europe/Dublin
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

WORKDIR /usr/src/redmine
COPY plugins plugins/
COPY configuration.yml config/configuration.yml
RUN gem install rufus-scheduler
COPY getmail.rb config/initializers/getmail.rb
RUN echo "gem 'rufus-scheduler'" >> Gemfile
RUN bundle install --without development test 

The getmail.rb then configures rufus-scheduler:

require 'rubygems'
require 'rake'
require 'rufus-scheduler'

load File.join(Rails.root, 'Rakefile')

ENV['host']='imap.gmail.com'
ENV['port']='993'
ENV['ssl']='SSL'
ENV['username']='user@example.com'
ENV['password']='mypassword'
ENV['project']='support'

scheduler = Rufus::Scheduler.new
# Check emails every 10 mins
scheduler.interval '10m' do
  task = Rake.application['redmine:email:receive_imap']
  task.reenable
  task.invoke 
end

In the getmail.rb, you can set any of the receive email parameters using ENV , like default-group and project_from_subaddress, etc.

Hope this helps.

-Barry Flanagan

@wglambert wglambert added the Request Request for image modification or feature label Apr 25, 2018
@maximkrusina
Copy link

Hi everybody, finally I decided to handle all cron jobs via another external container, here is something interesting: http://deck-chores.readthedocs.io/en/stable/index.html

Why? Coz our production server is getting more and more containers I need one centralized place for managing cron jobs. One, place, easy to read and configure, no need to manually adjust each of the containers.

@michael-fuchs
Copy link

I used @flantel 's approach with the rufus-scheduler using the docker image for redmine 4.0. I had the issue that Redmine:IMAP was not found and I got following ruby error:

rufus-scheduler intercepted an error:
redmine_1               |   47305684005800   job:
redmine_1               |   47305684005800     Rufus::Scheduler::EveryJob "30s" {}
redmine_1               |   47305684005800   error:
redmine_1               |   47305684005800     47305684005800
redmine_1               |   47305684005800     NameError
redmine_1               |   47305684005800     uninitialized constant Redmine::IMAP

Same with POP3.
I solved this by an additional import in the rufus-scheduler script (getmail.rb in the example above):
require 'redmine/imap.rb'

@lmoellendorf
Copy link

If you do not want to create your own Docker image you could also add a cronjob on the host.

Create the script /home/user/docker/redmine/receive_imap.sh with the content below:

#!/bin/bash
docker container exec -i docker_redmine_1 rake -f /usr/src/redmine/Rakefile redmine:email:receive_imap RAILS_ENV="production" host=imap.yourmail.org username=redmine@yourmail.org password=secret starttls=1 >> /tmp/receive_imap.log 2>&1

Where docker_redmine_1 is your container's name which you can retrieve using docker container ls

Make the script executable:

chmod u+x /home/user/docker/redmine/receive_imap.sh

Edit the crontab of the user which is running the container:

crontab -e

...and add this line to execute your script

* * * * * /home/user/docker/redmine/receive_imap.sh >> /tmp/cron.log 2>&1

This will execute every minute.

@rafaelteb
Copy link

rafaelteb commented Mar 31, 2021

Hi there!
Im having an issue in this line I think. Im getting pretty crazy currently. Got the feeling that I miss something really simple :D
Would be very thankful for any help.
Maybe I should add rufus to a Dockerfile like its done above?

Currently wanted to setup with cron. But Im constantly getting this error:

rake aborted! OpenSSL::SSL::SSLError: SSL_connect returned=1 errno=0 state=error: certificate verify failed (unable to get local issuer certificate)

Opened this issue on this subject: #232

@EchedelleLR
Copy link

#64 (comment)

If you do not want to create your own Docker image you could also add a cronjob on the host.

I am unsure this would work if the container is inside a Kubernetes cluster which could change machine in each restart or, mostly, if the cluster is managed in the cloud such as ECS by AWS.

@EchedelleLR
Copy link

EchedelleLR commented Nov 4, 2022

I have some proposals of changes to fix the issue permanently:

  • install cron, logrotate and supervisor
  • create a supervisor task for cron (cron -f) running as root and rails running as redmine
  • current docker-entrypoint.sh would be removed from the Dockerfile and be put as command of execution in rails task at supervisor
  • the old docker-entrypoint.sh could be modified changing its name and either:
    • replacing isLikelyRedmine to check on supervisor task filenames (for rails, rake, etc)
    • or just passing the correct parameter to run Redmine itself
    • in both cases, the function to re-execute as redmine user would be removed as it would be already being executed as that by the supervisor task configuration
  • create logrotate configuration for supervisor logs
  • optionally install rsyslog and create its corresponding supervisor task to check cron jobs execution logs

I have the following questions:

  • where are you installing extra dependencies after changing to redmine user? The fact that is not at GEM_HOME makes it more difficult to run cron jobs unless I create a 1-time not-autostarted not-restarted supervisor task for redmine user and calls it from cron
  • is there a way to setup a global place to install the dependencies or is this a Redmine requirement? Just a mix of global (fixed place) and local dependencies (somewhere but knowing by execution model and .bundle cache that it is done at redmine user seems very messy

@tianon
Copy link
Member

tianon commented Nov 4, 2022 via email

@EchedelleLR
Copy link

I am unsure to understand. supervisor, in contrast to the average init system, is focused in a custom control from user/administrator side as it is thought to even be running on top of a real init system but which works perfectly in Docker images for tasks like this.

@EchedelleLR
Copy link

EchedelleLR commented Jan 30, 2023

@tianon Recently, I have seen that Nextcloud allows 3 options for scheduled tasks at their side.

The first being AJAX requests while loading the frontend and the second being using an external scheduled request call a PHP script which performs the tasks internally.

This last option could be something that Redmine could implement I guess. Given that this is more important for get it working properly on Docker, I guess this should be performed at this side?

A little endpoint in RubyOnRails to make the tasks after getting a scheduled curl request?

@EchedelleLR
Copy link

I submitted an issue at https://www.redmine.org/issues/38226 anyways to cover both sides in the case is preferable to be implemented by upstream.

@petiepooo
Copy link

I too am opposed to the inclusion of cron, logrotate, and supervisor. The latest version finally got rid of all the detritus involved in using passenger.

The inclusion of an in-Rails scheduler fits more with Docker's "single-process-per-container" design pattern. Would love to see rufus-scheduler or something similar included as part of the base image so we can still run the stock official image.

@EchedelleLR
Copy link

@petiepooo the proposal I made to the Redmine team is quite different already.

Maybe you could appreciate it since it is what Nextcloud uses optionally.

@petiepooo
Copy link

@EchedeyLR If I am reading #38226 correctly, your solution would still require an external app (like curl or cron) to launch the RAKE task. I would prefer puma launch periodic tasks directly per flantel's solution using rufus-scheduler.

As for an AJAX or REST interface to create/manage/launch tasks, I am not opposed, but don't need that functionality myself. What I am opposed to is running multiple processes within a single docker container. Requiring supervisor or tini in an image should be avoided, IMHO.

@EchedelleLR
Copy link

EchedelleLR commented Mar 9, 2023

The problem with Rufus scheduler is that would likely behave the same as the cron proposal (basically, every single node of the instance would have its own scheduler being called and sending a notification for each node) in HA setups, which may be a big problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Request Request for image modification or feature
Projects
None yet
Development

No branches or pull requests