<commit_before>---
id: 2022-01-21-openswoole-timer-cron
author: matthew
title: 'Running cronjobs via an Openswoole timer'
draft: false
public: true
created: '2022-01-21T09:16:00-06:00'
updated: '2022-01-25T08:32:00-06:00'
tags:
    - mezzio
    - openswoole
    - php
    - swoole
---

Sites I build often utilize cronjobs to periodically pull in data from other sources.
For example, I might want to poll an API once a day, or scrape content from another website once a month.
Cronjobs are a perfect fit for this.

However, cron has a few problems:

- If the job is writing information into the file tree of your web application, you need to ensure permissions are correct, both at the filesystem level, and when writing the cronjob (e.g., running it as the same user, or changing permissions on completion).
- If you are running console tooling associated with your PHP application, you may need to worry about whether or not particular environment variables are in scope when you run the job.
- In containerized environments, usage of cron is strongly discouraged, as it means running another daemon.
  You can get around this with tools such as the [s6-overlay](https://github.com/just-containers/s6-overlay), but it's another vector for issues.

Since most sites I build anymore use [mezzio-swoole](https://docs.mezzio.dev/mezzio-swoole/), I started wondering if I might be able to handle these jobs another way.<commit_msg>feat: add image to swoole + cron post<commit_after>---
id: 2022-01-21-openswoole-timer-cron
author: matthew
title: 'Running cronjobs via an Openswoole timer'
draft: false
public: true
created: '2022-01-21T09:16:00-06:00'
updated: '2022-02-10T14:55:00-06:00'
tags:
    - mezzio
    - openswoole
    - php
    - swoole
image:
    url: https://live.staticflickr.com/7030/6812481635_ed463ae1fa_b.jpg
    creator: 'photosteve101'
    attribution_url: https://www.flickr.com/photos/42931449@N07/6812481635
    alt_text: 'Business Calendar & Schedule'
    license: 'BY'
    license_url: https://creativecommons.org/licenses/by/2.0/
---

Sites I build often utilize cronjobs to periodically pull in data from other sources.
For example, I might want to poll an API once a day, or scrape content from another website once a month.
Cronjobs are a perfect fit for this.

However, cron has a few problems:

- If the job is writing information into the file tree of your web application, you need to ensure permissions are correct, both at the filesystem level, and when writing the cronjob (e.g., running it as the same user, or changing permissions on completion).
- If you are running console tooling associated with your PHP application, you may need to worry about whether or not particular environment variables are in scope when you run the job.
- In containerized environments, usage of cron is strongly discouraged, as it means running another daemon.
  You can get around this with tools such as the [s6-overlay](https://github.com/just-containers/s6-overlay), but it's another vector for issues.

Since most sites I build anymore use [mezzio-swoole](https://docs.mezzio.dev/mezzio-swoole/), I started wondering if I might be able to handle these jobs another way.
{'fraction_non_alphanumeric': 0.07586206896551724, 'fraction_numerical': 0.04231974921630094, 'mean_word_length': 4.065079365079365, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 7, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3827540', 'n_tokens_mistral': 1020, 'n_tokens_neox': 919, 'n_words': 422}