Repository: celery/celery

Issue:
Following Application examples lead to Instance of 'Celery' has no 'worker_main' member
<!--
Please fill this template entirely and do not erase parts of it.
We reserve the right to close without a response
bug reports which are incomplete.
-->
# Checklist
<!--
To check an item on the list replace [ ] with [x].
-->

- [ x] I have checked the [issues list](https://github.com/celery/celery/issues?utf8=%E2%9C%93&q=is%3Aissue+label%3A%22Category%3A+Documentation%22+)
  for similar or identical bug reports.
- [ x] I have checked the [pull requests list](https://github.com/celery/celery/pulls?q=is%3Apr+label%3A%22Category%3A+Documentation%22)
  for existing proposed fixes.
- [ x] I have checked the [commit log](https://github.com/celery/celery/commits/master)
  to find out if the bug was already fixed in the master branch.
- [x ] I have included all related issues and possible duplicate issues in this issue
      (If there are none, check this box anyway).

## Related Issues and Possible Duplicates
<!--
Please make sure to search and mention any related issues
or possible duplicates to this issue as requested by the checklist above.

This may or may not include issues in other repositories that the Celery project
maintains or other repositories that are dependencies of Celery.

If you don't know how to mention issues, please refer to Github's documentation
on the subject: https://help.github.com/en/articles/autolinked-references-and-urls#issues-and-pull-requests
-->

#### Related Issues

- None

#### Possible Duplicates

- None

# Description
I'm running celery inside a django project, from a python script. Until 5.0, I was able to run celery using worker_main() method : 
```
from celery import Celery
app = Celery()
app.worker_main()
```

From the [application related documentation page](https://docs.celeryproject.org/en/stable/userguide/application.html), nothing points out the fact that worker_main() method seems gone.

I suppose I miss something somewhere, but maybe because of some lack of clarity from documentation



Discussion:
It's the same as 
```
app.start()
```
@thedrow - Looks like some doco changes which didn't get captured in the click CLI refactor work you did prior to 5.0 (01651d2).
@NullYing

`app.start()
AttributeError: 'Celery' object has no attribute 'start'`
Seems that this worker_main issue is not the only one causing problems.

I've followed django related doc step by step, I can't get any working process. It just stops without any error message. 

Uppercase config options beginning with a defined namespace (as explained in the doc) lead to regressions messages, and it seems impossible to define any specific config variables this way.

After 4 hours of fight, I give it up. Celery 5.0 is unusable for me, and not having an up to date configuration doesn't help. Unfortunatly, downgrading to 4.4 is the only way to have something working.

Appreciate the efforts though, but seems to me that some pieces are missing right now
I have the same issue with `celery==5.0.1` (e.g. no `worker_main` or `start`, no idea how to programmatically start a worker).
Hi, any updates?
> Uppercase config options beginning with a defined namespace (as explained in the doc) lead to regressions messages, and it seems impossible to define any specific config variables this way.

This is fixed in 5.0.2.
@maybe-sybr is correct. This seems like an oversight on my part.
I'll address this shortly.
Now I remember why I removed those methods.
It's harder to implement the same with Click.

I also don't understand why you'd need to pass the command line arguments as a list here.
Still looking into this.
Using the `CliRunner` from `click.testing` fails to run `celery worker`.

I think I can provide the `start` method without any overrides which is not enough.
Hey man,

I met the same issue on python3.8. 

```
Traceback (most recent call last):
  File "celery_worker.py", line 90, in <module>
    app.worker_main(argv)
AttributeError: 'Celery' object has no attribute 'worker_main'
```


Resolving patch:
diff --git a/celery/app/base.py b/celery/app/base.py
--- a/celery/app/base.py
+++ b/celery/app/base.py
@@ -1,12 +1,14 @@
 """Actual App instance implementation."""
 import inspect
 import os
+import sys
 import threading
 import warnings
 from collections import UserDict, defaultdict, deque
 from datetime import datetime
 from operator import attrgetter
 
+from click.exceptions import Exit
 from kombu import pools
 from kombu.clocks import LamportClock
 from kombu.common import oid_from
@@ -342,6 +344,30 @@ def close(self):
         self._pool = None
         _deregister_app(self)
 
+    def start(self, argv=None):
+        from celery.bin.celery import celery
+
+        celery.params[0].default = self
+
+        try:
+            celery.main(args=argv, standalone_mode=False)
+        except Exit as e:
+            return e.exit_code
+        finally:
+            celery.params[0].default = None
+
+    def worker_main(self, argv=None):
+        if argv is None:
+            argv = sys.argv
+
+        if 'worker' not in argv:
+            raise ValueError(
+                "The worker sub-command must be specified in argv.\n"
+                "Use app.start() to programmatically start other commands."
+            )
+
+        self.start(argv=argv)
+
     def task(self, *args, **opts):
         """Decorator to create a task class out of any callable.
 


Test patch: