You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Copy the `env.sample` file and name it `.env`. You can do this using the following command in your command line
21
+
```bash
22
+
$ cp env.sample .env
23
+
```
24
+
25
+
- Inside the `.env file`, create the variable `CELERY_BROKER`. If you are using Redis running on your computer, the value of `CELERY_BROKER` should be:
26
+
```sh
27
+
#.env
28
+
...
29
+
CELERY_BROKER="redis://localhost:6379"
30
+
```
31
+
20
32
- To tell Celery where to find your Django settings, add `DJANGO_SETTINGS_MODULE` to your environmental variables. You can do this by opening a terminal window and running the following command:
21
33
```bash
22
34
$ export DJANGO_SETTINGS_MODULE=core.settings
23
35
```
24
36
37
+
- The Celery configuration file, named `celery.py`, defines how Celery will operate within your Django project. It is located in your `home` directory.
38
+
```py
39
+
# home/celery.py
40
+
...
41
+
if os.environ.get('DJANGO_SETTINGS_MODULE'):
42
+
43
+
app = Celery('core')
44
+
45
+
# - namespace='CELERY' means all celery-related configuration keys should have a `CELERY_` prefix.
# Load task modules from all registered Django apps.
49
+
app.autodiscover_tasks()
50
+
```
51
+
52
+
- The configuration options for celery can be found in `core/settings.py`. More configuration options can be found in Celery's [documentation](https://docs.celeryq.dev/en/stable/userguide/configuration.html)
CELERY_RESULT_EXPIRES=60*60*24*30# Results expire after 1 month
68
+
CELERY_ACCEPT_CONTENT= ["json"]
69
+
CELERY_TASK_SERIALIZER='json'
70
+
CELERY_RESULT_SERIALIZER='json'
71
+
```
72
+
25
73
To create your scripts, head over to the `tasks_scripts` folder within the base directory. Script files saved in this location can be executed using the `Async task manager` feature.
26
74
75
+
- In the root folder of the application, create a folder called `task_logs`. You can do that from the terminal using the command:
76
+
```bash
77
+
$ mkdir tasks_logs
78
+
```
79
+
27
80
- Run the celery command from the terminal
28
81
```bash
29
82
$ celery -A home worker -l info -B
30
83
```
84
+
31
85
- Run node server to allow the use of tailwind on another terminal
32
86
```bash
33
87
$ npm run dev
@@ -48,6 +102,56 @@ Visit https://localhost:8000 to view the application.
48
102
49
103
- You can start and cancel any task from the UI that exists as a script in the `tasks_scripts` folder.
50
104
105
+
### Adding a new script
106
+
Django Celery allows you to create custom scripts that can be executed from the user interface (UI). These scripts can perform various tasks, such as backups, data processing, or sending emails.
107
+
108
+
- The first step is to locate the `tasks_scripts` directory within your project's base directory. This directory is where custom Celery scripts should be placed.
109
+
110
+
- Inside the `tasks_scripts` directory, create a new Python file. In this tutorial, we'll use the filename `backup_db.py`.
This script creates a backup of the current database in use by creating a copy of the database. Once you have added this script to the `tasks_scripts` folder, it can be executed from the application.
139
+
140
+
### Adding a new task
141
+
Tasks to be executed by Celery can be added from the user interface of the application.
142
+
143
+
- Navigate to the `Tasks` section of the application. You can access it from the `Apps` menu on the sidebar.
144
+
145
+
- The scripts located in the `tasks_scripts` folder are already preloaded and ready to be executed.
146
+
147
+
- To execute tasks, you need to be logged in as an administrator. However, anyone can view the progress of tasks in execution.
148
+
149
+

150
+
151
+
On the tasks page, you can select and execute the desired tasks. The status of the last executed task and a history of previously executed tasks are displayed for your reference.
152
+
153
+

154
+
51
155
## Conclusion
52
156
The Asynchronous task handler feature makes it easy to run time-consuming tasks without affecting the user experience. This can be helpful for tasks like sending emails, processing payments, or generating reports.
0 commit comments