Repository: fossasia/open-event-server

Issue:
Error on running celery 
Got an error after running 
`INTEGRATE_SOCKETIO=false celery worker -A app.celery`


> [2017-09-15 17:58:31,192: ERROR/MainProcess] Task rebuild.events.elasticsearch[0b5bc452-3fc1-45b9-93a3-cdae9844dc31] raised unexpected: ConnectionError('N/A', '<urllib3.connection.HTTPConnection object at 0x7f6728c83050>: Failed to establish a new connection: [Errno 111] Connection refused', NewConnectionError('None: None',))
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/celery/app/trace.py", line 240, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/celery/app/trace.py", line 438, in __protected_call__
    return self.run(*args, **kwargs)
  File "/home/bhavesh/Desktop/bhaveshAn/fossasia/open-event-orga-server/nextgen/app/views/elastic_cron_helpers.py", line 37, in cron_rebuild_events_elasticsearch
    es_store.indices.delete('events')
  File "/usr/local/lib/python2.7/dist-packages/elasticsearch/client/utils.py", line 73, in _wrapped
    return func(*args, params=params, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/elasticsearch/client/indices.py", line 200, in delete
    params=params)
  File "/usr/local/lib/python2.7/dist-packages/elasticsearch/transport.py", line 312, in perform_request
    status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout)
  File "/usr/local/lib/python2.7/dist-packages/elasticsearch/connection/http_urllib3.py", line 123, in perform_request
    raise ConnectionError('N/A', str(e), e)
ConnectionError: ConnectionError(<urllib3.connection.HTTPConnection object at 0x7f6728c83050>: Failed to establish a new connection: [Errno 111] Connection refused) caused by: NewConnectionError(None: None)



Discussion:
@shubham-padia  I am not getting this error it's just i cannot connect to `redis://localhost:6379/0`
```
[2017-09-15 19:03:30,665: WARNING/MainProcess] /usr/lib/python2.7/site-packages/celery/apps/worker.py:161: CDeprecationWarning: 
Starting from version 3.2 Celery will refuse to accept pickle by default.

The pickle serializer is a security concern as it may give attackers
the ability to execute any command.  It's important to secure
your broker from unauthorized access when using pickle, so we think
that enabling pickle should require a deliberate action and not be
the default choice.

If you depend on pickle then you should set a setting to disable this
warning and to be sure that everything will continue working
when you upgrade to Celery 3.2::

    CELERY_ACCEPT_CONTENT = ['pickle', 'json', Cannot connect to redis://localhost:6379/0'msgpack', 'yaml']

You must only enable the serializers that you will actually use.


  warnings.warn(CDeprecationWarning(W_PICKLE_DEPRECATED))
 
 -------------- celery@deathping v3.1.25 (Cipater)
---- **** ----- 
--- * ***  * -- Linux-4.12.12-1-ARCH-x86_64-with-glibc2.2.5
-- * - **** --- 
- ** ---------- [config]
- ** ---------- .> app:         app.views.celery_:0x7fad04728590
- ** ---------- .> transport:   redis://localhost:6379/0
- ** ---------- .> results:     disabled://
- *** --- * --- .> concurrency: 4 (prefork)
-- ******* ---- 
--- ***** ----- [queues]
 -------------- .> celery           exchange=celery(direct) key=celery
                

[2017-09-15 19:03:31,083: ERROR/MainProcess] consumer: Cannot connect to redis://localhost:6379/0: .
Trying again in 2.00 seconds...

[2017-09-15 19:03:33,087: ERROR/MainProcess] consumer: Cannot connect to redis://localhost:6379/0: .
Trying again in 4.00 seconds...

[2017-09-15 19:03:37,096: ERROR/MainProcess] consumer: Cannot connect to redis://localhost:6379/0: .
Trying again in 6.00 seconds...

^C
worker: Hitting Ctrl+C again will terminate all running tasks!

worker: Warm shutdown (MainProcess)

```
Make sure redis is up and running, run `redis-cli ping` on the terminal, make sure get a `PONG` message on the terminal. For more https://redis.io/topics/quickstart
@shubham-padia Thanks for prompt reply!! 😄 Now i am getting the same error mentioned by @bhaveshAn 

Resolving patch:
diff --git a/app/__init__.py b/app/__init__.py
--- a/app/__init__.py
+++ b/app/__init__.py
@@ -142,18 +142,18 @@ def create_app():
     if 'SENTRY_DSN' in app.config:
         sentry.init_app(app, dsn=app.config['SENTRY_DSN'])
 
+    # redis
+    redis_store.init_app(app)
+
     # elasticsearch
     if app.config['ENABLE_ELASTICSEARCH']:
         es.init_app(app)
+        with app.app_context():
+            try:
+                cron_rebuild_events_elasticsearch.delay()
+            except Exception:
+                pass
 
-    # redis
-    redis_store.init_app(app)
-
-    with app.app_context():
-        try:
-            cron_rebuild_events_elasticsearch.delay()
-        except Exception:
-            pass
     return app, _manager, db, _jwt
 
 
diff --git a/config.py b/config.py
--- a/config.py
+++ b/config.py
@@ -55,7 +55,7 @@ class Config(object):
     SERVE_STATIC = env.bool('SERVE_STATIC', default=False)
     DATABASE_QUERY_TIMEOUT = 0.1
     SENTRY_DSN = env('SENTRY_DSN', default=None)
-    ENABLE_ELASTICSEARCH = env('ENABLE_ELASTICSEARCH', default=False)
+    ENABLE_ELASTICSEARCH = env.bool('ENABLE_ELASTICSEARCH', default=False)
     ELASTICSEARCH_HOST = env('ELASTICSEARCH_HOST', default='localhost:9200')
     REDIS_URL = env('REDIS_URL', default='redis://localhost:6379/0')
 


Test patch: