Skip to content

Commit

Permalink
Merge pull request #7124 from ProcessMaker/bugfix/FOUR-16575-b-develop
Browse files Browse the repository at this point in the history
Bugfix/FOUR-16575: Bugfix/FOUR-16575: Getting OOM Memory Failures when Calling Tasks API Endpoint
  • Loading branch information
ryancooley authored Jul 24, 2024
2 parents 5cfbca5 + 7c68fe8 commit 5a7288b
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 37 deletions.
18 changes: 18 additions & 0 deletions ProcessMaker/Console/Migration/ExtendedMigrateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace ProcessMaker\Console\Migration;

use Illuminate\Database\Console\Migrations\MigrateCommand as BaseMigrateCommand;
use Illuminate\Support\Facades\Cache;
use ProcessMaker\Models\ProcessMakerModel;

class ExtendedMigrateCommand extends BaseMigrateCommand
{

public function handle(): void
{
Cache::tags(ProcessMakerModel::MIGRATION_COLUMNS_CACHE_KEY)->flush();

parent::handle();
}
}
2 changes: 1 addition & 1 deletion ProcessMaker/Http/Resources/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function toArray($request)
$array = parent::toArray($request);
$include = explode(',', $request->input('include', ''));

$this->process = Process::findOrFail($this->processRequest->process_id);
$this->process = $this->resource->process;

foreach ($this->includeMethods as $key) {
if (!in_array($key, $include)) {
Expand Down
31 changes: 31 additions & 0 deletions ProcessMaker/Models/ProcessMakerModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;

/**
* Base class that all models should extend from.
Expand All @@ -13,6 +15,8 @@ class ProcessMakerModel extends Model
{
use HasFactory;

const MIGRATION_COLUMNS_CACHE_KEY = 'migration_columns';

/**
* Prepare a date for array / JSON serialization.
*
Expand All @@ -23,4 +27,31 @@ protected function serializeDate(DateTimeInterface $date)
{
return $date->format('Y-m-d H:i:s');
}

public function scopeExclude($query, array $columns)
{
if (!empty($columns)) {
return $query;
}

$columnsToShow = array_diff($this->getTableColumns(), $columns);
$columnsToShow = array_map(function ($column) {
return $this->table . '.' . $column;
}, $columnsToShow);

return $query->select($columnsToShow);
}

private function getTableColumns()
{
$key = 'MigrMod:' . $this->getTable();

return Cache::tags(static::MIGRATION_COLUMNS_CACHE_KEY)->rememberForever(
$key, function () {
return $this->getConnection()
->getSchemaBuilder()
->getColumnListing($this->getTable());
}
);
}
}
32 changes: 0 additions & 32 deletions ProcessMaker/Models/Screen.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,28 +105,6 @@ class Screen extends ProcessMakerModel implements ScreenInterface
'updated_at',
];

/**
* Table columns.
*
* @var array
*/
protected $columns = [
'id',
'uuid',
'screen_category_id',
'title',
'description',
'type',
'config',
'computed',
'custom_css',
'created_at',
'updated_at',
'status',
'key',
'watchers',
];

/**
* Validation rules
*
Expand Down Expand Up @@ -185,16 +163,6 @@ public function projectAssets()
->wherePivot('asset_type', static::class)->withTimestamps();
}

public function scopeExclude($query, $value = [])
{
$columns = array_diff($this->columns, (array) $value);
$columns = array_map(function ($column) {
return $this->table . '.' . $column;
}, $columns);

return $query->select($columns);
}

/**
* Set multiple|single categories to the screen
*
Expand Down
9 changes: 9 additions & 0 deletions ProcessMaker/Providers/ProcessMakerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Laravel\Horizon\Horizon;
use Laravel\Passport\Passport;
use Lavary\Menu\Menu;
use Illuminate\Database\Console\Migrations\MigrateCommand;
use ProcessMaker\Console\Migration\ExtendedMigrateCommand;
use ProcessMaker\Events\ActivityAssigned;
use ProcessMaker\Events\ScreenBuilderStarting;
use ProcessMaker\Helpers\PmHash;
Expand Down Expand Up @@ -149,6 +151,13 @@ public function register(): void
$this->app->singleton(PackageManifest::class, fn () => new LicensedPackageManifest(
new Filesystem, $this->app->basePath(), $this->app->getCachedPackagesPath()
));

$this->app->extend(MigrateCommand::class, function () {
return new ExtendedMigrateCommand(
app('migrator'),
app('events')
);
});
}

/**
Expand Down
18 changes: 15 additions & 3 deletions ProcessMaker/Traits/TaskControllerIndexMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use ProcessMaker\Filters\Filter;
use ProcessMaker\Http\Resources\Task as Resource;
use ProcessMaker\Models\ProcessRequest;
use ProcessMaker\Models\ProcessRequestToken;
use ProcessMaker\Models\User;
Expand All @@ -17,8 +16,21 @@ trait TaskControllerIndexMethods
{
private function indexBaseQuery($request)
{
$query = ProcessRequestToken::with(['processRequest', 'user', 'draft']);
$query->select('process_request_tokens.*');
$query = ProcessRequestToken::exclude(['data'])->with([
'processRequest' => fn($q) => $q->exclude(['data']),
// review if bpmn is reuiqred here process
'process' => fn($q) => $q->exclude(['svg', 'warnings']),
// review if bpmn is reuiqred here processRequest.process
'processRequest.process' => fn($q) => $q->exclude(['svg', 'warnings']),
// The following lines use to much memory but reduce the number of queries
// bpmn is required here in processRequest.processVersion
// 'processRequest.processVersion' => fn($q) => $q->exclude(['svg', 'warnings']),
// review if bpmn is reuiqred here processRequest.processVersion.process
// 'processRequest.processVersion.process' => fn($q) => $q->exclude(['svg', 'warnings']),
'user',
'draft'
]);

$include = $request->input('include') ? explode(',', $request->input('include')) : [];

foreach (['data'] as $key) {
Expand Down
2 changes: 1 addition & 1 deletion ProcessMaker/Traits/TaskResourceIncludes.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private function includeScreen($request)

if ($array['screen']) {
// Apply translations to screen
$processTranslation = new ProcessTranslation($this->process);
$processTranslation = new ProcessTranslation($this->processRequest->process);
$array['screen']['config'] = $processTranslation->applyTranslations($array['screen']);

// Apply translations to nested screens
Expand Down

0 comments on commit 5a7288b

Please sign in to comment.