# Big Campaign Update failing in middle

**URL:** <https://forum.mautic.org/t/big-campaign-update-failing-in-middle/26095>\
**Category:** Product Support\
**Created:** [November 16, 2022, 8:43am UTC](https://forum.mautic.org/t/big-campaign-update-failing-in-middle/26095 "2022-11-16T08:43:11Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikew](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/mikew/32/5156_2.png) [@mikew](https://forum.mautic.org/u/mikew)\
**Post date:** [November 16, 2022, 8:43am UTC](https://forum.mautic.org/t/big-campaign-update-failing-in-middle/26095/1 "2022-11-16T08:43:11Z")

</div>

**Your software**  
My Mautic version is: 4.0.1  
My PHP version is: 7.4  
My Database type and version is: mariadb

**Your problem**  
My problem is:

We are doing a big campaign update of 800K+ records. So I wanted to start doing some benchmarking for the client and was monitoring the update.  
The benchmarking looks like the following:  
1 minute: 10K records imported  
10 minutes: 87k  
20 minutes: 167K  
31 minutes: 251K  
40 minutes: 320K  
50 minutes: 384K  
60 minutes: 440K

Then when the import hit 62% (Imported 549K) it was thrown out with the following error:

```auto
The stream or file "/var/www/mautic/app/../var/logs/mautic_prod-2022-11-16.php" could not be opened in append mode: failed to open stream: Permission denied

```

I then try and rerun the campaign update again, it runs for a bit and then the same thing.

@silavapi @joeyk - any ideas ?

Also if this happens on such a big campaign can anyone think of a way I could catch this error or monitor it and then restart the job again ?

---

<div class="post-metadata">

**Author:** ![mikew](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/mikew/32/5156_2.png) [@mikew](https://forum.mautic.org/u/mikew)\
**Post date:** [November 16, 2022, 9:04am UTC](https://forum.mautic.org/t/big-campaign-update-failing-in-middle/26095/2 "2022-11-16T09:04:40Z")

</div>

When going through the php log from today and prepping only for CAMPAIGN entries I see the following:

```auto
[2022-11-16 07:05:44] mautic.ERROR: CAMPAIGN: An exception occurred while executing 'INSERT INTO campaign_leads (date_added, manually_removed, manually_added, date_last_exited, rotation, campaign_id, lead_id) VALUES (?, ?, ?, ?, ?, ?, ?)' with params ["2022-11-16 07:0
5:44", 0, 0, null, 1, 727, "484495"]: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '727-484495' for key 'PRIMARY' [] []
[2022-11-16 07:20:43] mautic.ERROR: CAMPAIGN: An exception occurred while executing 'INSERT INTO campaign_leads (date_added, manually_removed, manually_added, date_last_exited, rotation, campaign_id, lead_id) VALUES (?, ?, ?, ?, ?, ?, ?)' with params ["2022-11-16 07:2
0:43", 0, 0, null, 1, 727, "739800"]: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '727-739800' for key 'PRIMARY' [] []
[2022-11-16 07:35:44] mautic.ERROR: CAMPAIGN: An exception occurred while executing 'INSERT INTO campaign_leads (date_added, manually_removed, manually_added, date_last_exited, rotation, campaign_id, lead_id) VALUES (?, ?, ?, ?, ?, ?, ?)' with params ["2022-11-16 07:3
5:44", 0, 0, null, 1, 727, "879230"]: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '727-879230' for key 'PRIMARY' [] []
[2022-11-16 07:50:44] mautic.ERROR: CAMPAIGN: An exception occurred while executing 'INSERT INTO campaign_leads (date_added, manually_removed, manually_added, date_last_exited, rotation, campaign_id, lead_id) VALUES (?, ?, ?, ?, ?, ?, ?)' with params ["2022-11-16 07:5
0:44", 0, 0, null, 1, 727, "1075882"]: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '727-1075882' for key 'PRIMARY' [] []
[2022-11-16 08:00:04] mautic.ERROR: CAMPAIGN: An exception occurred while executing 'INSERT INTO campaign_lead_event_log (rotation, date_triggered, is_scheduled, trigger_date, system_triggered, metadata, channel, channel_id, non_action_path_taken, event_id, lead_id, c
ampaign_id, ip_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params [3, "2022-11-16 08:00:04", 0, null, 1, "a:0:{}", "mobile_notification", null, 0, 402, "5492184", 31, null]: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '402-54921
84-3' for key 'campaign_rotation' [] []
[2022-11-16 08:00:05] mautic.ERROR: CAMPAIGN: An exception occurred while executing 'INSERT INTO campaign_lead_event_log (rotation, date_triggered, is_scheduled, trigger_date, system_triggered, metadata, channel, channel_id, non_action_path_taken, event_id, lead_id, c
ampaign_id, ip_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params [3, "2022-11-16 08:00:05", 0, null, 1, "a:0:{}", "email", 32, 0, 121, "1206546", 12, null]: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '121-1206546-3' for key 'c
ampaign_rotation' [] []
[2022-11-16 08:05:48] mautic.ERROR: CAMPAIGN: An exception occurred while executing 'INSERT INTO campaign_leads (date_added, manually_removed, manually_added, date_last_exited, rotation, campaign_id, lead_id) VALUES (?, ?, ?, ?, ?, ?, ?)' with params ["2022-11-16 08:0
5:48", 0, 0, null, 1, 727, "1149634"]: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '727-1149634' for key 'PRIMARY' [] []
[2022-11-16 08:10:10] mautic.ERROR: CAMPAIGN: An exception occurred while executing 'INSERT INTO campaign_lead_event_log (rotation, date_triggered, is_scheduled, trigger_date, system_triggered, metadata, channel, channel_id, non_action_path_taken, event_id, lead_id, c
ampaign_id, ip_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params [2, "2022-11-16 08:10:10", 0, null, 1, "a:0:{}", "email", 31, 0, 116, "650957", 12, null]: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '116-650957-2' for key 'cam
paign_rotation' [] []
[2022-11-16 08:26:28] mautic.ERROR: CAMPAIGN: An exception occurred while executing 'INSERT INTO campaign_leads (date_added, manually_removed, manually_added, date_last_exited, rotation, campaign_id, lead_id) VALUES (?, ?, ?, ?, ?, ?, ?)' with params ["2022-11-16 08:2
6:28", 0, 0, null, 1, 727, "1241546"]: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '727-1241546' for key 'PRIMARY' [] []
[2022-11-16 08:44:55] mautic.ERROR: CAMPAIGN: An exception occurred while executing 'INSERT INTO campaign_leads (date_added, manually_removed, manually_added, date_last_exited, rotation, campaign_id, lead_id) VALUES (?, ?, ?, ?, ?, ?, ?)' with params ["2022-11-16 08:4
4:55", 0, 0, null, 1, 727, "2056416"]: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '727-2056416' for key 'PRIMARY' [] []
[2022-11-16 08:45:05] mautic.ERROR: CAMPAIGN: The EntityManager is closed. [] []
[2022-11-16 08:50:58] mautic.ERROR: CAMPAIGN: An exception occurred while executing 'INSERT INTO campaign_leads (date_added, manually_removed, manually_added, date_last_exited, rotation, campaign_id, lead_id) VALUES (?, ?, ?, ?, ?, ?, ?)' with params ["2022-11-16 08:5
0:58", 0, 0, null, 1, 727, "2462150"]: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '727-2462150' for key 'PRIMARY' [] []
[2022-11-16 08:51:07] mautic.ERROR: CAMPAIGN: The EntityManager is closed. [] []
[2022-11-16 09:00:07] mautic.ERROR: CAMPAIGN: An exception occurred while executing 'INSERT INTO campaign_lead_event_log (rotation, date_triggered, is_scheduled, trigger_date, system_triggered, metadata, channel, channel_id, non_action_path_taken, event_id, lead_id, c
ampaign_id, ip_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params [2, "2022-11-16 09:00:07", 0, null, 1, "a:0:{}", "email", 92, 0, 411, "262140", 31, null]: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '411-262140-2' for key 'cam
paign_rotation' [] []

```

---

<div class="post-metadata">

**Author:** ![joeyk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/joeyk/32/11164_2.png) [@joeyk](https://forum.mautic.org/u/joeyk)\
**Post date:** [November 16, 2022, 9:11am UTC](https://forum.mautic.org/t/big-campaign-update-failing-in-middle/26095/3 "2022-11-16T09:11:11Z")

</div>

Hi, I never experienced this, but we are not running campaigns for so long time.

---

<div class="post-metadata">

**Author:** ![mikew](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/mikew/32/5156_2.png) [@mikew](https://forum.mautic.org/u/mikew)\
**Post date:** [November 16, 2022, 9:17am UTC](https://forum.mautic.org/t/big-campaign-update-failing-in-middle/26095/4 "2022-11-16T09:17:34Z")

</div>

> [@mikew](#):
>
> `SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry`

Thanks @joeyk

To add to this thread I have seen a number of different posts on this topic as well:

> [@Mautic:campaigns:trigger throws Integrity Constraint Violation for Duplicate entry for key 'campaign\_rotation'](https://forum.mautic.org/t/mautictrigger-throws-integrity-constraint-violation-for-duplicate-entry-for-key-campaign-rotation/16713/8):
>
> Having exactly the same issue on mautic 3.1. After duplicate key violation which caused due to jump to event issue. ([Github](https://github.com/mautic/mautic/pull/7605) The real issue here from my end is when this error pops up all scheduled campaigns actions that will be triggered afterwards wouldnt take any action not in this run and not afterwards. Any idea how to bypass this error so it wont affect the rest of the campaigns?

maybe @escopecz can help she some light here ?

---

<div class="post-metadata">

**Author:** ![joeyk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/joeyk/32/11164_2.png) [@joeyk](https://forum.mautic.org/u/joeyk)\
**Post date:** [November 16, 2022, 9:34am UTC](https://forum.mautic.org/t/big-campaign-update-failing-in-middle/26095/5 "2022-11-16T09:34:19Z")

</div>

I think it also matters how you run your campaign commands. I suggest paralell running using ID range in each thread.

---

<div class="post-metadata">

**Author:** ![mikew](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/mikew/32/5156_2.png) [@mikew](https://forum.mautic.org/u/mikew)\
**Post date:** [November 16, 2022, 9:41am UTC](https://forum.mautic.org/t/big-campaign-update-failing-in-middle/26095/6 "2022-11-16T09:41:39Z")

</div>

when you say ID range are you referring to running a specific campaign with -i [campaign\_id]

If so yes this is what I am doing - we have our crontab builder that I sent you a while ago that does this

---

<div class="post-metadata">

**Author:** ![joeyk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/joeyk/32/11164_2.png) [@joeyk](https://forum.mautic.org/u/joeyk)\
**Post date:** [November 16, 2022, 9:52am UTC](https://forum.mautic.org/t/big-campaign-update-failing-in-middle/26095/7 "2022-11-16T09:52:13Z")

</div>

No. I’m referring to contact ID.  
I’m sorry, I didn’t have time to look at the cronjob builder in detail, I realized it was too complicated code for me ☹

---

<div class="post-metadata">

**Author:** ![mikew](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/mikew/32/5156_2.png) [@mikew](https://forum.mautic.org/u/mikew)\
**Post date:** [November 16, 2022, 9:55am UTC](https://forum.mautic.org/t/big-campaign-update-failing-in-middle/26095/8 "2022-11-16T09:55:16Z")

</div>

how would you go about doing this on a db of 1M ?

---

<div class="post-metadata">

**Author:** ![joeyk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/joeyk/32/11164_2.png) [@joeyk](https://forum.mautic.org/u/joeyk)\
**Post date:** [November 16, 2022, 9:58am UTC](https://forum.mautic.org/t/big-campaign-update-failing-in-middle/26095/9 "2022-11-16T09:58:27Z")

</div>

Create ranges of contacts.  
We pull in the campaign members by campaign, create even batches and run the commands paralell via bash script.  
The end command looks like this apprx.  
`php /var/www/html/mautic/bin/console mautic:campaign:trigger --min-contact-id="$minId" --max-contact-id="$maxId" --thread-id="$thisbatch" --campaign-i="$thisCampaign" -f`

This way we have 10 threads processing equal number of conacts every minute. The script runs until the total id pool is completed and moves to the next campaign. It is still work in progress, but it kinda works.

I’m not saying your way is not correct, just saying we use this

---

<div class="post-metadata">

**Author:** ![mikew](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/mikew/32/5156_2.png) [@mikew](https://forum.mautic.org/u/mikew)\
**Post date:** [November 16, 2022, 10:34am UTC](https://forum.mautic.org/t/big-campaign-update-failing-in-middle/26095/10 "2022-11-16T10:34:01Z")

</div>

Hey Joey, sorry for going on here, what would the midID and maxID be ?  
If the DB is 1M, but so many anonymous users such that there can be a userid with id 5M

---

<div class="post-metadata">

**Author:** ![joeyk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/joeyk/32/11164_2.png) [@joeyk](https://forum.mautic.org/u/joeyk)\
**Post date:** [November 16, 2022, 10:36am UTC](https://forum.mautic.org/t/big-campaign-update-failing-in-middle/26095/11 "2022-11-16T10:36:54Z")

</div>

No worries, (sorry im on mobile)  
You are defining the range:  
–min-contact-id=1 --max-contact-id=10000  
–min-contact-id=10001 --max-contact-id=20000  
–min-contact-id=20001 --max-contact-id=30000  
Etc…

---

<div class="post-metadata">

**Author:** ![mikew](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/mikew/32/5156_2.png) [@mikew](https://forum.mautic.org/u/mikew)\
**Post date:** [November 16, 2022, 11:27am UTC](https://forum.mautic.org/t/big-campaign-update-failing-in-middle/26095/12 "2022-11-16T11:27:04Z")

</div>

hmm… I wonder how this will be dealt with huge DB, any thoughts on how big the range can be from min to max ?

---

<div class="post-metadata">

**Author:** ![joeyk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/joeyk/32/11164_2.png) [@joeyk](https://forum.mautic.org/u/joeyk)\
**Post date:** [November 16, 2022, 12:04pm UTC](https://forum.mautic.org/t/big-campaign-update-failing-in-middle/26095/13 "2022-11-16T12:04:42Z")

</div>

I would do it this way (based on server resources)  
Chop up the 1M into 10 threads, each 10k users.  
Then I would measure how long it runs.  
Let’s say 2 min.  
Then I would time the next batch into 2 min afterwards.  
You can alternatively look into this:

> **[Mauticast #37 🔈 Replacing Cron Jobs by Timers (Klemen Kobetič)](https://www.leuchtfeuer.com/en/mauticast/37/)**
>
> plus ➤Mautic 4.4 ➤Inboxing ➤Programming GrapesJS ➤Mautic Conference South America (São Paulo 11/2022) ➤and more

---

<div class="post-metadata">

**Author:** ![escopecz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/escopecz/32/370_2.png) [@escopecz](https://forum.mautic.org/u/escopecz)\
**Post date:** [November 16, 2022, 3:43pm UTC](https://forum.mautic.org/t/big-campaign-update-failing-in-middle/26095/14 "2022-11-16T15:43:27Z")

</div>

Please test [Avoid increment twice for campaign jump to events by fedys · Pull Request #10206 · mautic/mautic · GitHub](https://github.com/mautic/mautic/pull/10206) if it solves your problem

---

<div class="post-metadata">

**Author:** ![mikew](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/mikew/32/5156_2.png) [@mikew](https://forum.mautic.org/u/mikew)\
**Post date:** [November 16, 2022, 3:44pm UTC](https://forum.mautic.org/t/big-campaign-update-failing-in-middle/26095/15 "2022-11-16T15:44:45Z")

</div>

Thanks @escopecz will install and test, just hard to reproduce this one. Will keep you updated

---

<div class="post-metadata">

**Author:** ![joeyk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/joeyk/32/11164_2.png) [@joeyk](https://forum.mautic.org/u/joeyk)\
**Post date:** [November 25, 2022, 1:12pm UTC](https://forum.mautic.org/t/big-campaign-update-failing-in-middle/26095/16 "2022-11-25T13:12:19Z")

</div>

@mikew  
This supposed to fix multi.threading:

> <https://github.com/mautic/mautic/pull/11747>
>
> \<!-- ## Which branch should I use for my PR?
> 
> Assuming that:
> 
> a = current ma…jor release
> b = current minor release
> c = future major release
> 
> \* a.x for any features and enhancements (e.g. 5.x)
> \* a.b for any bug fixes (e.g. 4.4, 5.1)
> \* c.x for any features, enhancements or bug fixes with backward compatibility breaking changes (e.g. 5.x) --\>
> 
> | Q | A
> | -------------------------------------- | ---
> | Bug fix? (use the a.b branch) | \[Y \]
> | New feature/enhancement? (use the a.x branch) | \[N \]
> | Deprecations? | \[N \]
> | BC breaks? (use the c.x branch) | \[N \]
> | Automated tests included? | \[N \] 
> | Related user documentation PR URL | mautic/mautic-documentation#... 
> | Related developer documentation PR URL | mautic/developer-documentation#... 
> | Issue(s) addressed | Fixes #... 
> 
> \<!--
> Additionally (see https://contribute.mautic.org/contributing-to-mautic/developer/code/pull-requests#work-on-your-pull-request):
> - Always add tests and ensure they pass.
> - Bug fixes must be submitted against the lowest maintained branch where they apply
> (lowest branches are regularly merged to upper ones so they get the fixes too.)
> - Features and deprecations must be submitted against the "4.x" branch.
> \--\>
> 
> \#### Description:
> Campaign trigger command has an interesting option for multi threading which is not working, because the second process is not allowed to run:
> \`\`\`
> \[2022-11-23 14:10:01\] Script in progress. Can force execution by using --bypass-locking.
> \`\`\`
> This PR fixes this issue.
> 
> \<!--
> Please write a short README for your feature/bugfix. This will help people understand your PR and what it aims to do. If you are fixing a bug and if there is no linked issue already, please provide steps to reproduce the issue here.
> \--\>
> 
> \#### Steps to test this PR:
> 
> \<!--
> This part is really important. If you want your PR to be merged, take the time to write very clear, annotated and step by step test instructions. Do not assume any previous knowledge - testers may not be developers.
> \--\>
> 1. Open this PR on Gitpod or pull down for testing locally (see docs on testing PRs \[here\](https://contribute.mautic.org/contributing-to-mautic/tester))
> 2. Run first thread \`bin/console mautic:campaigns:trigger --max-threads=2 --thread-id=1\`
> 3. Run second thread \`bin/console mautic:campaigns:trigger --max-threads=2 --thread-id=2\`
> 4. Both command should be running simultaneously
> 
> \<!--
> If you have any deprecations, list them here along with the new alternative.
> If you have any backwards compatibility breaks, list them here.
> \--\>
